Using grep inside Vim
Changing a feature's functionality often involves tracking down where it is used throughout a program's source code. Or say you're trying to find a nasty bug that is generated by a rouge stored procedure and you want to find every location it is called. To search for text in files I often grep via the command line, look at the matches and then manually load up each file. Today I'll join the two methods together in a fairly eloquent way using vim.
Using it
| Command | Description |
|---|---|
| :cd to\my\dir | Change to the directory I want to look in |
| :lvimgrep 'discount' *.txt | Search for the text discount in any .txt file in the current directory |
| :lnext | Go to the next match |
| @: | Repeat :lnext, go to the next match |
| @@ | Keep looking through matches |
| :lopen | Open the list of matches – looks very similar to grep output in the command line |
| jjj | Move down the list to another match |
| <Enter> | Moves to the match under the cursor |
| :lprevious | Move to the previous match |
| :lnfile | Move to the next file |
| :lfirst | Go back to the very first match |
Worth mentioning is that ** (starstar) can be used to search down directories and help related to vimgrep is in quickfix.txt, so use that reference to discover more.