3

I have started working on a huge PHP application that has thousands of lines of code in each file, with lots of huge if blocks, classes, and functions all existing in the same file. I'm not the only dev working on it, so I cannot refactor!

I have tried using the Tags List plugin but it does not really help. Is there any way to have VIM respect only a particular code block, and ignore the rest of the file? I am hoping for some or all of these features:

  1. Enable line numbering only for the current code block, starting from 1 at the line containing the opening {, and showing no numbering for lines preceding it or after the closing }.
  2. Searching with / would be restricted only to the block in question.

I am thinking along the lines of selecting the current block and editing it in a new buffer when enabling the mode, then replacing the existing block with the edited block when exiting the mode. However, I am having trouble actually implementing this feature. My current version is this:

map <F7> <Esc>mO<C-V>aBy:new<Return>p:set nu<Return>:set ft=php<Return>ggi<?php<Return><Esc>
map <F8> <Esc>ggdd<C-V>aBx:bp<Return>`O<C-V>aBp

However, this has several issues, such as the inability to perform incremental saves.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • 1
    See http://stackoverflow.com/questions/2921752/limiting-search-scope-for-code-in-vim – kikuchiyo Feb 23 '12 at 17:03
  • For function limited search: http://drchip.0sites.net/astronaut/vim/index.html#FUNCSRCH – William Pursell Feb 23 '12 at 17:06
  • @kikuchiyo: That is helpful for the occasional search, but tedious for working much in the block. Therefore the desire to have a separate buffer for the block of interest. – dotancohen Feb 23 '12 at 17:08

2 Answers2

2

I would be very surprised if Vim allows the kind of line numbering you ask for.

This plugin (and 1 or 2 similar ones IIRC) allows you to visually select a region of your current file, work on it in another buffer and put everything back in its place in the original file on :w.

romainl
  • 186,200
  • 21
  • 280
  • 313
1

Even if it's not the solution you are wanting, I think the following can help you to solve your problem.

You can use phpfolding plugin, which folds by PHP syntax (functions, classes, methods, PhpDoc...)

You can then select a fold by pressing v$ over the closed fold and execute whatever you want with :whatever. For example, :s/this/self/g to substitute all this for self in the fold. When you press :, vim will automatically add '<,'> to denote following command it's only for the visually selected text.

Waiting for Dev...
  • 12,629
  • 5
  • 47
  • 57