如何在 VIM 中突出显示所选单词的所有出现位置?

发布于 2024-09-13 20:22:39 字数 51 浏览 2 评论 0原文

如何在 GVim 中突出显示所选单词的所有出现位置,就像在 Notepad++ 中一样?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

贱贱哒 2024-09-20 20:22:39

在普通模式下:

:set hlsearch

然后在普通模式下使用命令 / 搜索模式,或者在插入模式下使用 o 后跟 / 搜索模式。正常模式下的 * 将搜索光标下单词的下一个出现位置。如果设置,hlsearch 选项将突出显示所有内容。 # 将搜索该单词之前出现的位置。

要删除先前搜索的突出显示:

:nohlsearch

您可能希望将 :nohlsearch 映射到某个方便的键。

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.

红颜悴 2024-09-20 20:22:39

* 键将突出显示光标下出现的所有单词。

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

故事与诗 2024-09-20 20:22:39

我知道这是一个非常老的问题,但如果有人对此功能感兴趣,可以检查此代码
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

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
情绪 2024-09-20 20:22:39

最简单的方法,在正常模式下输入 *

我也有这些映射来启用和禁用

"highligh search enabled by default
set hlsearch
"now you can toggle it
nnoremap <S-F11> <ESC>:set hls! hls?<cr>
inoremap <S-F11> <C-o>:set hls! hls?<cr>
vnoremap <S-F11> <ESC>:set hls! hls?<cr> <bar> gv

通过单击选择单词

set mouse=a     "Enables mouse click
nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>

奖金:CountWordFunction

fun! CountWordFunction()
    try
        let l:win_view = winsaveview()
        let l:old_query = getreg('/')
        let var = expand("<cword>")
        exec "%s/" . var . "//gn"
    finally
        call winrestview(l:win_view)
        call setreg('/', l:old_query)
    endtry
endfun
" Bellow we set a command "CountWord" and a mapping to count word
" change as you like it
command! -nargs=0 CountWord :call CountWordFunction()
nnoremap <f3> :CountWord<CR>

用鼠标选择单词并立即计算出现次数:
OBS:请注意,在这个版本中,我们在末尾有“CountWord”命令

nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>

the simplest way, type in normal mode *

I also have these mappings to enable and disable

"highligh search enabled by default
set hlsearch
"now you can toggle it
nnoremap <S-F11> <ESC>:set hls! hls?<cr>
inoremap <S-F11> <C-o>:set hls! hls?<cr>
vnoremap <S-F11> <ESC>:set hls! hls?<cr> <bar> gv

Select word by clickin on it

set mouse=a     "Enables mouse click
nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>

Bonus: CountWordFunction

fun! CountWordFunction()
    try
        let l:win_view = winsaveview()
        let l:old_query = getreg('/')
        let var = expand("<cword>")
        exec "%s/" . var . "//gn"
    finally
        call winrestview(l:win_view)
        call setreg('/', l:old_query)
    endtry
endfun
" Bellow we set a command "CountWord" and a mapping to count word
" change as you like it
command! -nargs=0 CountWord :call CountWordFunction()
nnoremap <f3> :CountWord<CR>

Selecting word with mouse and counting occurrences at once:
OBS: Notice that in this version we have "CountWord" command at the end

nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>
橘香 2024-09-20 20:22:39

基于搜索的解决方案(*/...)移动光标,这可能会很不幸。

另一种方法是使用 增强的 mark.vim 插件,然后完成您的 < code>.vimrc 让双击触发突出显示(我不知道键盘选择如何触发命令):

"Use Mark plugin to highlight selected word  
map <2-leftmouse> \m   

它允许多次突出显示、持久化等。

要删除突出显示,可以:

  • 再次双击
  • < code>:Mark(关闭直到下一个选择)
  • :MarkClear

Search based solutions (*, /...) move cursor, which may be unfortunate.

