[Tex/LaTex] In vim: How to enable spellchecking of text only and exclude listings

spellingvim

How can I set up vim in such a manner, that it spellchecks only the text portion of my file?

I write a lot of reports that include code listings, which I would like to exclude from the spellchecking.

Also I am writing in different languages: german and english.
How do I setup vim to do the spell checking properly. Which spell-checking packages are the best (aspell/ispell/…)?

Best Answer

Vim has built in spell checking. You do not need external spell checkers. Simply use :set spell to enable spell checking.

To disable spell checking in code listings, you need to modify the vim syntax file for tex. Copy $VIMRUNTIME/syntax/tex.vim to $HOME/.vim/syntax/tex.vim. Around line 402, this file has:

syn region texZone      start="\\begin{verbatim}"           end="\\end{verbatim}\|%stopzone\>"  contains=@Spell
syn region texZone      start="\\begin{code}"               end="\\end{code}\|%stopzone\>"  contains=@Spell
" listings package:
syn region texZone      start="\\begin{lstlisting}"         end="\\end{lstlisting}\|%stopzone\>"    contains=@Spell
" moreverb package:
syn region texZone      start="\\begin{verbatimtab}"        end="\\end{verbatimtab}\|%stopzone\>"   contains=@Spell
syn region texZone      start="\\begin{verbatimwrite}"      end="\\end{verbatimwrite}\|%stopzone\>" contains=@Spell
syn region texZone      start="\\begin{boxedverbatim}"      end="\\end{boxedverbatim}\|%stopzone\>" contains=@Spell

Change each of these @Spell to @NoSpell and vim will not spell check in the respective environments.