3

Vim has a built in capability to spell check files (see :help spell). This spell checking is syntax aware (see :help syn-spell). Unfortunately, I have no experience writing syntax rules for highlighting.

How do I add simple syntax rules to certain files that disable spell checking of certain code regions without changing the existing highlighting? What are basic ideas to approach such problems?

The following example motivates my question:

Often I use markdown-style inline code in comments in the source code. I would like to disable spell checking within the inline code in the comment delimited by the back ticks (even better would be to enable Vim script syntax highlighting there, but I guess this would deserve its own Stack Exchange question). For example, having the word filetype highlighted as a spelling mistake is distracting:

     [... some Vim script ...]
     " plug#begin() automatically runs:
     "   `filetype plugin indent on`
     "   `syntax enable`
     " Those may be reverted with:
     "   `filetype plugin indent off`
     "   `syntax off`
     [... some Vim script ...]
3
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Mar 13 at 21:27
  • Yes, I have. I left some comments below your answer. Thanks for following up.
    – Kokoro
    Mar 14 at 0:27
  • Related: vi.stackexchange.com/questions/38321/…
    – Rich
    Apr 12 at 11:55

1 Answer 1

0

I would enrich the vim syntax with:

syn match vimLineComment /".*`[^`]*`/ contains=@NoSpell

You could achieve that by adding the following line to your vimrc file:

autocmd Syntax vim syn match vimLineComment /".*`[^`]*`/ contains=@NoSpell

Alternatively (as mentioned by @BenKnoble) you could add the following line to the ~/.vim/after/syntax/vim.vim:

syn match vimLineComment /".*`[^`]*`/ contains=@NoSpell
17
  • 1
    You could probably drop the non-autocmd in after/syntax/vim.vim
    – D. Ben Knoble
    Mar 12 at 21:00
  • 1
    Putting this in my after/syntax/vim.vim worked. However, after the inline code the rest of the comment is not properly syntax highlighted. Hence, the suggested "command?" does break existing syntax highlighting. If another inline comment appears on the same line behaviour changes as well. After reading a little bit, including in the syntax/vim.vim, and the manual, a proper solution likely requires to nest the inline code as a syntax item in the syntax items for vim comments. The proposed solution likely matches instead of the syntax item for vim comments.
    – Kokoro
    Mar 13 at 23:56
  • 1
    Or rather the proposed solution has some non-trivial interaction with the syntax items used for vim comments in syntax/vim.vim. Because up to and including the inline code, the highlighting rule from syntax/vim.vim matches. And then stops matching after the inline comment unless another backtick can be found on the same line, and so on. In other words, text after the last backtick on a line does not get matched.
    – Kokoro
    Mar 14 at 0:01
  • 1
    After more reading, it seems to me that a correct solution for my example should be along the line of syn match vimCommentInlineCode /`[^`]*`/ contains=@NoSpell containedin=vimCommentGroup contained. An answer to the question should briefly explain how this would work (it doesn't), what the keywords are that are used, and that the vimCommentGroup is optained from thesyntax/vim.vim file, as this is where inline code is supposed to be contained. But this solution has the same problems as the proposed one. Likely, this is related to the vimCommentGroup itself containg @Spell.
    – Kokoro
    Mar 14 at 0:25
  • 1
    And of course, the solution proposed in the original answer disables spell checking on the whole line, instead of only within the inline code region.
    – Kokoro
    Mar 14 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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