jtfairbank
jtfairbank

Reputation: 2307

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?

Upvotes: 10

Views: 2714

Answers (3)

Steve Doyle
Steve Doyle

Reputation: 21

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=+>+

Upvotes: 2

Aaron Lasseigne
Aaron Lasseigne

Reputation: 205

A possible workaround to global highlighting is to attach the keywords to an existing syntax group. In your .vimrc:

function! HighlightAnnotations()
  syn keyword vimTodo contained HACK OPTIMIZE REVIEW
  syn keyword rubyTodo contained HACK REVIEW
  syn keyword coffeeTodo contained HACK OPTIMIZE REVIEW
  syn keyword javaScriptCommentTodo contained HACK OPTIMIZE REVIEW
endfunction
autocmd Syntax * call HighlightAnnotations()

I'm appending to existing Todo types for languages I commonly use. I'm being lazy by adding it to all file types but you could be more specific if you wanted.

To find an existing group, open a file with the extension you're interested in and run :syn list. Vim will provide a list of all syntax groups currently loaded.

Hopefully this will be a passable solution until you can figure out a way to do this across all file types.

Upvotes: 4

kelloti
kelloti

Reputation: 8951

This recipe contains a script plus instructions to do exactly what you're trying to do.

Upvotes: 2

Related Questions