Alex.Zhang
Alex.Zhang

Reputation: 133

How to highlight multiple words in vim

I am checking a log file using vim. There's only one word can be highlighted, when I search another word,the second word is highlighted but the previous one is not highlighted anymore.

Can anyone show me a way that easily highlight/cancel highlight multiple words in vim? For example, I want to highlight words "stack", "over" and "flow", then I want to cancel the highlight of "stack" and highlight another word "error". So that I can analyze the log more efficiently. Thanks.

Upvotes: 13

Views: 17971

Answers (3)

Raviteja
Raviteja

Reputation: 2022

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 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.

Upvotes: 29

Ingo Karkat
Ingo Karkat

Reputation: 172590

To have all words highlighted in the same color and be able to search and replace all of then, add them as alternatives to the search pattern, e.g. /foo\|bar. My SearchAlternatives plugin provides handy mappings to add and remove patterns.

If you want different colors for different matches, you need a highlight mechanism different from the built-in search. My Mark plugin is used by many people for that. (The plugin page has links to alternative plugins.)

Upvotes: 3

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

/\v\/folder|\/copy - search for \folder and \copy

and add to .vimrc set hlsearch

Upvotes: 14

Related Questions