How do I make vim syntax highlight a whole line?
Asked Answered
B

3

12

I'd like to have vim highlight entire lines that match certain patterns. I can get all the text in a line to highlight (by doing syn match MyMatch "^.*text-to-match.*$"), but it always stops at the end of the text. I'd like to to continue to the end of the term, like highlighting CursorLine.

I've tried replacing $ with a \n^, hoping that would wrap it around. No change. (I didn't actually expect this would work, but there's no harm in trying.) I also tried adjusting the syn-pattern-offset (which I read about here: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-pattern). Long story short, adding he=he-5 will highlight 5 fewer characters, but he=he+5 doesn't show any extra characters because there aren't characters to highlight.

This is my first attempt at making a vim syntax and I'm relatively new to vim. Please be gentle and include explanations.

Thanks!

(edit: Forgot to include, this is a multiline highlight. That probably increases the complexity a bit.)

Bono answered 27/1, 2010 at 21:2 Comment(5)
Is your pattern supposed to read ^.*text-to-match.*$?Renell
Jefromi, it is. SO apparently uses wrapped asterisks to do italics. It's fixed now.Bono
No other syntaxes, but it's entirely possible the rest of my syntax is bludgeoning itself. Maybe I should start from the lines I want to fully highlight and then write the rest once that's working?Bono
Oh, I totally read "whole line" as "whole line of text". I suspect there's in fact no way to do this.Renell
was looking for the same thing, and actually it works for me by leaving out the ^ and $Atropos
F
10

From the documentation on syn-pattern:

The highlighted area will never be outside of the matched text.

I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.

Fermanagh answered 27/1, 2010 at 21:36 Comment(4)
Yes, but the pattern is matching the entire line. It should highlight the entire line.Renell
If the window is 80 characters, and the text is 4 characters, only the 4 characters are matched and only the 4 characters are highlighted. The OP wants all 80 columns highlighted.Fermanagh
Jay's correct. There's no way to mimic the behavior of the cursorline option with standard syntax highlighting. Syntax highlighting is simply meant to highlight existing text.Bohr
That's unfortunate, but also explains why I wasn't able to figure it out :-PBono
E
15

It's not very adaptive as the filename (buffer) and line to full row highlight needs to be explicitly identified, but apparently the sign command can be used:

It is possible to highlight an entire line using the :sign mechanism.
An example can be found at :help sign-commands
In a nutshell:

:sign define wholeline linehl=ErrorMsg
:sign place 1 name=wholeline line=123 file=thisfile.txt

Obviously, you should pick a higlight group that changes the color of the background for the linehl argument.

source: Erik Falor, vim mailing list

Endearment answered 12/12, 2010 at 10:40 Comment(1)
You can background-highlight the whole line this way but the text will lose its syntax highlighting.Lizettelizotte
F
10

From the documentation on syn-pattern:

The highlighted area will never be outside of the matched text.

I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.

Fermanagh answered 27/1, 2010 at 21:36 Comment(4)
Yes, but the pattern is matching the entire line. It should highlight the entire line.Renell
If the window is 80 characters, and the text is 4 characters, only the 4 characters are matched and only the 4 characters are highlighted. The OP wants all 80 columns highlighted.Fermanagh
Jay's correct. There's no way to mimic the behavior of the cursorline option with standard syntax highlighting. Syntax highlighting is simply meant to highlight existing text.Bohr
That's unfortunate, but also explains why I wasn't able to figure it out :-PBono
L
0

could also try :set cursorline :set cursorcolumn change colors like this: :hi cursorline :hi cursorcolumn using the usual term=, ctermfg=, ctermbg= etc see this answer VIM Highlight the whole current line

Lieb answered 5/7, 2018 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.