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

One feature of Notepad++, which I find really useful and haven't found elsewhere, is the highlighting of other text that is identical to the one currently selected.

Is there something similar possible with vi(m)? (Of course, there is. But how do I achieve it?) That is, any of those:

  • If I am in Visual Mode and have text selected: Highlight identical text

  • If I have searched /foo, highlight all instances of foo.

  • If I am at the beginning of a string (series of characters, numbers or underscores), highlight all other matching strings (prefered solution).

The last one is similar to the closing parentheses matching and IMHO the most useful.

Edit: For my second use case, I found a solution (that is, Google found it...):

:set hls

However, the others remain.

share|improve this question
up vote 3 down vote accepted

For your third requirement,

nnoremap , :mat Error "<C-R><C-W>"<CR>
Put this in your vimrc file..
Press comma to highlight all occurrences of the word at cursor.
Also, pressing * or # will highlight all occurrences of the string at cursor when hlsearch is set
For your first requirement,
vnoremap <silent> , :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy:mat Error "<C-R><C-R>=substitute(
  \escape(@", '/".*$^~['), '_s+', '\_s\+', 'g')<CR>"<CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>
Found this here. Modified to your needs.

share|improve this answer
Cool, that quite did it. Haven't known the # trick before. (However, how do I cancel again the search highlighting?) – Boldewyn Apr 2 '10 at 9:37
just type in command ':noh' – asdfg Apr 2 '10 at 10:07
:help hlsearch

 When there is a previous search pattern, highlight all its matches.

so, put

set hlsearch

to your vimrc

share|improve this answer
...I really get distracted by all those unicorns! Where did Jeff Atwood get all those dozens (hundreds, thousands?) of different images from? Anyway, thanks for the answer. Seems like I found this one while you typed. – Boldewyn Apr 1 '10 at 12:13
@Boldewyn,… – GmonC Apr 1 '10 at 15:17

For door number three, that is what the # key does. It will also jump to the beginning of the previous string of such. Asterisk(*) does the opposite: jump to the beginning of the next string of such.

share|improve this answer
Thanks! The asterisk is, too, new for me. – Boldewyn Apr 2 '10 at 9:37

You must log in to answer this question.

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