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?

  • Your post has two questions and they're not very related to each other... Would you please post the second part in a new separate question? When you do, please post more details about your macro, how it's defined, where it's defined (in your vimrc?) It's hard to figure out what's wrong without more context... Thanks!
    – filbranden
    Commented Sep 15, 2020 at 16:43
  • 1
    @filbranden Yup, thanks for the tip!
    – Zim
    Commented Sep 15, 2020 at 16:45
  • Do ask the second question! It's an interesting one... But please do add more context when you do. Cheers!
    – filbranden
    Commented Sep 15, 2020 at 17:22

2 Answers 2


Vim will load filetype-specific commands from an ftplugin. See :help ftplugin.

In short, you can create a file under ~/.vim/ftplugin (or %HOME%\vimfiles\ftplugin on Windows), or better ~/.vim/after/ftplugin (or %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 ftpugin/tex.vim, ftplugin/tex_*.vim and ftplugin/tex/*.vim (from both ~/.vim and ~/.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 ~/.vim/after.

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.

If you want to add auto-detection for new filetypes, see :help new-filetype. The :help plugin-filetype section also has useful information, you might want to take a peek at that one as well.

  • 1
    Awesome, thanks for the comprehensive answer!
    – Zim
    Commented Sep 15, 2020 at 17:20

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., :Vedit ftplugin/tex.vim)
  • to actually put an autocommand on a specific filetype, such as "automatically run :Require on 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>'.

  • 1
    Amazing. After some searching I honestly don't think this information on putting an auto-command on a specific filetype is actually specified anywhere else. Not including the * <buffer> after the autocmd!, as I had been doing up till now, leads to weird results. Commented Mar 25 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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