PVA – Archives

Sưu tầm những bài viết hay trên internet…

(Vim) Find by file content In Your Project

From: http://lostechies.com/derickbailey/2010/05/11/vim-grep-find-all-occurrences-of-text-in-your-project/

The search pattern is as easy as any other / search, with a few different options:

:vim[grep][!] /{pattern}/[g][j] {file} ...
  • The /pattern/ is a standard vim search pattern
  • The “g” option find all matches – including multiple matches on the same line.
  • The “j” option turns off the auto-goto first match feature.
  • The file list allows you to specify individual files, or wild-card searches, including recursive search with file globs

There are a lot of great tips and tricks on that wikia page. You’ll want to read up on them to get an idea of what you can really do with this.

Searching For .path_to_command In All Files

The working directory in vim can be considered the ‘project root’ in that all file / folder based commands work relative to the working directory. This makes a global find in all files of a project easy since I always start up vim in the working folder that I want. For example, using NERDTree I can see that I am in the root of my albacore project as the working directory:

image

To search within the albacore ‘project’ folder and find all instances of ‘path_to_command’, I used vimgrep like this:

:vimgrep /path_to_command/gj ./**/*.rb

This told vim to grep for ‘path_to_command’ and return every result on every line, in any .rb file, recursively, starting at the project folder. Run the :cw command after the grep is done, and the list of files and locations will show up:

image

You can navigate the list with standard vim navigation (h,j,k,l, etc). Hit <enter> on the item you want and the top window split will jump to the file/line/column:

image

 

Macro’ing The Change To Speed Things Up

Now that I have an easy way to find the instances of ‘path_to_command’, I can easily macro the change to ‘command’. For this macro, I assume that my cursor is currently on the ‘p’ in ‘path_to_command’ and that my grep search results is in the window split below the current cursor selection:

:qccwcommand<Esc>:w<Ctl-W>jj<Enter>q

This macro (which includes the record start / stop, if you’re wondering) will change the current ‘path_to_command’ word to ‘command’, write the file out to disk, change window splits to the grep search results, move down one line in the results and navigate to that result’s location. I ran the macro by hitting @c and then verifying the line selected was one that I wanted to change. For a few instances where I did not want to change the line, I manually navigated to the next search result and then re-ran the @c macro.

All told, I got through the changes in a few minutes with this macro. I would estimate that this saved me at least 30 minutes to an hour of trying to remember if I found all the places I needed to change prior to running the test suite to verify my changes.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.