34

I want to "set spell" automatically when i am editing the commit text in git. From the % I see that it is writing to a filename called .git/COMMIT_EDITMSG. How do I update my .vimrc to automatically set spell on when editing that file. something on the lines

if ( filename has a word COMMIT)

set spell

fi

user205315
  • 391
  • 3
  • 5

6 Answers6

36

This line works for me:

autocmd FileType gitcommit setlocal spell
Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
  • Add the suggested line into ~/.vimrc and set Vim as default editor for Git: `git config --global core.editor "vim"` – Noam Manos Apr 05 '22 at 08:26
26

Ordinarily you could do this using an autocmd (au BufNewFile,BufRead COMMIT_EDITMSG setlocal spell) but recent versions of vim already have a filetype assigned for git commit messages, so what you can do instead is create a file ~/.vim/ftplugin/gitcommit.vim and put this in it:

if exists("b:did_ftplugin")
  finish
endif

let b:did_ftplugin = 1 " Don't load twice in one buffer

setlocal spell

and make sure that you have filetype plugin on in your .vimrc. It's a little more work getting going but it makes it easier to add tweaks in the future. :)

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • I missed your answer but figured "autocmd ...." over the weekend. Thanks for the filetype way of doing it. – user205315 Nov 09 '09 at 16:55
  • Could we do this in ~/.vim/after/ftplugin/gitcommit.vim instead? – wik Sep 01 '12 at 18:46
  • The wording of this answer implies that this is preferable over using an autocmd (as shown in @Keith Smiley's answer.) Can someone help me understand why that might be? The one-line autocmd looks significantly simpler. – Jonathan Hartley Feb 01 '23 at 21:40
11
au BufNewFile,BufRead COMMIT_EDITMSG setlocal spell
ephemient
  • 198,619
  • 38
  • 280
  • 391
5

autocmd BufNewFile,BufRead COMMIT_EDITMSG set spell

in ~/.vimrc will do it

user205315
  • 391
  • 3
  • 5
5

A handy way to do this cleanly is with a vim filetype plugin.

This will allow you to place file type dependant configurations/mappings in a seperate file (see my .vim/ftplugin/gitcommit.vim for example)

To do so, create a file at ~/.vim/ftplugin/gitcommit.vim and place your custom configurations there.

Eddie Groves
  • 33,851
  • 14
  • 47
  • 48
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
3

You can add 'set spell' to your .vimrc file to make Vim automatically spell check all documents including your git commit messages. Vim is smart enough to spell check comments and strings while ignoring your source code.

Depending on your colorscheme, this can be annoying though to see variable names in your comments and strings highlighted as misspelled words.

See this stackoverflow question for more details on spell checking.

Community
  • 1
  • 1
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59