Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I highlight a word in vim automatically?

I want to highlight a certain word regardless of filetype. In my .vimrc I added:

highlight link notes todo
syntax match notes contained "TODO"
syntax match notes contained "NOTE"
syntax match notes contained "!!!"
syntax match notes contained "???"

Note: I also tried syntax match notes "TODO" (without the contained).

Unfortunately, I can't actually get any of these keywords to be highlighted.

EDIT:

I moved the code to .vim/after/syntax/html.vim in order to get the syntax applied. It currently looks like:

syntax match todo contained "\<\(TODO\|NOTE\|!!!\|???\)"

Now when I type :syntax I get Todo xxx match /\<\(TODO\|NOTE\|!!!\|???\)/ contained

But, when I try to type an html comment with TODO in it (ie <!-- TODO something -->) no highlighting is applied.

Also, would I have to create a .vim/after/syntax/file_type.vim for each file type, or can I apply this generally somehow?

like image 800
jtfairbank Avatar asked Mar 04 '12 19:03

jtfairbank


1 Answers

I'm late to the table with this, but I got it to work by adding these three lines to my html.vim syntax file which I have in $HOME/vimfiles/after/syntax:

syntax keyword htmlTodo contained todo<br>
syntax match htmlComment /<!--.*/ contains=htmlTodo<br>
hi def link htmlTodo Todo

I'm only using "todo", but the list could be expanded by adding more keywords, i.e. syntax keyword htmlTodo contained todo hack test review...

An alternative:
Remove the syntax match ... line and add the following:

syntax clear htmlComment
syntax clear htmlCommentPart
syntax region htmlComment                start=+<!+      end=+>+  contains=htmlCommentPart,htmlCommentError,@Spell,htmlTodo 
syntax region htmlCommentPart  contained start=+--+      end=+--\s*+  contains=@htmlPreProc,@Spell,htmlTodo
syntax region htmlComment                  start=+<!DOCTYPE+ keepend end=+>+
like image 57
Steve Doyle Avatar answered Oct 20 '22 14:10

Steve Doyle