651

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"?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yktula
  • 14,179
  • 14
  • 48
  • 71
  • 42
    Get used to using `:help`. `:help /` shows help about searching, and the answer to your question appears just a little bit down the page. – Chris Morgan Jul 07 '11 at 08:19

7 Answers7

1068

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.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
  • @XavierT. any idea, how i can jump a fixed number of results in search. I mean for example jump to the 10th matching line – GP cyborg Nov 11 '15 at 15:24
  • 1
    @GPcyborg : `n` like most vim operator can be prefixed with a number to repeat the command. If you type `10n` it will move to 10th result (after the initial one). It also works for all motion operator like `3j` to go down 3 lines. – Xavier T. Nov 12 '15 at 09:26
  • 1
    Press Enter to exit search typing before you press n or N. – Igris Dankey Aug 19 '22 at 04:09
265

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 7
    And in case it wasn't obvious, `n` and `N` take you forward and backward through the `*` matches once you've pressed `*`. (Or you can just keep pressing `*` to go forward or `#` to go backward, but using those shifted-keys is generally sub-optimal.) – Herbert Sitz Jul 07 '11 at 15:57
  • 4
    @Herbert, actually n and N don't take you "forward" and "backward" resp. As @Xavier noted above, n is **next** and N is **previous**. Control of forwards and backwards searching is done by stating the search with either the '/' key and the '?' key or with the '*' key and the '#' key. – Rob Wells Jul 08 '11 at 09:22
  • I don't think * is that useful... most of the time I'm searching for a fragment of a word (for example, a part of a function name). I don't want to `/Func`, have it take me to `SomeFunc` and then press '*' to go to the next instance of `SomeFunc` when the next instance of `Func` is in `SomeOtherFunc`. – weberc2 Sep 19 '13 at 14:50
  • Even worse, with C++ I find it doesn't go from method invocation to method implementation because `/\` does not match `"className::foo()"` – puk Dec 08 '13 at 20:12
120

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)
Community
  • 1
  • 1
iamnotsam
  • 9,470
  • 7
  • 33
  • 30
  • 2
    Typing `*` is incorrect. I.e. a file `aa aaa`. Search `/aa`, you have to matches. On the first match pressing `*` changes the search term. – Bernhard Sep 23 '14 at 09:32
  • 1
    n isn't 'next' but "repeat last search". So entering ?var will start searching upwards from your current position for 'var'. And 'N' is actually "repeat last search in opposite direction" so for this example that would search downwards for 'var' from current position. – Rob Wells Mar 27 '15 at 17:22
19

You may be looking for the n key.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sam hocevar
  • 11,853
  • 5
  • 49
  • 68
17

Typing n will go to the next match.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
Jim Deville
  • 10,632
  • 1
  • 37
  • 47
15

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.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
  • 3
    If you're feeling inspired by `q/`, you should also play with `q:`. Both are really handy! (Not really related to this question, though.) – Micah Elliott Sep 04 '15 at 15:28
  • Thanks, Micah. I knew about 'q:' already, although I don't use it that much. But 'q/' was entirely missed in my vim education, and I think will get a lot of use. – Stabledog Sep 10 '15 at 13:18
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bass chuck
  • 77
  • 1
  • 2
  • 1
    "When we execute a search, Vim scans forward from the current cursor position,stopping on the first match that it finds. " As Practical Vim points out. – bass chuck Oct 16 '17 at 04:58