Search code examples
vimspell-checkingvim-pluginneovim

Can we escape types, codes, etc. in comments section so that spell check does not consider them as typo


Vim supports spell-check only in comments section already, however, if I have a type name or something not a regular word, it will consider it as a typo. For instance, in the following example, std::endl will be highlighted as typo.

// Don't use std::endl, it will flush unnecessarily

I wish we could use `` to escape them like following.

// Don't use `std::endl`, it will flush unnecessarily

Is there any tips or solution for this besides adding everything into dictionary?

I really don't want to disable spell-check due to this, so any help is greatly appreciated.

Thank you!


Solution

  • You can use this syntax rule to create a new group matching a `...` block and disable spelling inside those blocks:

    syntax region cCommentNoSpell start=+`+ end=+`+
        \ contained containedin=cComment,cCommentL transparent
        \ contains=@NoSpell
    

    To load this for cpp and c files, add this line (by itself) to a file ~/.vim/after/syntax/c.vim, so it is loaded after the system syntax files for C++ and C. (The cpp syntax rule includes all syntax for c so you'll get it on cpp too.)

    The syntax rule uses ` as both start and ending delimiter.

    It uses contained and containedin to only match inside comments. The cComment rule matches traditional multi-line /* ... */ comments and cCommentL matches single-line // ... comments. (Both are defined in the syntax file for C and C++ shipped with Vim.)

    The transparent attribute instructs it not to use this syntax rule as a highlighting group, so it keeps the normal highlighting for comments in the parts matched by this rule.

    Finally, contains=@NoSpell is what disables spelling on the regions that match this rule. See :help spell-syntax for more details on how spelling works together with syntax highlighting.