11

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?

14

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:

  • 4
    Great, thanks! This worked for me: autocmd FileType latex,tex,md,markdown setlocal spell – Sauce McBoss Mar 22 '16 at 20:16
  • I'm glad it was useful to you :-) – statox Mar 22 '16 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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