Vim+Grep: Find All Occurrences Of Text In Your Project

I’m in the middle of some sweeping changes in albacore – changing the .path_to_command property to .command for all of the tasks that run an external tool. The core of the change is easy. I have a RunCommand module that contains the definition for .path_to_command. However, there are a lot of unit tests and example code chunks that use the .path_to_command attribute all throughout the code. Back when I was using E TextEditor or another editor that had the concept of a “project”, I would do a “find all occurrences” in the project and look for the .path_to_command property. In vim, I wasn’t sure how to do this, but I did know that I could accomplish the same thing using grep on the command line. A quick google search for “vim grep” turned up  a page on the vim wikia and had me up and running with vimgrep (among other options) in no time.

From the wikia page:

These commands all fill a list with the results of their search. "grep" and "vimgrep" fill the "quickfix list", which can be opened with :cw or :copen, and is a list shared between ALL windows. [… This] can be used to instantly jump to the matching line in whatever file it occurs in.

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.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Albacore, Productivity, Ruby, Smoke Test, Vim. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.lostechies.com/members/gnschenker/default.aspx Gabriel N. Schenker

    Although I am using gvim and viemu in many of my daily tasks and like it very much I still think that some of the tasks are too complicated compared to how you do such things in VS. The above task would take you 5 seconds to accomplish…

  • http://www.lostechies.com/members/louissalin/default.aspx Louis Salin

    Derick, don’t listen to Gabriel. He does that to me every day at work while I become faster at using vim. One day, we’ll rock his socks off and he’ll wish I’d have jumped in with us.

  • http://www.lostechies.com/members/louissalin/default.aspx Louis Salin

    Your post reminded me of a blog I read recently: http://kevin-berridge.blogspot.com/2009/04/vim-update-many-files.html

    He does a search/replace through files with just a single command.

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    ooooh, the bitter rivalry of coworkers! :)

    gabriel – go look at the link that Louis posted… there’s your “5 seconds” version in vim :)

  • AK

    From the borders it seems you are running Windows and if my eyesight is fine then the VIM is looking gorgeous. Please let me (us) know how you set it up. The fonts, colours, smoothness etcetera. That too on Windows. If not setup with al the bundles and all, even then intro to make it good and how to manage VIM on Windows and keep it good. I am a VIM beginner and tweaking around it on Xubuntu has been a lot easier. I have hit a dead end for Windows though.

    PS. Oh, I also notice it’s a 2 year old post :(

    • http://mutedsolutions.com Derick Bailey

      yes, this was gVim on windows, using the twilight color theme. nothing special beyond that.

      • AK

        Ok. Actually this http://imgur.com/5VrSo is how mine looks. Shall the theme. Does it also fixes the fonts and makes it smooth and good looking?