bdesham
bdesham

Reputation: 16089

Showing the number of matches during an incremental search

Most IDEs’ text editors will, while you’re searching for a certain string, display information like “this is match 3 of 7”. Is there any way to get Vim to display this information when you move to a match using n and N?

Upvotes: 5

Views: 792

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45147

The most common way is use the n flag with the substitution command.

:%s/set//gn

Or use the current pattern via :%s//gn. This gives you some of the feedback you asked for.

However I prefer to use :vimgrep and the quickfix list. Search for your pattern via:

:vimgrep/set/ %

This searches the current file, %, and adds the matches to the quickfix list. Then you can move through the quickfix list via :cnext or :cprevious. Upon moving through the quickfix list text will display at the bottom showing something like this (1 of 5). By using the :copen command a window showing the quickfix results will open. Move to the pattern via pressing <cr> on a quickfix item.

There are some drawbacks to using :vimgrep.

  • :vimgrep as of vim 7.3 does not support using the current pattern i.e. no :vimgrep// %. Instead one must use <c-r>/ to pull in the search pattern register and possibly escape any /'s.
  • The use of % for the current file means the file must exists, so you can not search a scratch buffer.
  • :cnext, :cprev, and friends are rather verbose compared to nice and simple n and N. Adding nice mappings can overcome this. I use [q and ]q from Tim Pope's excellent unimpaired plugin.

For more help see:

:h :s_flags
:h :vimg
:h c_CTRL-R
:h quote/
:h quickfix
:h c_%

Upvotes: 2

timss
timss

Reputation: 10270

From Show Count of Matches in Vim:

What you want is probably the plugin IndexedSearch.
When doing /set it'll display count, and the search query in the command line:

Match 5 of 81  /set/

Install it using your favorite plugin manager. I recommend Vundle.

Upvotes: 6

Related Questions