Highlight line(s) containing search word - vim

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

Related

Go to search result without having to press enter in vim

I would like for my cursor to automatically go to the search result after I stop typing. For example, I type in /new, after 100ms (or whatever the debounce/delay is), I'd like the cursor to automatically go to the first match (if it exists) rather than me having to constantly hit return with my pinkie, which gets a bit sore having to press return 100 times per day.
Is there a setting to do this in vim, or how could something like this be achieved if there is not? From vim's help for incsearch:
You
still need to finish the search command with <Enter> to move the
cursor to the match.
First, important information:
When you are performing a search and incsearch is set, the current match will be highlighted as the highlight group IncSearch. If you also have hlsearch set, then the other non-current matches on your screen will also be highlighted, instead as the highlight group Search. For example, if you search for thing and the word thing appears three times on screen in your current file, the first match after your cursor will be the 'current match' and highlighted so. The other matches will be highlighted differently.
Now to answer your question
You can move between matches while searching without ending the search by pressing <Enter>. This is done with <C-G> and <C-T> (That's CTRL-G and CTRL-T). Reference :h /_ctrl-g and :h /_ctrl-t. This allows you to move through the file and see all the matches of the pattern you have already typed without actually ending the search. This also allows you to use <C-L> to add characters to the search. I'll let you look that one up (:h /_ctrl-l>).

Vim - count nonblank nonwhitespace lines

Using GVIM, I'd like to have something similar to the line count MSExcel offers - an on-the-fly line count that shows me how many lines I've selected so far that are non-blank (i.e. don't contain only whitespaces).
Up until now I've always used y for yank and then it shows on the bottom how many lines yanked, but:
this is not on-the-fly
this counts also blank/whitespace lines.
What's the best way to achieve this?
The downside of :substitute//n is that is clobbers the last search pattern and search history, and its output contains additional text and is difficult to capture. Alternatively, you can filter() the entire buffer, and count the matches:
:echo len(filter(getline(1, '$'), 'v:val =~# "\\S"'))
This can be easily turned into a custom mapping or command. If the performance is acceptable, you can even add this to your 'statusline':
:let &statusline .= ' %{len(filter(getline(1, "$"), ''v:val =~# "\\S"''))} lines'
Note: The statusline update won't work during a visual selection, because the marks '< and '> are only set after you've left the selection.
:%s/\S//n
3 matches on 3 lines
This combines a no-op :substitute (with the /n flag) that only counts the matching lines with the \S atom, which matches non-whitespace. As long as there is any such in a line, it is counted.
For the visual selection, just trigger it from there; it'll automatically use :'<,'> range instead of :%.
To get the number of blank lines you could use
:%s/^.\+//n
of course, instead of % you can use any other range command.
However, this approach will count only non-blank lines (without whitespace) not starting with a whitespace. Some hints on counting search results can be found here.
To allow for whitespace recognition you could use something like
:%s/^.*[^ ]\+//n

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

Highlight search pattern without moving cursor

In vim, is it possible to highlight a search pattern without moving the cursor?
For example, if I want to find m_depthTable I could do:
/m_depthTable
and that will highlight all instances of m_depthTable, but it will also move to the next occurance.
I want to highlight without moving. Possible?
You could do a substitute command with the n flag. This won't move the cursor or do the substitute.
:s/pattern//n
just
/pattern<enter>
then press ``
You can write directly into register that contains last search pattern:
:let #/="m_depthTable"

How do I change the character Vim uses to number blank lines in a buffer?

Currently, when my window is bigger than the buffer being displayed, blank lines beyond the end of file are shown with the ~ characters in the line number column. I would prefer for the line number column for those lines to be blank. Is it possible to make it so?
As of Vim 8.0, the color of the filler line character (~) can be changed indepedently by configuring the EndOfBuffer highlight group:
highlight EndOfBuffer ctermfg=bg guifg=bg
Unfortunately, it is not possible to change the tilde character that
Vim uses to show the lines beyond the end of a file (without modifying
the source code).
A viable workaround is to hide those tildes by configuring the
NonText highlight group, which is used for displaying them,
so that its foreground color is the same as the background one:
:highlight NonText ctermfg=bg guifg=bg
However, this approach is not a complete solution, because this
highlighting group is also used for rendering the list characters
(see :help 'list' and :help 'listchars'), making it impossible to
specify highlighting just for the beyond-last-line markings.
Starting with version 8 (see :helpg Patch 7.4.2213), Vim allows
to highlight the filler lines after the last line in a buffer using
a separate highlighting group called EndOfBuffer:
:highlight EndOfBuffer ctermfg=bg guifg=bg

Resources