vim - Highlight identical strings in vi(m)

07
2013-08
  • Boldewyn

    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.

  • Answers
  • asdfg

    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
    edit:
    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.

  • akira
    :help hlsearch
    
     When there is a previous search pattern, highlight all its matches.
    

    so, put

    set hlsearch
    

    to your vimrc

  • Kevin M

    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.


  • Related Question

    highlighting - Highlight email?
  • Jarvis

    I often find certain portions of email more important than the rest, so is there a way for me to highlight or mark certain sentences for future reference?

    Are there any mail clients that support something like this?


  • Related Answers
  • Matthew Lock

    Microsoft Outlook lets you open a message and then edit it. You can then apply any formatting operation to it (including highlighting if the email is in HTML/Rich Text) before closing it.

    Open the message into it's own window, then Edit -> Edit Message, and bring up the formatting toolbar.


    The other option is to just forward the email to yourself, and edit it with highlighting before you send it.

  • caliban

    none has the ability (which is a feature long asked for by power email users).

    My current setup involves Thunderbird with the XNote add-on, and instead of highlighting, I use the notes to indicate important portions of my emails.

    You could try that - it's more versatile too.