I flip between Ruby and Markdown files a lot. Is there a way that I can have set spell
automatically set when I open a *.md
file, but not when I open a *.rb
one?
-
I found this, but I don't think it works the way I want... ajayfromiiit.wordpress.com/2006/06/02/…– Sauce McBossMar 20, 2016 at 15:48
-
2robots.thoughtbot.com/vim-spell-checking– VanLaserMar 20, 2016 at 16:24
1 Answer
The best way to set an option for a particular filetype is to use autocommands.
Here you could add something like that in your .vimrc
:
autocmd FileType markdown setlocal spell
This line will trigger the command setlocal spell
when the filetype of a buffer is set as markdown. you can also use the autocommand based on the extension of the file you edit with something like:
autocmd BufRead,BufNewFile *.md setlocal spell
For more information you should read :h :autocmd
and :h autocmd-events
.
EDIT Using autocommands is not the best way to do that.
First, if you use the autocommands remember to put them in an augroup
:
augroup markdownSpell
autocmd!
autocmd FileType markdown setlocal spell
autocmd BufRead,BufNewFile *.md setlocal spell
augroup END
This way if you source your vimrc several times the autocommands won't stack and get executed several times.
Now a better solution would be to use a ftplugin. To do that you want to create the file ~/.vim/after/ftplugin/markdown.vim
.
This file will be sourced when the filetype of a buffer is set to markdown
and after the ftplugin
which comes with Vim by default has been sourced (this way you don't loose the already existing settings).
In this file you can simply add:
setlocal spell
Here you need to use setlocal
instead of set
to make sure that the settings will only apply to the current buffer and not to all buffers.
Some interesting related help topics:
-
9Great, thanks! This worked for me:
autocmd FileType latex,tex,md,markdown setlocal spell
Mar 22, 2016 at 20:16 -