59

I'm using putty on a Linux machine. My usual method for searching in vi is entering the slash /, then my search string, enter, then go through all the matches by n or shift + n.

I was wondering if vi had a feature like Eclipse's Incremental Search (ctrl + j), wherein as I type, it jumps to the next logical match. I think one of my office mates used to do it but I'm not sure.

Adam
  • 15,537
  • 2
  • 42
  • 63
Michael Sanchez
  • 1,215
  • 1
  • 11
  • 19
  • 2
    For the record, incremental search is covered by vimtutor. Anybody who is at least half-serious about using Vim efficiently should really start their Vim life by a vimtutor session. After all, it only takes half an hour or so. – nilo Jul 16 '20 at 15:33
  • Expected to recall a feature presented at beginning of vimlife? That's the exact time you wouldn't memory-bank a non-critical feature – Zach_is_my_name Apr 12 '22 at 10:43

1 Answers1

98

If you're using vim (and who isn't nowadays), you can enable incremental search with:

:set incsearch

Then just use the regular search command / and it will move the highlight as you add characters to the search string.

I also like hlsearch as well since it highlights all found items (once the search command is done) for easy viewing.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • how do you cycle through the results? Say I type `/app` and I have the words `app` and `apple`, how do I cycle through the results? – J86 Feb 22 '21 at 18:37
  • 1
    @J86: have a look at https://stackoverflow.com/questions/9195010/vim-incremental-search-next-result. – paxdiablo Feb 22 '21 at 23:26
  • How would you use `hlsearch` with `incsearch`, but prevent Vim from jumping to the first result? – Felix Crazzolara May 08 '21 at 17:01
  • 1
    @Felix, you should be asking a *question* about that rather than leaving a comment. The entire SO swarm will see the former, very few people will see (and act on) the latter. In any case, I'm not sure I understand. The whole point of `incsearch` is to jump to the first match depending on what you're typing in. If you didn't want to do that, you would disable `incsearch`, surely? – paxdiablo May 08 '21 at 23:30
  • Definitely worth adding both `incsearch` and `hlsearch` to .vimrc. – Mark Kahn Jul 27 '21 at 13:04