How to highlight multiple words in vim - 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.

There are two simple ways to highlight multiple words in vim editor.
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
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.

/\v\/folder|\/copy - search for \folder and \copy
and add to .vimrc set hlsearch

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

Related

VIM: How to count a pattern on a line restricting from column A to column B similar to :s/,//gn?

I have a line that looks like the following, which I am viewing in vim.
0,0,0,1.791759,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5.278115,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
It is from a feature vector file, each row is an instance and each column is the feature value for that feature number. I would like to figure out which feature number 5.27 corresponds to. I know the
s/,//gn
will count the number of commas in the line, but how do I restrict the command to count the number of commas in the line up to the columns with the number 5.27?
I have seen these two posts that seem relevant but cannot seem to piece them together: How to compute the number of times word appeared in a file or in some range and Search and replace in a range of line and column
s/,\ze.*5\.27//gn
The interesting part is the \ze which sets the end of the match. See :h /\ze for more information
Select the wanted area with visual mode and do
:s/\v%V%(,)//gn
\v enables us to escape less operators with \
%V limits the search to matches that start inside the visual selection
%() keeps the search together if you include alternations with |
It's not pretty but it works. See help files for /\v, \%V and \%(
There are also several versions of a plugin called vis.vim, which offers easier commands that aim to do just the above. However I haven't gotten any of them to work so I'll not comment on that further.
try this
s/,.\{-}5.27//gn
it should work.

auto highlight dictionary words when opening text file in vim

How would I make vim highlight all the words that are in my dictionary when opening a text file (at startup)?
My preferred way would be to add some 2 or 3 lines settings/fun/autocmd to my vimrc, but if it is not possible, what would be the plugin offering exactly this dictionary word highlight function?
Thanks in advance.
I tried various approaches such as creating custom dictionaries and then
redefining the highlighting for Normal and SpellBad words. I also tried
marking the desired words as being “rare” (since Vim uses different
highlighting for rare words) but those ideas didn’t work out. Here's the best
I could come up with:
Define highlighting
First, define how you want the words to be highlighted. In this example, I
want all the numbers from one to ten to be highlighted so I call my group,
“Numbers” and tell Vim that I want these words to appear in red with either
the terminal or the GUI version.
highlight Numbers ctermfg=red guifg=red
Option 1: Syntax
If there are a lot of words, use the syntax command to define the keywords
to be highlighted:
syntax on
syntax keyword Numbers one two three four five six seven eight nine ten One Two Three Four Five Six Seven Eight Nine Ten
Option 2: Match
Note that using syntax option, you need to include different permutations of
upper and lower case. If you don’t want to do that, you could instead use the
match keyword which operates on regular expression patterns rather than a
list of words. Use the \c option to ignore case.
match Numbers /\c\<one\>\|\<two\>\|\<three\>\|\<four\>\|\<five\>\|\<six\>\|\<seven\>\|\<eight\>\|\<nine\>\|\<ten\>/
The drawback to using match is that Vim has to keep evaluating the match
pattern for changes in the text. This becomes computationally expensive if the
regular expression pattern is too long (lots of words). This would cause
Vim to become too slow.

vim : Highlight tab with one color and spaces with another ?

How can I highlight tabs with one color and spaces with another in vim ?
I know how to highlight only tabs or only spaces. And I don't know how to select the colors separately for both spaces and tabs.
A simple solution would be to do something like:
:match Error /\t/
:2match Todo / /
Where Error and Todo are highlight groups from :highlight. This is going to take up two of your three matches and will only be temporary.
Theoretically you could use matchadd() or a combination of highlight groups and :syntax match commands in your .vimrc to make this more permanent but your question doesn't really specify if that's what you want.

Highlight line(s) containing search word

I am aware that it is possible to configure vim to highlight all matches for the searched word.
But, is it possible to highlight the entire line(s) on which the searched for word is found? If so, how?
If you generally want the current line highlighted:
:set cursorline
If you just want the searches highlighted, the only easy way is by extending the search pattern to cover the entire line:
:set hlsearch
:let #/ = '.*'.#/.'.*'
Note that on n / N, the cursor will now jump to the beginning of the line, not the matched word. Also, you won't be able to do :%s//... substitutions of the matched word (without repeating the search pattern) any more.
The exact solution depends probably on your goal:
Do you want to make the matched lines stand out a little more?
Do you want to know the line numbers for further use?
Do you want to act directly on those lines?
You could use the quickfix window to list the lines containing a match:
:vim! foo . | copen
You could use the :global command to list or act on every line containing a match:
:g/foo<CR> " list the lines
:g/foo/<Ex command> " act on each line
:set hlsearch
will highlight the searched-for word.
Note also that you can highlight your current line i.e. the line your cursor is on. So when you move between matches, the complete line you move to will be highlighted.
You may install this plugin highlight.vim. One of the shortcut key <C-h><C-j> allowed you to highlight the line contains the previous search pattern.
Please use this:
To highlight all the search pattern matches in a file set the following option:
:set hlsearch
To disable the highlighting temporarily, use:
:nohlsearch

GVIM. How to change certain words that match the pattern?

In normal mode I press +* and VIM highlighs the occurences of the word under cursor. How to change for example 2,4-5 (second, fourth and fifth) words in search results with %s command?
I know I can use %s and to change the searched word in certain lines, but it is not what I need.
Assuming you did a find first, you could use
:%s//replaced/gic
It will ask for each replacement if it needs to be done.

Resources