Highlight all occurrence of a selected word?

后端 未结 15 1637
难免孤独
难免孤独 2020-12-12 10:05

How can I highlight all occurrence of a selected word in GVim, like in Notepad++?

相关标签:
15条回答
  • 2020-12-12 10:20

    In Normal mode:

    :set hlsearch
    

    Then search for a pattern with the command / in Normal mode, or <Ctrl>o followed by / in Insert mode. * in Normal mode will search for the next occurrence of the word under the cursor. The hlsearch option will highlight all of them if set. # will search for the previous occurrence of the word.

    To remove the highlight of the previous search:

    :nohlsearch
    

    You might wish to map :nohlsearch<CR> to some convenient key.

    0 讨论(0)
  • 2020-12-12 10:20

    First (or in your .vimrc):

    :set hlsearch
    

    Then position your cursor over the word you want highlighted, and hit *.

    hlsearch means highlight all occurrences of the current search, and * means search for the word under the cursor.

    0 讨论(0)
  • 2020-12-12 10:20

    Enable search highlighting:

    :set hlsearch
    

    Then search for the word:

    /word<Enter>
    
    0 讨论(0)
  • 2020-12-12 10:20

    My favorite for doing this is the mark.vim plugin. It allows to highlight several words in different colors simultaneously.

    Example Screenshot

    0 讨论(0)
  • 2020-12-12 10:23

    I know than it's a really old question, but if someone is interested in this feature, can check this code http://vim.wikia.com/wiki/Auto_highlight_current_word_when_idle

    " Highlight all instances of word under cursor, when idle.
    " Useful when studying strange source code.
    " Type z/ to toggle highlighting on/off.
    nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>
    function! AutoHighlightToggle()
       let @/ = ''
       if exists('#auto_highlight')
         au! auto_highlight
         augroup! auto_highlight
         setl updatetime=4000
         echo 'Highlight current word: off'
         return 0
      else
        augroup auto_highlight
        au!
        au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
        augroup end
        setl updatetime=500
        echo 'Highlight current word: ON'
      return 1
     endif
    endfunction
    
    0 讨论(0)
  • 2020-12-12 10:25

    The * key will highlight all occurrences of the word that is under the cursor.

    0 讨论(0)
提交回复
热议问题