views:

377

answers:

2

I like that vim 7.0 supports spell checking via :set spell, and I like that it by default only checks comments and text strings in my C code. But I wanted to find a way to change the behavior so that vim will know that when I write words containing underscores, I don't want that word spell checked.

The problem is that I often will refer to variable or function names in my comments, and so right now vim thinks that each piece of text that isn't a complete correct word is a spelling error. Eg.

/* The variable proj_abc_ptr is used in function do_func_stuff' */

Most of the time, the pieces seperated by underscores are complete words, but other times they are abbreviations that I would prefer not to add to a word list. Is there any global way to tell vim to include _'s as part of the word when spell checking?

+3  A: 

You'll need to move it into its own group. Something like this:

hi link cCommentUnderscore cComment
syn match cCommentUnderscore display '\k\+_\w\+'
syn cluster cCommentGroup add=cCommentUnderscore

In some highlighters you may need contains=@NoSpell on the end of the match line, but in C, the default is @NoSpell, so it should be fine like that.

Al
That works great! Thanks a lot. One comment that I will add: I originally tried putting this in my .vimrc, but it doesn't do anything there. After looking at the :syn help, I figured out that to get this syntax highlighting change to be used each time the C syntax is loaded, I needed to put these lines into a ~/.vim/after/syntax/c.vim file. After I did that, everything worked nicely.
David
After using this change for a short time, I made two refinements. First, to not spell check words with _'s at the beginning or end of the word. Second, to not cause every word with an _ in it to be colored as per comments. The new line is:"syn match cCommentUnderscore display '_\k\+\|\k\+_\w*' contained"
David
+1  A: 

Here are some more general spell-checking exception rules to put in .vim/after/syntax/{LANG}.vim files:

" Disable spell-checking of bizarre words:
"  - Mixed alpha / numeric
"  - Mixed case (starting upper) / All upper
"  - Mixed case (starting lower)
"  - Contains strange character
syn match spellingException "\<\w*\d[\d\w]*\>"      transparent contained containedin=pythonComment,python.*String contains=@NoSpell
syn match spellingException "\<\(\u\l*\)\{2,}\>"    transparent contained containedin=pythonComment,python.*String contains=@NoSpell
syn match spellingException "\<\(\l\+\u\+\)\+\l*\>" transparent contained containedin=pythonComment,python.*String contains=@NoSpell
syn match spellingException "\S*[/\\_`]\S*"         transparent contained containedin=pythonComment,python.*String contains=@NoSpell

Change pythonComment,python.*String for your language.

  • transparent means that the match inherits its highlighting properties from the containing block (i.e. these rules do not change the way text is displayed).
  • contained prevents these matches from extending past the containing block (the last rule ends with \S* which would likely match past the end of a block)
  • containedin holds a list of existing syntax groups to add these new rules to.
  • contains=@NoSpell overrides any and all inherited groups, thus telling the spellchecker to skip the matched text.
RobM