30

I'm a C/C++ professional programmer who makes lots of spelling mistakes in comments. I want to configure vim such that the spell-checker only looks for misspelled words within comments. If necessary I'm willing to add special symbols around the comment that vim can look for to know where to check, such as:

 int main(){
     /*<--C_S 
        This is comment line in main function ..
        C_S-->*/
 }

If the plugin can work without the C_S symbols that'd be even better. I want the spell-checker to highlight any spelling mistakes it finds within comments. Does this already exist? Or is it easy to write myself?

Justin Ethier
  • 16,686
  • 9
  • 43
  • 55
Vikas Kumar
  • 409
  • 4
  • 3
  • My problem is exactly the opposite. I have many LaTeX files that contain regular text (without scripting syntax). I also have sections that are commented out with "%" percent sign. Spell with check everything inside the comments, for instance \dosomething will get marked up as mispelled: "dosomething" bad spelling. However, outside of the comment, all my syntax tags are skipped (exactly as I desire) so \dosomething doesn't get flagged as bad: "dosomething" misspelled. I want the reverse to happen. Please don't check my comments. How do I turn it off? – user12711 Jan 29 '22 at 17:01

3 Answers3

28

Good news, this is already part of Vim. Turn on syntax highlighting (:syntax enable) and this should be taken care of automatically with the default syntax files packaged with any reasonably recent vim distribution. See :help spell-syntax for an explanation. The short version is that syntax files can use @Spell and @NoSpell to specify where spell checking should or should not occur.

jw013
  • 50,274
  • 9
  • 137
  • 141
  • 3
    This only applies to buffers whose syntax files explicitly enable spell checking. Most, in my experience, do _not_. Since globally enabling spell checking is not necessarily a safe option, I typically extend [Psirus](https://unix.stackexchange.com/users/14906/psirus)' [helpful answer](https://unix.stackexchange.com/a/31162/117478) with an `augroup` whose body resembles `autocmd FileType md,rst,text,yaml setlocal spell spelllang=en_ca`. See also the _SPELLING_ subsection of [this](https://github.com/leycec/vimrc/blob/github/.vim/conf.d/30-usage.vim). – Cecil Curry Aug 12 '16 at 06:37
17

It already exists. Just type :set spell spelllang=en_us and it underlines spelling mistakes. Adjust the language to your needs. Additional languages can be found here.

Psirus
  • 417
  • 2
  • 7
3

As others said, it already exists. However, the rainbow plugin (default in spf13) will mess the spell check. It took me a while to figure it out.

The problem comes from the following line:

let def_rg = 'syn region %s matchgroup=%s containedin=%s contains=%s,@NoSpell %s'

rainbow incorrectly adds @NoSpell to all rainbow regions.

See https://github.com/luochen1990/rainbow/issues/30 for the details

Hai Feng Kao
  • 153
  • 6
  • 1
    Your answer would be much better if you took the effort to summarise the issue and how to fix it so that when the link dies, the essentials will still be present in your answer. – zagrimsan Aug 02 '16 at 09:17
  • This is the correct answer. There is no easy solution – Coroos May 15 '22 at 08:35