Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the number of matches during an incremental search

Tags:

vim

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?

like image 318
bdesham Avatar asked May 29 '13 14:05

bdesham


2 Answers

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.

like image 58
timss Avatar answered Oct 25 '22 13:10

timss


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_%
like image 38
Peter Rincker Avatar answered Oct 25 '22 14:10

Peter Rincker