Vim is grep, and more vim search tips

Something I learned the other day blew my mind even after years of using VI/vim. Because I'd also been using grep for years.

The name 'grep' comes from the vi command g/re/p

g in ex-mode of vi means 'global', so any :g command will apply to all the text in the current buffer.

p after 're' means print.

're' in g/re/p actually just means a regular expression should go there (a vim regular expression, which is different fromregex in PHP or even a re string in grep - this is where vim magic modes can help). Meaning if you actually try running g/re/p that it will treat 're' as the regular expression matching anything with the string 're' in it. And so that example would print any line in the file containing the string 're'.

A more useful example: g/^ *function/p which would print any function declarations in a language like PHP or JavaScript (while probably missing anonymous functions). Just like running grep '^ *function' on a file or stdin. You'll never forget g/re/p.

And you know how grep -v prints the inverse of what would otherwise be matched (all lines NOT matching the regular expression)? Well, :v is the inverse of :g in vi!

What's the advantage of using vim to grep instead of grep? Besides printing, you can run other commands on the matched lines! E.g. g/re/d deletes all matching lines, g/re/t$ copies matched lines to the end of the file.

But if you want to operate on multiple files at once then use :vimgrep.

Search with: / ? * #

You probably know that / is the vi search command. You can type some string and vim will search either when you hit enter or as you're typing if you enable set incsearch. Did you know the string can be any regular expression?

The first search match after the current cursor position moves the cursor to the match. Pressing 'n' takes you to the next match. And you can combine counts with 'n' e.g. 10n for the next 10th match.

You can also search up for a match instead of down. Just use '?' instead of '/' e.g. ?^function.

You can also seach for the word currently under the cursor with '' and this can be combined with a count too. Or you can search in the reverse direction for the word under the cursor with '#' instead of ''.

Search exact word

The '' command actually does something magic when it searches. You can see it when you run '' and then hit '/' and the up key to view the last '/' command, which was created by '*' in this case. You'll see the word 'word' is surrounded by the greater-than and less-than symbols, e.g. /\<word\>. Those characters stop searching at word boundaries which can either mean whitespace or punctuation.

Case insensitive search

In my ~/.vimrc I set set ignorecase to enable case-insensitive search. This is more useful for English text rather than code, but when you find what you want but it was capitolized differently from what you thought then you'll be grateful and I have been enough times to consider this a useful default. You can disable it for one search by appending '\C' to the end, e.g. /html\C.

Add new comment