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!

Leave a Reply

Your email address will not be published. Required fields are marked *