Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight multiple words in vim

Tags:

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.

like image 903
Alex.Zhang Avatar asked Nov 14 '13 06:11

Alex.Zhang


People also ask

How do I highlight all instances of words in vim?

The hlsearch option will highlight all of them if set. # will search for the previous occurrence of the word. You might wish to map :nohlsearch<CR> to some convenient key. But you can go back to the position where you pressed * or # by <Ctrl>o in normal mode.

How do I search for multiple words in vi?

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return. Type n to go to the next occurrence of the string.

How do I set highlights in vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

How do you change multiple words in vim?

Change and repeat Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.


2 Answers

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.

like image 122
Raviteja Avatar answered Sep 30 '22 20:09

Raviteja


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

and add to .vimrc set hlsearch

like image 37
Dmitry Zagorulkin Avatar answered Sep 30 '22 20:09

Dmitry Zagorulkin