Vim Find Next
In Vim, the previous action can be repeated with the dot command. This is how Vim power users can get so much done so quickly: Structuring their operations into commands that can simply be repeated with a tap of the .
character. The gn
command is one oft-overlooked Vim command (added in version 7.4) that can help build repeatable replacement operations.
To find a matching pattern in Vim, we simply use the /
command. Type /foo
and Vim will find the next reference to “foo” in your text and move your cursor there. From here, we can perform any kind of replacement. For example we could change the word with cw
by typing cwbar<Esc>
to change the word to “bar”. Now, we can press n
to go to the next “foo” match and press .
to repeat the same change.
But gn
gives us an even better way. Read the :help gn
docs for the full details but here’s the short summary: Pressing gn
will jump forward to the next match of the last used search pattern and visually select it. We can prepend this with c
for “change” to get cgn
which will find the next match of the previously used search pattern, select it, and enter insert mode. Then we can type our replacement and press <Esc>
. To repeat this, we only have to press .
instead of n
and .
— shortening our action to a single key press.
Vim Search Next Example
Watch the demo to see this in action. Here’s what I do:
/ million
– Finds the first match of ” million”cgn
– Changes the next match of my patternM USD<Esc>
– My replacement pattern- Then I just press
.
to repeat it
Vim Find Next Reverse (Vim Search Previous)
There is another variation of this, gN
, which does the same thing in reverse. So if you want to change progressively backwards from your cursor position through the file, use that gN
.
You can combine gn
with all kinds of other commands, too. Try dgn
to delete the next match — repeatable, of course, with just a dot.
Frequently Asked Questions
What does gn do in Vim?
The gn
command in Vim searches forward for the last used search pattern and then enter visual mode and select the last match.
How do I use the Vim gn command to find next?
To use gn
in Vim, first make a search using the / command. Then you can press gn
and Vim will take you to the next match and visually select it automatically.
How do I use the Vim gN command to find previous?
To use gN
in Vim, first make a search using the /
command. Then you can press gN
and Vim will take you to the previous match and visually select it automatically.
What are the Vim gn help docs?
gn
Search forward for the last used search pattern, like with n
, and start Visual mode to select the match. If the cursor is on the match, visually selects it. If an operator is pending, operates on the match. E.g., dgn
deletes the text of the next match. If Visual mode is active, extends the selection until the end of the next match.
wrapscan applies
Note: Unlike n
the search direction does not depend on the previous search command.
What are the Vim gN help docs?
Like gn
but searches backward, like with N