10

Is there a way to have vim not flag HTTP URLs and email addresses as spelling errors? Or, more generally, a way to list regexes of valid spellings?

13

You can add custom syntax rules, and give them the @nospell keyword, this will tell Vim not to apply spell checking to this syntax match. For example:

:syn match UrlNoSpell "\w\+:\/\/[^[:space:]]\+" contains=@NoSpell

The above will work for text files, and some filetypes (such as markdown), but not for all filetypes.

Note that I used a fairly simple regex here; see How do I parse URL's from the text for some alternatives.


For other filetypes, you need to do a bit more work. For example, for python files, comments are contained in a pythonComment group, from /usr/share/vim/vim74/syntax/python.vim:

syn match   pythonComment   "#.*$" contains=pythonTodo,@Spell

To override this, we need to do:

:syn match UrlNoSpellComment "\w\+:\/\/[^[:space:]]\+" contains=@NoSpell containedin=pythonComment
:highlight def link UrlNoSpellComment Comment

The trick is to add a list of previously matched syntax matches in which our custom syntax match may be contained with containedin= This tells Vim to look for the UrlNoSpell regex within the pythonComment matches.

We also need to use highlight to set the correct colours, since these aren't inherited.

You need to do this in a number of places, for example for Python strings:

:syn match UrlNoSpellString "\w\+:\/\/[^[:space:]]\+" contains=@NoSpell containedin=pythonString
:highlight def link UrlNoSpellString String

We need 2 different syntax match groups so we can apply the correct syntax highlighting.

Of course, for other filestypes you need to use other containedin= syntax matches... There is, AFAIK, no "universal" solution, but looking up the correct ones in /usr/share/vim/vim74/syntax/*.vim should not be too difficult.


Note that all of the above commands must be executed after the syntax files; there are 2 ways to do this:

  • From a command or key mapping, this must be invoked manually every time. e.g.

    fun! NoUrlSpell()
        if &filetype == 'python'
            :syn match UrlNoSpellComment "\w\+:\/\/[^[:space:]]\+" contains=@NoSpell containedin=pythonComment
            :highlight def link UrlNoSpellComment Comment
            :syn match UrlNoSpellString "\w\+:\/\/[^[:space:]]\+" contains=@NoSpell containedin=pythonString
            :highlight def link UrlNoSpellString String
        elseif &filetype == 'ruby'
            " ...
        else
            syn match   pythonComment   "#.*$" contains=pythonTodo,@Spell
        endif
    endfun
    command NoUrlSpell :call NoUrlSpell()
    
  • Put the commands in in the ~/.vim/after/syntax/[filetype].vim. Vim will pick up these files and execute them after the default syntax files (see: :help after-directory).

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.