In vim, how do I highlight TODO: and FIXME:?

VimEditorSyntax HighlightingTodo

Vim Problem Overview


In vim, FIXME and TODO are highlighted, but I can't get FIXME: and TODO: (note the colon after the keyword) to highlight? What should I put in my .vimrc to make this happen?

Vim Solutions


Solution 1 - Vim

Well, you've already found the problem, but here's the why.

There are three basic types of syntax matching: keywords, matches, and regions. Keywords are fixed strings, generally used for basic language keywords (int, double, ...) and also, in your case, for the FIXME and TODO. I really do mean fixed strings; they have to be exact and whole words, unlike matches and regions, which use regex. For example, from the C syntax:

syn keyword	  cTodo   contained    TODO FIXME XXX

It looks like that in pretty much all built-in syntax definitions, just with different group names (cTodo).

iskeyword tells vim whether a given character can be part of keyword. By default, it does not include colons, so when looking for keywords, vim sees "FIXME:" as "FIXME", and ignores the colon. If you tack on the colon (set iskeyword+=:), you can now define an extra bit of highlighting:

syn keyword   myTodo   contained   TODO: FIXME:

It's up to you how you want to work it into the existing syntax/highlight groups. If it's for just one filetype, you could add it to that syntax's todo group (e.g. cTodo). If you want it everywhere, you can do "myTodo" as I suggested, then link it straight to the Todo highlighting group (hi def link myTodo Todo).

Alternatively, you can leave iskeyword alone (I'd probably recommend this), and simply use a match:

syn match   myTodo   contained   "\<\(TODO\|FIXME\):"
hi def link myTodo Todo

Solution 2 - Vim

augroup vimrc_todo
    au!
    au Syntax * syn match MyTodo /\v<(FIXME|NOTE|TODO|OPTIMIZE|XXX):/
          \ containedin=.*Comment,vimCommentTitle
augroup END
hi def link MyTodo Todo

The containedin will add it to all groups ending in "Comment", plus vimCommentTitle, where " TODO: foo would not get highlighted as MyTodo otherwise.

Solution 3 - Vim

If you make your own environment, make syntax file (not .vimrc)

  • global syntax file is located vim directory (ex. /usr/share/vim/vim72/syntax/c.vim)

  • and if you make ~/.vim/syntax/c.vim, then you can add syntax for your own. (override)

Just add additional syntax in that file. (the way @Jefromi does)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul BiggarView Question on Stackoverflow
Solution 1 - VimCascabelView Answer on Stackoverflow
Solution 2 - VimblueyedView Answer on Stackoverflow
Solution 3 - VimMinSung JungView Answer on Stackoverflow