2

Does anyone know how to highlight the entire line if there is or if there are matches after doing a search.

p.e. I do a search for /user

Now I want to highlight the entire line if there are matches.

EDIT
I want to use the highlighting as in the search highlighting.
I don't want to use the highlighting groups.

Reman
  • 7,931
  • 11
  • 55
  • 97

3 Answers3

4

An alternative to highlighting the the lines might be using the quickfix list. For example doing following will put all lines matching the pattern /user/ into the quickfix list for the current file (%).

:vimgrep /user/ %

You can display in a separate window the contents of the quickfix list by doing :copen. You can move between matching lines by :cnext, :cprev, and friends. I personally recommend Tim Pope's excellent unimpaired.vim plugin to provide some rather nice and natural feeling mappings like [q and ]q to move through the quickfix list. You can also add a g flag to find multiple matches per line and add them to the quickfix list as well.

You may want to mapping to this vimgrep command to make it a bit faster. I personally use the following in my ~/.vimrc

nnoremap <leader>/ :vimgrep/<c-r>//g %<cr>:copen<cr>

A disadvantage to using :vimgrep command is that it needs a saved file, so unsaved buffers must be saved first. You can overcome this with using a combination of :global and :cgetexpr as shown below.

:cexpr []
:g//caddexpr expand("%").":".line(".").":".getline(".")

However maybe you really do just want to highlight the lines with a match instead of using the quickfix list. The I would suggest using :match like so

:match Search /.*user.*/

You can use whatever highlight group you want. I choose Search as it seemed appropriate. To turn off the highlighting just execute :match with out any arguments.

I personally prefer using :vimgrep and the quickfix list, but your needs may vary from mine.

For more help see:

:h quickfix
:h :vimgrep
:h :cnext
:h :cexpr
:h :caddexpr
:h :match
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • Tnx for the vimgrep info. I use it but it is not what I want. I want just to highlight the lines with a match. ... I once tried `/.*search-item.*` but it doesn't work in all cases p.e. it doesn't work with a lot of regex searches. – Reman Nov 14 '12 at 20:27
2

If you use

:let @/ = '.*\%(' . @/ . '\m\).*'

that should work for most regexp patterns (e.g. the bracketing takes care of \| branches). You could refine that to recognize ^ and $, and magic modifiers like \V.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • thank you Ingo, that seems to work. What do you mean with refine that to recognize ^ and $? Yes I use also a lot of \V and \%V in regexes. Do you think your solution doesn't work with these? – Reman Nov 15 '12 at 07:56
  • 1
    ^ and $ are only special when they appear at the beginning / end (potentially within grouping parentheses), so `.*\(^...` will match a literal `^`, not the start of line any more. You'd have to check `@/` for these and adapt the concatenation. – Ingo Karkat Nov 15 '12 at 08:02
  • `\V` changes the meaning of the trailing `.*`; it would need to be `\.\*` then. I've added a neutralizing `\m` to the answer. – Ingo Karkat Nov 15 '12 at 08:02
  • Thanks. What would be your code if do a search then select a block of text and want to highlight only the entire lines with matches in this block of text? I tried to put `\%V(` before and `\)` after your code but it doesn't work. – Reman Nov 15 '12 at 08:21
  • Uh, that's a different question. You have to `split(text, "\n")`, modify each part like in my answer, and then re-`join(text, "\n")`. – Ingo Karkat Nov 15 '12 at 08:31
  • Split and rejoin? but...you're right, that is a different question. No matter. I'm already happy that your solution seems to work. – Reman Nov 15 '12 at 08:37
1

I don't know if this is acceptable for you:

first you need to define a highlight group: e.g. userline

:highlight userline ctermbg=darkred guibg=darkred

then you could:

:match userline /.*user.*/

all lines containing "user" would be highlighted.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • thank you for replying Kent. No, that is not exactly what I want. I want to highlight the line as the search highlighting does. The reason is that I have a lot of scripts which with I can do operations on the search matches. I'm trying them to apply to the entire line. Hope I made myself clear. (I will add this to my question) – Reman Nov 14 '12 at 17:38