有没有办法在(g)Vim中突出显示多个搜索?

问题描述 投票:56回答:10

我想在Vim / gVim中搜索多个字符串,并以不同的颜色突出显示它们。有没有办法用开箱即用的Vim或插件做到这一点?

vim highlighting
10个回答
18
投票

试试“Highlight multiple words”,它使用matchadd()


-1
投票

我更喜欢高亮插件,简单而且足够,可以自动突出显示不同颜色的不同单词。

http://www.vim.org/scripts/script.php?script_id=1599


52
投票

在vim编辑器中有两种简单的方法可以突出显示多个单词。

  1. 转到搜索模式,即键入“/”然后键入\ v后跟要搜索的单词,用“|”分隔(管)。 例如:/ \ vword1 | word2 | word3
  2. 转到搜索模式,然后键入要用'\ |'分隔的搜索字词。 例如:/ word1 \ | word2 \ | word3

基本上,第一种方法是使您进入正则表达式模式,这样您就不需要在用于搜索的每个管道或其他分隔符之前添加任何额外的反斜杠。


41
投票

对于两种搜索模式,这可以手动完成,无需任何脚本。

:match Search /pattern/
:match Search /<CTRL-R>/   # highlight the current search pattern

搜索是突出显示组的名称,使用完成选择另一个组以使用不同的颜色突出显示。

 :match <TAB>
 :match <TAB>    # completion will list all highlight group

当您无法使用自己的vim配置时,这非常方便。

:match none      # clear the match pattern to stop highlighting

27
投票

要在vim中搜索多个字符串,您可以这样做:

/search1\|search2

这是有效的,并将突出search1search2,但具有相同的颜色。你必须在vim编辑器中这样做。


5
投票

是的,开箱即用你可以使用matchadd()

要添加突出显示,例如。用于尾随空格:

:highlight ExtraWhitespace ctermbg=grey guibg=grey
:call matchadd('ExtraWhitespace', '\s\+$', 11)

要查看所有匹配项:

:echo getmatches()

要删除匹配,请使用matchdelete()。例如。:

:call matchdelete(7)

4
投票

MultipleSearch:同时突出显示多个搜索,每个搜索都有不同的颜色。

http://www.vim.org/scripts/script.php?script_id=479

:Search <pattern1> //will highlight all occurences of <pattern1> in the current buffer.
A subsequent :Search <pattern2> will highlight all occurences of <pattern2> in the current buffer.

1
投票
:%s /red\|green\|blue/

我不知道如何为不同的关键字保留不同的颜色。谢谢。


0
投票

MultipleSearch2是另一个与vim搜索集成的脚本:http://www.vim.org/scripts/script.php?script_id=1183


0
投票

我的Mark plugin可以同时突出显示不同颜色的几个单词,如内置搜索。它带有许多映射和命令,允许持久化模式,并支持多个调色板。

© www.soinside.com 2019 - 2024. All rights reserved.