5

I want to color the background of a keyword but also for the whole line where it is found.

The file has entries like this:

\begin{frame}
\frametitle{title}
\framesubtitle{subtile}
\end{frame}
  • Lines with frametitle should be red in background.
  • Lines with framesubtitle should get a light blue background.
  • Lines that contain otherwise the word frame should get a light gray background

My approach did not succed so far, I have put this into .vimrc:

syntax keyword framesubtike framesubtitle
syntax keyword frametitle frametitle
hi frame term=bold ctermbg=1  

What did I miss?

Edit: Thanks to Carpetsmoker's answer this works good enough for now:

syntax region regionone matchgroup=framesubtitlecolorblue start=/^.*framesubtitle.*/ end=/$/ 
syntax region regiontwo matchgroup=framesubtitlecolorred start=/^.*frametitle.*/ end=/$/      
hi framesubtitlecolorblue ctermfg=blue guifg=red
hi framesubtitlecolorred ctermfg=red guifg=red
1

1 Answer 1

6

It's not possible to color complete lines using syntax highlighting.

You can however use signs (:help signs) for highlighting complete lines. This has the drawback that you need to update the signs manually on changes of the buffer.

I wrote a plugin (DynamicSigns) that allows this kind of manipulations and updates the signs on changes.

First you need to define a function that checks the lines. For example like this:

function! HiglightLines(line)
  let line=getline(a:line)
  if line =~ '\\frametitle\>'
      return 'SignLine4'
  elseif line =~ '\\framesubtitle\>'
      return 'SignLine3'
  elseif line =~ '\<frame\>'
      return 'SignLine1'
  else
      return 0
  endif
endfu

Then you call the plugin:

:SignExpression HighlightLines(v:lnum)

This works similar to the 'foldexpr' option and evaluates all lines for the keywords and depending on the word found, it will use one of the several predefined highlighting groups SignLine1-SignLine5 to highlight the lines accordingly. Read the help at :help Signs-Hooks for an explanation and other possibilities. Note: you can change the SignLine1 highlighting groups manually, but the defaults already match with what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.