12

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?

4
  • How about this sequence: 1) /word, 2) <CR>, 3) n, ..., n, 4) /, 5, <UP>? You'll return to your search command, i.e. /word. Then you can continue entering more. Commented Oct 22, 2016 at 14:04
  • Yes, that works (and is roughly what I do now), it's just that its less convenient than the emacs version. Vim should be more convenient than emacs at all times! ;-) Commented Oct 22, 2016 at 14:07
  • Of course the hardest part is hitting <UP>, you could instead use <C-r>/ to insert search register that is contains your previous searched item.
    – dNitro
    Commented Oct 22, 2016 at 14:12
  • '/' - to initiate a search 'Ctrl-l' - to expand search pattern using characters after the cursor (similar to 'Ctrl-w' in emacs).
    – maximk
    Commented Dec 11, 2017 at 7:30

4 Answers 4

16

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).

3
  • I upgraded vim, and this works! However, the keymappings by yolenoyer and Ruslan are actually better: They let me continue on an old search! Ctrl-T/Ctrl-G does not let me append on the previous search, which is at the core of what I want. Commented Oct 22, 2016 at 17:53
  • 1
    @00prometheus What do you mean by saying that you can't append to previous search? Pattern remains in place for me and I can continue editing it.
    – xaizek
    Commented Oct 22, 2016 at 18:06
  • 1
    Sorry, I figured it out now! Simply do a search, hit <ctrl-g>/<ctrl-t> and type some more to home in without leaving search at all. Perfect! Commented Oct 23, 2016 at 1:49
6

The two mappings below let me hit <Tab> or <S-Tab> to jump to the next or previous match without leaving incremental search:

" needed for mapping <Tab> in command-line mode
set wildcharm=<C-z>

cnoremap <expr> <Tab>   getcmdtype() == "/" \|\| getcmdtype() == "?" ? "<CR>/<C-r>/" : "<C-z>"
cnoremap <expr> <S-Tab> getcmdtype() == "/" \|\| getcmdtype() == "?" ? "<CR>?<C-r>/" : "<S-Tab>"
4
  • What is the advantage of the getcmdtype conditional compared to just plain keymapping? This is deeper into vim than I am accustomed to go :-) Commented Oct 22, 2016 at 15:58
  • Right now, I have nnoremap <C-s> /<UP> cnoremap <C-s> <CR>/<UP> cnoremap <C-z> <CR>?<UP>, in my .vimrc. It allows me to use ctrl-s to go forwards and ctrl-z to go back. Commented Oct 22, 2016 at 16:00
  • @00prometheus: This is a nice emulation of xaizek's answer for older Vim versions. Commented Oct 24, 2016 at 12:54
  • If you add zv after the first <CR>, it will also open folds if needed. E.g. cnoremap <expr> <Tab> getcmdtype() == "/" \|\| getcmdtype() == "?" ? "<CR>zv/<C-r>/" : "<C-z>"
    – Von
    Commented Jun 3, 2017 at 16:06
1

With the same idea:

:cnoremap <c-s> <cr>/<up>

With this mapping, he use of Ctrl-L during search is even more useful.

2
  • I tried this as well, but when I do it, the search jumps two results at a time. Are you getting different results? Commented Oct 22, 2016 at 14:39
  • If you change @Ruslans's mapping to be nnoremap <C-s> /<Up> as well, then it seems to work! Commented Oct 22, 2016 at 14:47
1

You can use a key mapping:

nnoremap <C-s> /<Up>

(for normal mode)

If you want to stay the the previous match (not to move to the next match), you may just return to the previous match after /<Up>:

nnoremap <C-s> N/<Up>

Usage:

  1. /word<CR> - start searching for word
  2. n, ..., n - move to the next matches
  3. <C-s>, i.e. Control + s - call the previous search (1)
  4. Type more - start searching for /wordmore
6
  • This is getting us there! I also tried adding :cnoremap <C-s> <CR>n/<Up> as well, so I can hit ctrl-s to go to the next match without leaving the search at all, but that jumps two results at a time. Any idea how to avoid that? Commented Oct 22, 2016 at 14:38
  • @00prometheus, how about nnoremap <C-s> N/<Up>? Commented Oct 22, 2016 at 14:48
  • @yolenoyer figured it out; see his answer! Commented Oct 22, 2016 at 14:56
  • @00prometheus, his solution doesn't work for me. But my mapping works just as it is described in the question. Commented Oct 22, 2016 at 15:10
  • Strange, it works for me. I am using nnoremap <C-s> /<Up> and cnoremap <c-s> <cr>/<up>in my .vimrc now, and it works great! Now I just need to find a free key for reverse-isearch as well! :-) Commented Oct 22, 2016 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.