In my opinion, the cleanest method of doing this is simply to add the command to a file in your .vim/vimfiles
directory:
~/.vim/after/ftplugin/python.vim
or for Windows:
$HOME\vimfiles\after\ftplugin\python.vim
This technique is documented in :help ftplugin-overrule
(list item 3), although the context (changing settings) is slightly different.
It offers the following benefits over the autocommand technique described by kapil.
Less code. You just write the Vimscript you want executed. You don't need to code an autocommand and surround it with augroup
and autocommand!
clear commands.
Less overhead. Vim is already detecting the file type and checking for the existence of the after/ftplugin/filetype
file: why add an additional autocommand that performs the same purpose?
Cleaner .vimrc
. I think it's better to have file-type-specific functionality separated out from my .vimrc
. (Some disagree with this, though: see the disadvantages, below.)
It also has a couple of minor disadvantages:
It only works if you are using Vim's filetype detection. (:help filetype
)
However, this is generally one of the first settings that people enable in their .vimrc, and I'd wager that almost all Vim users already have it switched on.
Some people prefer to keep all their config in their .vimrc
file.