Vim spell check feature works great in most cases - it is even smart enough to distinguish code from literals and comments in most languages. However I have problems with HTML spell check:

  <div title="text with mistaek #1" data-text="text with mistaek #2">text with mistaek #3</div>

In example above only mistake inside div is highlighted (#3). It would be great to have all relevant attributes checked - at least title and data-*, ideally - configurable list of attributes.

Is it achievable via configuration or VimScript? Plugin will also do, but ideally it should be configurable for different HTML-like files (for example, ASP .NET MVC cshtml files).

up vote 5 down vote accepted

The syntax items within which Vim will highlight spelling mistakes are defined using the @Spell and @NoSpell clusters. See :help spell-syntax (and the rest of the :help spell and :help syntax files) for full details.

The quick and dirty fix to get your desired result is to create a new file in your Vim config directory: .vim/after/syntax/html.vim with the contents:

syn region htmlString contained start=+"+ end=+"+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc,@Spell
syn region htmlString contained start=+'+ end=+'+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc,@Spell

These lines define the syntax highlighting for HTML attributes, and were copied from the html.vim file that is included within Vim's standard syntax files. I then added the @Spell cluster to the contains field in order to enable spell-checking within each syntax item.

In order to apply this only to "title" and "data-*" attributes requires fine-tuning the regular expression used for matching the items, and slightly more extensive editing of the way HTML highlighting works. Here's a solution that works for "title" attributes only:

syn region htmlStringSpell contained start=+title=["']+hs=s+6 end=+["']+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc,@Spell containedin=htmlTag,ScriptTag
hi def link htmlStringSpell String

N.B.

  1. The regular expression now includes the name of the title attribute, and an offset so that this part of the syntax item is not included in the highlighting. (See :help syn-pattern-offset)
  2. The syntax item now has its own name, and therefore needs to (i) be containedin all the syntax items that htmlStrings are contained in via their respective contains settings. (ii) have its own highlighting applied.
  • 1
    Thanks! Updated my answer to add the single-quote lines. (Working on the full fix) – Rich Feb 11 '15 at 10:48

From /usr/share/vim/vim74/syntax/html.vim:

syn region  htmlString   contained start=+"+ end=+"+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc
syn region  htmlString   contained start=+'+ end=+'+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc

To add spelling support, we need to add the @Spell keyword (see :help spell-syntax) like so:

syn region  htmlString   contained start=+"+ end=+"+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc,@Spell
syn region  htmlString   contained start=+'+ end=+'+ contains=htmlSpecialChar,javaScriptExpression,@htmlPreproc,@Spell

You need to put this in ~/.vim/after/syntax/html.vim so it will override the default syntax rules.

Bonus tip:
The first line is for attributes in double-quoted strings (attr="value"), and the second one if for single-quoted strings (attr='value'). You can also override just one of these, so you can choose whether you have spell checking.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.