Per file type configs

4.3
(45)

Occasionally I’d like to have Vim configuration settings vary by file type. There are a number of reasons I do this, and they include:

  • Having spell check on for certain file types like Markdown, text files, or Git commit messages. This is especially useful if you primarily write code in Vim, since spell checking is very intrusive when programming.
  • Using a color scheme or theme for a particular file type.
  • Setting line length and wrapping behavior per file type.
  • Setting key mappings to invoke tests per file type.
  • Configuring plugin behaviors per file type. (I’m talking to you syntastic!)

Fortunately, Vim supports this capability out of the box. By placing a file in the .vim/ftplugin folder named by file type, Vim will pick up file type specific configurations.

For example, I have set spell in the following files: gitcommit.vim, markdown.vim, and text.vim in my ~/.vim/ftplugins folder. This activates spell checking for these file types. Other file types like those for Javascript, Ruby, Python, etc are not spell checked. Spell checking programming languages is annoying and intrusive since variable names and other language keywords are often not valid natural language words. Sure, I could run :set spell to turn on spell checking and :set nospell to turn it off. But all too often I’ve forgotten to turn it on when writing notes or blog post and missed some glaring spelling errors. With file type configs, it’s always on and only for the file types I want it to be.

Similarly, I like to set up keybindings to execute tests, but the test execution mechanism often differs across file types I personally use <F10> to execute a full test suite, and <F9> to execute tests for the current file. I map these to depending on how tests are invoked per file type. For example, in my clojure.vim file I use the following, which invoke tests using Fireplace:

map <F9> :RunTests<CR>
map <F10> :0RunTests<CR>

But in my javascript.vim I use the following, using Dispatch:

map <F9> :Dispatch exec npm test %<CR>
map <F10> :Dispatch exec mocha spec/**<CR>

And in my ruby.vim I use, which also uses Dispatch:

map <F9> :Dispatch<CR>
map <F10> :Dispatch bundle exec rspec spec<CR>

If you’re having trouble making this work, check to ensure filetype plugin is on by running :filetype. It should say something like this:

filetype detection:ON plugin:ON indent:ON

If it does not, add the following to your .vimrc, or just get Sensible.

filetype plugin indent on

Even if you don’t work in multiple languages, you certainly work with different file types and using Vim’s file type configurations is an organized way to enable differing configurations across files.

How useful was this tip?

Average rating 4.3 / 5. Vote count: 45

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

28 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts

1 thought on “Per file type configs”

  1. You should use `map ` in filetype-specific mappings. See `:help map-`. Without it, you’re (re)defining a global mapping, so switching to a new buffer will keep the last one that was defined which could be very surprising.

Leave a reply

Your email address will not be published. Required fields are marked *