I have a plugin for a specific filetype (
*.tex), and I'm wondering where I can find all the commands which are executed when I type
:e examplefile.tex? In general, is there a standardized practice for where to keep filetype-specific autocommands within a plugin?
Vim will load filetype-specific commands from an
In short, you can create a file under
%HOME%\vimfiles\ftplugin on Windows), or better
%HOME%\vimfiles\after\ftplugin) named after the filetype for which it should be loaded.
For example, when a file is detected with
filetype=tex, Vim will load files
ftplugin/tex/*.vim (from both
~/.vim/after), so you can use a suffix after an
_ or a subdirectory if you want to manage multiple separate files with commands.
Usually, it's better to use the
~/.vim/after/ftplugin directory for your user ftplugins, because this directory is loaded last and the commands in it will run after the filetype-specific scripts from
$VIMRUNTIME have loaded. Usually, that means you can have the final say on the value of options, if you're loading from
Note that ftplugins are loaded based on the filetype and not the extension of the file! There's a process by which Vim autocmds will define what to set the
'filetype' option to, when opening a file. It usually bases the decision on the file extension (or the whole filename in many cases), but it can often include inspecting the contents of the file itself.
For example, a
*.tex file can get
'filetype' set to either
tex (which actually means it's a LaTeX file), or
plaintex (pure TeX). If you typically edit LaTeX only, you might want to add
let g:tex_flavor = "latex" to your vimrc in order to make that the default. See
:help ft-tex-plugin for more details on how the auto-detection works.
A couple of other tips-n-tricks:
- with tpope/scriptease, you can use
:Vedit(and other variants) to fill the location list with all runtimepath entries matching the provided file (e.g.,
- to actually put an autocommand on a specific filetype, such as "automatically run
:Requireon clojure files when they save," you can use a
<buffer>-based autocommand in your ftplugin:
augroup CljRequire " can't use autocmd! here because it would clear the group entirely autocmd! * <buffer> autocmd BufWritePost <buffer> silent! Require augroup END
and the matching
b:undo_ftplugin entry is
'autocmd! CljRequire * <buffer>'.