LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Tuesday, October 21. 2008

Vimgrep and Vim Project

Chris Hartjes today was on a quest for a "find in project" feature for Vim. "Find in Project" was a feature of Textmate that he'd grown accustomed to and was having trouble finding an equivalent for.

The funny thing is that Textmate is a newcomer, and, of course, vim has had such a feature for years. The thing to remember with vim, of course, is its unix roots; typically if you know the unix command for doing something, you can find what you need in vim. In this case, the key is the vimgrep plugin, which ships in the standard vim distribution.

There are a variety of resources on vimgrep. The vim documentation includes a chapter on it, and a quick google search on the subject turns up some nice tutorials immediately. If you've ever used grep, the syntax is very straightforward:

vimgrep /{pattern}/[g][j] {file} ...

The "g" option indicates that all matches for a search will be returned instead of just one per line, and the "j" option tells vim not to jump to the first match automatically. What does the "g" flag really mean, though, and how are searches returned?

Vimgrep returns search results in what's known as a "quickfix" window, and this is where the vimgrep documentation falls apart... it doesn't explain what this is, or link to it (which would be a nice indication that it actually has a separate topic for this).

The Quickfix window is a pane that shows a search result per line. Each line shows the file that matches, the line number, and the contents of that line:

/home/matthew/git/bugapp/application/controllers/helpers/GetForm.php|10| * @var Zend_Loader_PluginLoader

You can't do much from this window; it simply serves as a visual indicator of what file you're currently looking at from the list. However, in the main window, you can start iterating through the results one at a time, using a subset of the Quickfix commands. As a quick summary:

  • :cc will move to the next match in the list
  • :cn will move to the next match in the list
  • :cp will move to the previous match in the list
  • :cr will rewind to the first match in the list
  • :cla will fast forward to the last match in the list

When done, you can simply close the Quickfix window/pane, and continue working.

I should note that vimgrep is cross-platform. On *nix-based systems, it defaults to using the native grep command, but it also contains an internal (slower) implementation for use on operating systems that do not provide grep by default. You may also map the command to alternate implementations if desired.

I personally use this feature most with the project plugin. Project maps vimgrep to two different commands: <Leader>g and <Leader>G. The first will grep all files in the current project at the current level; the second does the same, but also recurses into subprojects. This is an incredibly easy way to refactor code, particularly for name changes.

Posted by Matthew Weier O'Phinney in Programming, Perl, PHP at 07:36 | Comments (9) | Trackbacks (0)
Defined tags for this entry: perl, php, programming, vim
Related entries by tags:
Module Bootstraps in Zend Framework: Do's and Don'ts
Responding to Different Content Types in RESTful ZF Apps
Symfony Live 2010
Creating Re-Usable Zend_Application Resource Plugins
Quick Start to Zend_Application_Bootstrap

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Excellent information Matthew! It's because of you I've started using VIM more.
#1 Jon W (Link) on 2008-10-21 08:50 (Reply)
@Matthew

Well, vimgrep has the functionality I want but I would like to see a dropdown presented that I can then scroll through to find the file I'm looking for. If you look at the link I put in my blog posting to Jamis Buck's posting about FuzzyFineTextmate, that's the type of interface I'm looking for.
#2 Chris Hartjes (Link) on 2008-10-21 09:43 (Reply)
Find in project...

$ grep -Rn "search string" .
or
:!grep -Rn "search string" .

The command means:
Search the current directory (".") recursively for "search string", reporting line numbers (the 'n' flag).

What's so difficult? And you get all of the other grep features with that.
#3 Thomas on 2008-10-21 12:52 (Reply)
Integration in vim is nice -- you can immediately start editing the files found, as well as see persistent context for them. As I mentioned, it actually executes grep and pipes the information back to vim; it's simply a convenience feature for when you're already editing.

Further, the Project plugin for vim is particularly useful as it will grep through only the files you've defined in your project -- which may or may not mirror a given directory structure 1:1.
#3.1 Matthew Weier O'Phinney (Link) on 2008-10-21 13:17 (Reply)
I simply can't figure out the Project plugin (I know it supports Project-wide grep, etc.). Do you know of any resources? I found the docs on the plugin page to be confusing, and I couldn't get it to work properly on my own (I read all of the helpdocs too).
#3.1.1 Thomas on 2008-10-21 14:52 (Reply)
I guess people don't read what I actually wrote. :-)

I do use vimgrep, I just want a better interface to it, that's all. That's something I'm willing to pay for.
#3.2 Chris Hartjes (Link) on 2008-10-22 21:55 (Reply)
hi
i have using following code in bootstrap similar to manual zf:
$route = new Zend_Controller_Router_Route(
array(
'host' => array(
'regex' => '([a-z]+).mysite.com',
'reverse' => '%s.mysite.com',
'params' => array(
1 => 'username'
)
),
'path' => ''
),
array(
'module' => 'users',
'controller' => 'profile',
'action' => 'index'
)
);
$router->addRoute('profile', $route);
?>
Array to string conversion in ....\library\Zend\Controller\Router\Route.php on line 74
what is problem?
but a error showed to me :
#4 zaman on 2008-11-16 02:05 (Reply)
This question has nothing to do with the post. If you need help with Zend Framework, please direct your questions to the various Zend Framework mailing lists, which can be found at http://framework.zend.com/archives
#4.1 Matthew Weier O'Phinney (Link) on 2008-11-16 12:39 (Reply)
what about ctags? is that something like what textmate does? try googling "site:vimdoc.sourceforge.net ctags" and read the first two hits.. it seems including an URL here makes me look like a spammer
#5 kennethd on 2008-11-22 19:32 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back March '10 Forward
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

March 2010
February 2010
January 2010
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml cw09
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml git
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml rest
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zendcon09
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net