Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight line(s) containing search word

Tags:

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?

like image 445
hmjd Avatar asked Dec 09 '22 20:12

hmjd


2 Answers

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.

like image 65
Ingo Karkat Avatar answered Feb 03 '23 23:02

Ingo Karkat


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
like image 22
romainl Avatar answered Feb 03 '23 21:02

romainl