Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do incremenatal search in Vim like it is done in Emacs?

When using incremental search in vim, the cursor immediately goes to the next occurrence of the search term as far as you have typed it. Emacs has a similar incremental search function. However, there is a feature of emacs isearch that I have found very useful that I really would like in vim. In emacs, if you type:

<ctrl-s>word

it immediately goes to "word", just like vim incremental search. In emacs, you can now type additional <ctrl-s> to move to the next result occurrence of "word" and in emacs this does not end your incremental search session. To do the same in vim, you must hit <cr>, to end the search term, then hit n to go to the next search result. Because emacs does not end the search session, you can do things like:

<ctrl-s>word<ctrl-s><ctrl-s>more

This lets you "home in" on your search once you get closer to where you want to go and turns out to be incredibly useful!

Is there a way to get vim to do the same?

like image 209
00prometheus Avatar asked Oct 22 '16 13:10

00prometheus


1 Answers

Recent versions of Vim provide builtin mappings of command-line mode to do exactly what you want.

You can find this in help:

CTRL-G

When 'incsearch' is set, entering a search pattern for "/" or "?" and the current match is displayed then CTRL-G will move to the next match (does not take search-offset into account). Use CTRL-T to move to the previous match. Hint: on a regular keyboard T is above G.

Patch numbers for reference as not all installations have the mappings:

  • Patch 7.4.2259 made Ctrl-N/Ctrl-P act like Ctrl-S/Ctrl-R in Emacs.

  • Patch 7.4.2268 changed the mappings to Ctrl-G/Ctrl-T (the ones used initially have different purpose).

like image 127
xaizek Avatar answered Nov 10 '22 00:11

xaizek