Add filetype plugin indent on at the start of the main ~/.vimrc
Note that filetype plugin indent on
is the default for Neovim; also it's in Vim's defaults.vim
, so if you already have runtime defaults.vim
(or vimrc is missing) you don't need it either. If you're unsure you can check the current setting by typing :filetype
without any other parameters. If it's not yet here then add that line to your vimrc.
To elaborate on what it's really doing:
filetype on
enables :h 'filetype'
auto-detection by catching BufRead
/BufNew
events (within augroup filetypedetect
) and analyzing buffer contents, or more often simply file extension. Note that setting :h 'filetype'
also triggers FileType
event for that buffer.
filetype plugin on
enables filetype-based settings (mostly various buffer-local options) by catching FileType
event (within augroup filetypeplugin
). As FileType
event is normally triggered by auto-detection routine, filetype plugin on
also switches filetype on
to save a few keystrokes.
filetype indent on
is almost the same but it catches FileType
event (within augroup filetypeindent
) to install some custom indent-calculation procedure (typically by :h 'indentexpr'
). As calculating indent on-the-fly is rather sophisticated procedure it was moved into a dedicated subplugin of its own and can be switched on or off independently of ftplugin
thing.
Finally, filetype plugin indent on
is a 3-in-1 command which enables all that ingredients.
Also, you might want to switch syntax highlighting on, so you'll probably need syntax on
too. BTW. Although the syntax stuff has a command of its own, but ultimately it also depends on FileType event, and so syntax on
also switches filetype on
automatically.
Create folders ~/.vim/ftplugin/python.vim and ~/.vim/ftplugin/markdown.vim.
When the code in $VIMRUNTIME/ftplugin.vim
receives FileType
event it doesn't do anything there, but, for maximum flexibility, it sources all appropriate scripts to change the settings. Therefore, the effect depends on the order those scripts are sourced. It's all in :set rtp?
- normally your ~/.vim
precedes $VIMRUNTIME
, which in turn precedes ~/.vim/after
. And so if your settings are in ~/.vim/ftplugin/python.vim
they may be (and will be) overwritten by $VIMRUNTIME/ftplugin/python.vim
(in principle, you can add let b:did_ftplugin=1
and skip all that standard "python" stuff altogether, but usually it's not recommended). For this reason you normally want ~/.vim/after/ftplugin
and not ~/.vim/ftplugin
.
Any mistakes/gotchas in this?
Note that this code will be executed each time any buffer's 'filetype'
is set to "python" (or "markdown"). Hence, you'd better do only buffer-local initialization. A thing like :packadd
is hardly suitable here.
Having said this, almost all plugins have guards against multiple initialization, and so it should not really matter; but, anyway, it looks ugly to me. So I'd rather do:
if !exists('g:loaded_ale')
packadd ale
endif
This should at least save us from extra reading and parsing plugin/ale.vim
. Also note that you misuse the "bang" in :packadd
- read :h :packadd
carefully.
/after/
, for instance, does but not how will that affect the following setup. That answer is probably helpful to experienced users but leave beginners like me kinda confused.