I'm using Vim (with vim-latex) to write my LaTeX documents.

I made a bunch of custom commands to mark different kinds of "variables" (as in programming languages) I use in text, in formulas and in code listings. I always wrap the variable names in my commands to ensure consistent text formatting and have the ability to conveniently change the format at a later point.

Usually a command—e.g. \somevariable{somename}—does not contain an orthographically correct name. The problem is, Vim's spell checker marks all my variable names as wrong and this clutters the spell checking (e.g. when I want to step between errors).

Another example are TikZ pictures where the node names of my graph are marked as spelt wrong.

Is there a (preferably easy) way to exclude the contents (the stuff inside { and }) of custom commands from spell checking?

(I already found this question but this is not exactly what I want as it only deals with standard commands/environments and adding all my custom commands here is quite cumbersome)


You can do it manually by using syn match. For example, your problem with \somevariable can be solved with

syn match texSomevariable "\\somevariable{[^}]\{-}}"hs=s+14,he=e-1 containedin=texStatement contains=@NoSpell

This will disable spellcheking in between { and } after \somevariable. (It assumes that you do not use } inside the argument.)

Depending on how complicated the part of your document in which you do not want the spell checker on, it might get quite complicated to do something similar. Your example with Tikz seems to be dificult, but the solution above should work with somevariable changed to anything else.

EDIT: If you would like to have { and } highlighted as well, as per default, then you should include texDelimiter in contains, so that it becomes contains=@NoSpell,texDelimiter instead of contains=@NoSpell

EDIT: There can be problems if \somevariable matches something special, like \cite. Since \cite is highlighted with texRefZone, this should be added to containedin, so the command should be:

syn match texSomevariable "\\cite{[^}]\{-}}"hs=s+6,he=e-1 containedin=texStatement,texRefZone contains=@NoSpell
  • Tried this "syn match texCiteyear "\\citeyear{[^}]\{-}}"hs=s+14,he=e-1 containedin=texStatement contains=@NoSpell" Does not seem to work. – ablmf Mar 9 '16 at 22:14
  • 1
    @ablmf: No that is right. You should add texRefZone to containedin, so it should look like: \\citeyear{[^}]\{-}}"hs=s+14,he=e-1 containedin=texStatement,texRefZone contains=@NoSpell – Kristian Mar 11 '16 at 14:59
  • 1
    @ablmf: You should probably alter hs=s+14 to hs=s+10 since \citeyear is 4 characters shorter than \somevariable – Kristian Mar 14 '16 at 7:42
  • even thou I changed it to contains=@NoSpell,texDelimiter it still changes the color of my brackets, and it doesn't run from the vimrc ;-( – Thorbjørn E. K. Christensen May 15 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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