Here’s what Vim’s documentation has to say about the
gn command (
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.
You know how pressing
n tells Vim to jump to the start of the next match?
gn tells Vim to start visual mode and select the next match.
When used in this fashion, the
gn command makes Vim’s search behave like most graphical text editors, where the search command selects the current match.
As you might expect, the
gN command lets you visually select the previous match.
I find the
gn command especially useful in Operator-pending mode. For example,
dgn will delete the next match,
cgn will do the same then switch to Insert mode,
gUgn will convert the next match to uppercase characters.
:h operator for more ideas.
gn with the dot command
Suppose that we have a document containing several occurrences of the word ‘Normal’ and we’d like to change each occurrence to ‘Visual’.
We can run
/Normal to search for the word ‘Normal’, then type
cgnVisual<Esc> to change the next match to the word ‘Visual’.
You’re probably familiar with the technique of pressing
n. to repeat the change for each subsequent match. In Practical Vim, I call this pattern of usage The Dot Formula.
It lets us use two keystrokes per change.
In this context, we can do better.
The dot command repeats the last change, which is equivalent to typing
We could translate that into plain English as: “change the next match”.
We don’t have to press
. does the job by itself.
That makes it just one keystroke per change!
You need Vim 7.4.110 to use
You can use the
gn command if you are running Vim 7.4 at patch level 110 or greater.
gn feature was first introduced by Christian Brabandt in Vim 7.3 with patch 610, then the functionality was refined with patches 625, 636, 645, 647, and 1275. The feature was made widely available in Vim 7.4, but if you are running a version less than patch level 110 then the
gUgn command demonstrated in this video won’t work.
Practical Vim - tip 84
In the 1st edition of Practical Vim, tip 84 goes over a different approach for making this same kind of change, which is nowhere near as tidy! When I wrote this edition, the
gn feature hadn’t yet been added to Vim. I’ll have to revise this tip when the time comes to write a new edition of Practical Vim.
If you’re stuck on an older version of Vim, note that you can get similar functionality by installing the textobj-lastpat plugin by Kana Natsuno.
I used to consider this to be an essential plugin, but don’t now that the
gn command is built-in to Vim.