Highlight all occurrence of a selected word?

后端 未结 15 1689
难免孤独
难免孤独 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:12

    to highlight word without moving cursor, plop

    " highlight reg. ex. in @/ register
    set hlsearch
    " remap `*`/`#` to search forwards/backwards (resp.)
    " w/o moving cursor
    nnoremap  * :execute "normal! *N"
    nnoremap  # :execute "normal! #n"
    

    into your vimrc.

    What's nice about this is g* and g# will still work like "normal" * and #.


    To set hlsearch off, you can use "short-form" (unless you have another function that starts with "noh" in command mode): :noh. Or you can use long version: :nohlsearch

    For extreme convenience (I find myself toggling hlsearch maybe 20 times per day), you can map something to toggle hlsearch like so:

    " search highlight toggle
    nnoremap  st :set hlsearch!
    

    .:. if your is \ (it is by default), you can press \st (quickly) in normal mode to toggle hlsearch.

    Or maybe you just want to have :noh mapped:

    " search clear
    nnoremap  sc :nohlsearch
    

    The above simply runs :nohlsearch so (unlike :set hlsearch!) it will still highlight word next time you press * or # in normal mode.

    cheers

提交回复
热议问题