You are on page 1of 4

17/01/2020 highlighting - Is there any way to highlight multiple searches in (g)Vim?

- Stack Overflow

Is there any way to highlight multiple searches in (g)Vim?


Asked 10 years, 9 months ago Active 1 year, 4 months ago Viewed 45k times

I want to search for multiple strings in Vim/gVim and have them highlighted in different colours. Is
there a way of doing this with out-the-box Vim or with a plug-in?
61
vim highlighting

edited Jul 27 '17 at 13:52 asked Apr 1 '09 at 7:40


21 Mateusz Piotrowski feihtthief
4,990 6 36 58 4,295 3 23 25

10 Answers

Try "Highlight multiple words", which uses matchadd() .

18 edited Aug 8 '14 at 16:33 answered Apr 1 '09 at 7:44


the Tin Man George V. Reilly
142k 28 187 267 12.4k 6 35 36

There are two simple ways to highlight multiple words in vim editor.

1. Go to search mode i.e. type '/' and then type \v followed by the words you want to search
61 separated by '|' (pipe).
Ex: /\vword1|word2|word3
2. Go to search mode and type the words you want to search separated by '\|'.
Ex: /word1\|word2\|word3

Basically the first way puts you in the regular expression mode so that you do not need to put
any extra back slashes before every pipe or other delimiters used for searching.

answered Mar 5 '15 at 10:29


Raviteja
993 1 8 10

This is especially useful since you can still use 'n' (or 'N') to go to the next (or previous) result. – mgarey
May 25 '18 at 23:37

The only downside is that you don't get multiple colors... – Dave Dopson Jan 6 at 19:30

This can be done manually, without any script, for two search patterns.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/704434/is-there-any-way-to-highlight-multiple-searches-in-gvim 1/4
17/01/2020 highlighting - Is there any way to highlight multiple searches in (g)Vim? - Stack Overflow

45 :match Search /pattern/


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

Search is the name of the highlight group, use the completion to select another group to
highlight with a different color.

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

This an be handy when you cannot use your own vim configuration.

:match none # clear the match pattern to stop highlighting

edited Jul 30 '10 at 7:18 answered Jul 30 '10 at 6:56


philant
29.4k 10 60 102

4 Did not work for me. For anyone wondering try this: stackoverflow.com/questions/4162664/… – Eric Chen
Dec 3 '13 at 2:54

For searching multiple strings in vim you can do like:

27 /search1\|search2

This works, and will highlight both search1 and search2 , but with same color. You have to do
this in vim editor.

edited Jun 3 '16 at 12:12 answered Dec 4 '14 at 22:15


Jair López Naren
622 1 5 15 2,301 1 16 14

what if you want to look for search1 but not matching search2, how can I modify that command line in
vim? – medev21 Nov 15 '17 at 16:53

Yes, out-of-the-box you can use matchadd() .

To add a highlight, eg. for trailing whitespace:


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

To view all matches:

:echo getmatches()

To remove matches use matchdelete() . Eg.:


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/704434/is-there-any-way-to-highlight-multiple-searches-in-gvim 2/4
17/01/2020 highlighting - Is there any way to highlight multiple searches in (g)Vim? - Stack Overflow

:call matchdelete(7)

answered Dec 15 '12 at 4:23


James Haigh
1,032 1 10 22

MultipleSearch : Highlight multiple searches at the same time, each with a different color.

http://www.vim.org/scripts/script.php?script_id=479
4
: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.

edited Sep 17 '18 at 11:54 answered Jul 1 '09 at 19:32


Naga Kiran
7,488 5 34 47

:%s /red\|green\|blue/

2 I am not sure about how to keep different colors for different keyword though. Thanks.

edited Jul 7 '18 at 2:46 answered Jul 6 '18 at 22:11


Roshana Pitigala Pri Bhi
5,895 6 31 55 31 1

MultipleSearch2 is another script which is integrated with vim's search:


http://www.vim.org/scripts/script.php?script_id=1183
0
answered May 3 '09 at 13:57
user12371
119 1 8

My Mark plugin can highlight several words in different colors simultaneously, like the built-in
search. It comes with many mappings and commands, allows to persist the patterns, and
0 supports multiple color palettes.

answered Mar 28 '14 at 14:40


Ingo Karkat
142k 15 168 222

Does it have github page with guideline description? It is not easy to read on vim.org. Also not easy to
report issues. – Ertuğrul Altınboğa Aug 31 '15 at 5:57
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
How can I install this plugin by Vundle? Becasue the code in github vim-scripts is too old. –
our Terms of Service
 Ertuğrul. Altınboğa Aug 31 '15 at 6:18
https://stackoverflow.com/questions/704434/is-there-any-way-to-highlight-multiple-searches-in-gvim 3/4
17/01/2020 highlighting - Is there any way to highlight multiple searches in (g)Vim? - Stack Overflow

@LiMingHung That version from vim-scripts will work, but yes, it is outdated. You could ask the guys from
vim-scripts to update it; they have some annoying issues with their scraper. – Ingo Karkat Aug 31 '15 at
20:45

1 Is it possible put your plugin on github? It can install by Vundle directly. Doesn't have to rely on vim-scripts.
– Ertuğrul Altınboğa Sep 1 '15 at 3:00

@ErtuğrulAltınboğa: Mark.vim is now on GitHub! – Ingo Karkat Sep 27 '17 at 6:52

I prefer highlight plugin, simple and enough, can highlight different words with differently colors
automatically.
-1 http://www.vim.org/scripts/script.php?script_id=1599

answered Apr 19 '14 at 2:53


diabloneo
1,629 13 16

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/704434/is-there-any-way-to-highlight-multiple-searches-in-gvim 4/4

You might also like