"Find next" in Vim

VimEditing

Vim Problem Overview


To search forward in Vim for cake, I'd type /cake, but the cursor jumps to the first match when I press return. Is there a Vim command analogous to "find next"?

Vim Solutions


Solution 1 - Vim

It is n for next and N for previous.

And if you use reverse search with ? (for example, ?cake) instead of /, it is the other way round.

If it is installed on your system, you should try to run vimtutor command from your terminal, which will start a tutorial of the basic Vim commands.

Rob Wells advice about * and # is also very pertinent.

Solution 2 - Vim

The most useful shortcut in Vim, IMHO, is the * key.

Put the cursor on a word and hit the * key and you will jump to the next instance of that word.

The # key does the same, but it jumps to the previous instance of the word.

It is truly a time saver.

Solution 3 - Vim

When I was beginning I needed to watch a demo.

How to search in Vim
  1. type /
  2. type search term e.g. "var"
  3. press enter
  4. for next instance press n (for previous N)

Solution 4 - Vim

You may be looking for the n key.

Solution 5 - Vim

Typing n will go to the next match.

Solution 6 - Vim

As discussed, there are several ways to search:

/pattern
?pattern
* (and g*, which I sometimes use in macros)
# (and g#)

plus, navigating prev/next with N and n.

You can also edit/recall your search history by pulling up the search prompt with / and then cycle with C-p/C-n. Even more useful is q/, which takes you to a window where you can navigate the search history.

Also for consideration is the all-important 'hlsearch' (type :hls to enable). This makes it much easier to find multiple instances of your pattern. You might even want make your matches extra bright with something like:

hi Search ctermfg=yellow ctermbg=red guifg=...

But then you might go crazy with constant yellow matches all over your screen. So you’ll often find yourself using :noh. This is so common that a mapping is in order:

nmap <leader>z :noh<CR>

I easily remember this one as z since I used to constantly type /zz<CR> (which is a fast-to-type uncommon occurrence) to clear my highlighting. But the :noh mapping is way better.

Solution 7 - Vim

If you press Ctrl + Enter after you press something like "/wordforsearch", then you can find the word "wordforsearch" in the current line. Then press n for the next match; press N for previous match.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYktulaView Question on Stackoverflow
Solution 1 - VimXavier T.View Answer on Stackoverflow
Solution 2 - VimRob WellsView Answer on Stackoverflow
Solution 3 - VimiamnotsamView Answer on Stackoverflow
Solution 4 - Vimsam hocevarView Answer on Stackoverflow
Solution 5 - VimJim DevilleView Answer on Stackoverflow
Solution 6 - VimMicah ElliottView Answer on Stackoverflow
Solution 7 - Vimbass chuckView Answer on Stackoverflow