An alternative is to use enhanced mark.vim plugin, then complete your .vimrc to let double-click trigger highlighting (I don't know how a keyboard selection may trigger a command) :

"Use Mark plugin to highlight selected word  
map <2-leftmouse> \m   

It allows multiple highlightings, persistence, etc.

To remove highlighting, either :

  • Double click again
  • :Mark (switch off until next selection)
  • :MarkClear
毁梦 2024-09-20 20:22:39

首先(或在您的 .vimrc 中):

:set hlsearch

然后将光标放在要突出显示的单词上,然后点击 *

hlsearch 表示突出显示当前搜索的所有匹配项,* 表示搜索光标下的单词。

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.

同展鸳鸯锦 2024-09-20 20:22:39

要突出显示单词而不移动光标,请

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

进入 vimrc。

这样做的好处是 g*g# 仍将像“正常”*# 一样工作。


要关闭 hlsearch,您可以使用“short-form”(除非您有另一个在命令模式下以“noh”开头的函数)::noh。或者您可以使用长版本: :nohlsearch

为了极其方便(我发现自己每天可能会切换 hlsearch 20 次),您可以映射某些内容来切换 hlsearch< /code> 像这样:

" search highlight toggle
nnoremap <silent> <leader>st :set hlsearch!<cr>

.:.如果您的 \(默认情况下),您可以按 \st(快速)在正常模式下切换hlsearch

或者也许您只想映射 :noh

" search clear
nnoremap <silent> <leader>sc :nohlsearch<cr>

上面只是运行 :nohlsearch 所以(与 :set hlsearch! 不同)它仍然会突出显示下次在正常模式下按*#时会出现单词。

干杯

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 <silent> * :execute "normal! *N"<cr>
nnoremap <silent> # :execute "normal! #n"<cr>

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 <silent> <leader>st :set hlsearch!<cr>

.:. if your <leader> 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 <silent> <leader>sc :nohlsearch<cr>

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

cheers

野生奥特曼 2024-09-20 20:22:39

例如这个插件:

只需在下面搜索光标位于 vimawesome.com

键,如 clagccs 提到,突出显示不会与您的搜索冲突https://vim.fandom.com/wiki/Auto_highlight_current_word_when_idle

与搜索不冲突的屏幕截图:
输入图片此处描述
注意:

  • 默认情况下,vim-照亮高亮显示,在我的屏幕截图中,我默认切换到下划线
  • vim-照亮高亮显示/下划线光标下的单词,在我的屏幕截图中,我取消设置它,
  • 我的颜色方案非常灰色。检查您的以进行自定义。

For example this plugIns:

Just search for under cursor in vimawesome.com

The key, as clagccs mentioned, is that the highlight does NOT conflict with your search: https://vim.fandom.com/wiki/Auto_highlight_current_word_when_idle

Screen-shot of how it does NOT conflict with search:
enter image description here
Notes:

  • vim-illuminate highlights by default, in my screen-shot I switched to underline
  • vim-illuminate highlights/underlines word under cursor by default, in my screen-shot I unset it
  • my colorschemes are very grey-ish. Check yours to customize it too.
各自安好 2024-09-20 20:22:39

首先确保通过发出以下命令启用 hlsearch

:set hlsearch

您还可以将其添加到

set hlsearch

现在设置的 .vimrc 文件中,当您在命令模式下使用快速搜索机制或常规搜索命令时,所有结果都会突出显示。要在结果之间向前移动,请按“n” 要向后移动,请按“N”

在正常模式下,要快速搜索光标下的单词并跳转到一个命令中的下一个匹配项,请按“*”,您也可以搜索光标下的单词,然后按 '#' 移动到上一个出现位置

在正常模式下,也可以使用 来调用快速搜索以

/searchterm<Enter>

删除出现的高亮显示,我已将其绑定到我的 .vimrc 中的快捷方式

:nohl

First ensure that hlsearch is enabled by issuing the following command

:set hlsearch

You can also add this to your .vimrc file as set

set hlsearch

now when you use the quick search mechanism in command mode or a regular search command, all results will be highlighted. To move forward between results, press 'n' to move backwards press 'N'

In normal mode, to perform a quick search for the word under the cursor and to jump to the next occurrence in one command press '*', you can also search for the word under the cursor and move to the previous occurrence by pressing '#'

In normal mode, quick search can also be invoked with the

/searchterm<Enter>

to remove highlights on ocuurences use, I have bound this to a shortcut in my .vimrc

:nohl
謌踐踏愛綪 2024-09-20 20:22:39
set hlsearch

或许 ?

set hlsearch

maybe ?

风渺 2024-09-20 20:22:39

启用搜索突出显示:

:set hlsearch

然后搜索单词:

/word<Enter>

Enable search highlighting:

:set hlsearch

Then search for the word:

/word<Enter>
山川志 2024-09-20 20:22:39

我最喜欢这样做的是 mark.vim 插件。
它允许同时用不同的颜色突出显示多个单词。

屏幕截图示例

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

Example Screenshot

埋情葬爱 2024-09-20 20:22:39

使用 autocmd CursorMoved * exe printf('match IncSearch /\V​​\<%s\>/', escape(expand(''), '/\'))

确保您已将 IncSearch 设置为某项。例如调用 s:Attr('IncSearch', 'reverse')。或者,您可以使用另一个突出显示组来代替它。

这将立即突出显示光标下出现的所有单词。我发现当我快速编写代码时,延迟会减慢我的速度。突出显示颜色将与单词的颜色相匹配,因此它与您的方案保持一致。

Use autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))

Make sure you have IncSearch set to something. e.g call s:Attr('IncSearch', 'reverse'). Alternatively you can use another highlight group in its place.

This will highlight all occurrences of words under your cursor without a delay. I find that a delay slows me down when I'm wizzing through code. The highlight color will match the color of the word, so it stays consistent with your scheme.

梦萦几度 2024-09-20 20:22:39

为什么不只是:
z/

这将突出显示光标下的当前单词以及任何其他出现的单词。而且您不必为您正在搜索的每个项目发出单独的命令。也许这在邪恶的 gvim 中不可用?默认情况下它在 vim 中。

* 仅当您希望光标移动到下一个出现的位置时才有用。当以视觉方式比较两个事物时,您通常不希望光标移动,而且每次都按 * 键很烦人。

Why not just:
z/

That will highlight the current word under cursor and any other occurrences. And you don't have to give a separate command for each item you're searching for. Perhaps that's not available in the unholy gvim? It's in vim by default.

* is only good if you want the cursor to move to the next occurrence. When comparing two things visually you often don't want the cursor to move, and it's annoying to hit the * key every time.

暖风昔人 2024-09-20 20:22:39
  1. 将这些行添加到您的 ~/.vimrc 文件


" 突出显示搜索到的项目
设置 hlsearch
" F8 递归搜索光标下的单词 :copen , 关闭 -> :ccl
nnoremap:grep! “\<<字词>\>” 。 -r CR :copen 33 CR ;

  1. 使用 :so% 重新加载设置
  2. 在正常模型中检查该单词。

  3. 按 * 按 F8 在整个项目中递归搜索该单词

  1. Add those lines in your ~/.vimrc file


" highlight the searched items
set hlsearch
" F8 search for word under the cursor recursively , :copen , to close -> :ccl
nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>

  1. Reload the settings with :so%
  2. In normal model go over the word.

  3. Press * Press F8 to search recursively bellow your whole project over the word

淡莣 2024-09-20 20:22:39

-->如果你想突出显示 gvim 中出现的文本,

请选择 text &复制
然后?粘贴所选文本(注意:这不适用于插入模式)

--> if you want to highlight a text occurrences in gvim

Select the text & copy
then ?paste the selected text (Note: This will not work for insert mode)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文