How can I highlight a word in vim automatically?

I want to highlight a specific word regardless of file type. 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" (no content).

Unfortunately, I really cannot highlight any of these keywords.

EDIT:

I moved the code to .vim/after/syntax/html.vim to apply the syntax. Currently, it looks like this:

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

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

But, when I try to enter an html comment with TODO in it (i.e. <!-- TODO something --> ), highlighting is not applied.

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

+6
source share
3 answers

This recipe contains script plus instructions to accurately accomplish what you are trying to do.

+2
source

A possible workaround for global highlighting is to associate keywords with 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 am adding to existing Todo types for the languages ​​that I usually use. I am lazy, adding it to all types of files, but you can be more specific if you want.

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

I hope this is an acceptable solution until you can find a way to do this in all types of files.

+1
source

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 use only "todo", but the list can be expanded by adding more keywords, i.e. The htmlTodo syntax keyword in the todo hack test review ...

Alternative:
Remove the match ... syntax 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=+>+ 
+1
source

Source: https://habr.com/ru/post/909956/


All Articles