Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In vim, I often perform searches to hop to a word or phrase instead of navigating there with h/j/k/l. Then I hit n to hop between occurrences.

Say I've got this text:

Time flies like an arrow; fruit flies like a banana. - Groucho Marx

I type /an arrow and hit enter. That phrase is highlighted, and I jump to it with n.

Now I want to visually select that text, maybe to change it or delete it. (Yes, I'm aware of the :s substitution command.)

Since my cursor is at the letter "a" at the beginning of "an arrow," I can hit v, then press e a couple of times to highlight the entire phrase. But I have a feeling there's a shorter and more semantic way. After all, I've already specified the text I'm interested in.

How might I compose a command to say "visually select the current search selection?"

share|improve this question
up vote 12 down vote accepted

You can use gn in version 7.4 onwards (and gN to go backwards). It replaces the v//e trick.

Search forward for the last used search pattern, like with `n`, and start Visual mode to select the match.

See :help gn or this Vimcast for more information.

share|improve this answer
This duplicates a comment to another answer and is not very useful as it stands. It might be a good idea if you quoted the documentation so that your answer can stand on its own. – bwDraco Jan 22 '15 at 0:13
Happy to make the answer better but I'm not sure whether that would help. The question lays out the objective neatly ("visually select the next search result") and gn does exactly as asked. Submitting it as a separate answer emphasises that it's a complete substitute for v//e. I just edited my answer to add documentation, is that better? – David Lord Jan 22 '15 at 0:23
That's good, thanks. If you need help, be sure to check out the tour and help center. Welcome to Super User, and we hope you enjoy your stay. – bwDraco Jan 22 '15 at 0:31
Fantastic! Thanks for updating this thread with the latest and greatest. :) – Nathan Long Jan 22 '15 at 17:01

Try this:

/an arrow

It goes into visual mode, repeats the last search and selects until the end.

share|improve this answer
Awesome! Thanks. – Nathan Long Dec 22 '10 at 16:35
From Vim 7.4 on gn does this too (without changing what n means) – smathy Nov 19 '14 at 23:53

I found that v//e by itself is insufficient because after you exit visual mode, jumping to the next/previous search result puts the cursor at the end of the result, instead of at its beginning as you would normally expect.

To fix this, we should first jump to the end of the search result (//e) and then visually select to its beginning (v??):

" visually select a search result
nnoremap g/ //e<Enter>v??<Enter>


share|improve this answer
For symmetry: nnoremap g? ??b<cr>v//e<cr> (does the same thing but searching backwards instead of forwards) – Kyle Strand Jun 20 '14 at 20:21

There is now a Vim-Search-Objects plugin that makes Vim treat search matches as regular text objects, so you can simply va/ (visual-select a match) after you perform a search.

share|improve this answer

A "dumbber" way to do it is to map a key (in this case F5) like below:

nnoremap <F5> v/<c-r>=strpart(@/,1) . '/e+1'<CR><CR>

After /an arrow hit F5.

share|improve this answer
That selects one more character because of the +1. Omit that and it will select only the part that was searched for. On the other hand, nnoremap <F5> v//e<CR> seems to work just fine. – Dennis Williamson Dec 22 '10 at 19:19
@Dennis Williamson,+1 is necessary to select exactly what was searched, if you remove +1 the last character will not be selected because it doesn't selects the character where the cursor is. Any way nnoremap <F5> v//e<CR> is much better. – Johnny Dec 23 '10 at 11:36
It does select the character for me. If I press x or c after doing your F5 but without the +1 the whole search string, including the character under the cursor, is deleted. – Dennis Williamson Dec 23 '10 at 15:50
@Dennis Williamson, I just learned that my 'selection' option is set to "exclusive", but the default value is "inclusive". That's why it worked for you without +1. (See :help selection ) – Johnny Jan 5 '11 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .