Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 808
Paul Biggar Avatar asked Nov 04 '10 13:11

Paul Biggar


People also ask

How do I highlight in vim?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.

How do I enable syntax highlighting in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does vim have syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

What does red highlight mean in Vim?

vimrc was using syntax highlighting and it was a syntax error the red highlight was flagging. Fixing these solved the issue, but if that's not an option :syntax off is your go-to. Follow this answer to receive notifications.


2 Answers

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 
like image 105
Cascabel Avatar answered Sep 28 '22 16:09

Cascabel


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.

like image 40
3 revs, 2 users 56%user5164080 Avatar answered Sep 28 '22 15:09

3 revs, 2 users 56%user5164080