vim: list all matching lines

vim_compOne of the coolest features of Notepad++ is you can find all matches (occurrences) of a pattern in a file in a list. Here are some ways to do the same in vim, sans any external plugin.

1. vimgrep (Error List)

This is a command to be used in the vim command mode. The syntax is:

:vimgrep pattern %

To open the list of matches in a buffer:

:copen

Use <Up> and <Down> keys to navigate the list, <Enter> to select a match. Traverse the matches in the open file using <n> and <N> the regular way.

Note that you can replace vimgrep with normal grep. This will increase one step (lists on the console first) but works almost the same way.

2. lvim (Location List)

Run the following in the command mode:

:lvim pattern %

To open the list

:lopen

Navigation is similar to that in vimgrep.

3. global search

To get the list of all matches in the file, run the following in command mode:

:g/regular-expression/p

Note that grep got its name from this command sequence!

Comment

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 )

Google+ photo

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

Connecting to %s