14

I'd like to configure Vim 8.2 on Ubuntu 18 so that when I open code in different programming languages, different settings load. Eg. indentation, color-theme, plugins etc.

I found ftplugin to be the recommended way to achieve this setup. Let's assume I want to setup VIM for python and markdown. I've installed the plugins (NERDTree, ale etc.) using VIM's native package manager. I've put some plugins like NERDTree in the /start folder while some like ALE in the /opt folder. This is because I want NERDTree to always load when I open any file using VIM while I want to conditionally load ALE when I only open a python file (and not markdown).

From what I've read in other answers and blogposts, we need to do the following -

  1. Add filetype plugin indent on at the start of the main ~/.vimrc.
  2. Create folders ~/.vim/ftplugin/python.vim and ~/.vim/ftplugin/markdown.vim.
"file => ~/.vim/ftplugin/python.vim
setlocal number
setlocal autoindent
setlocal shiftwidth=4
packadd! ale
"file => ~/.vim/ftplugin/markdown.vim
setlocal number
setlocal autoindent
setlocal shiftwidth=2

What will be the difference between adding ftplugin in the /after directory ie. ~/.vim/after/ftplugin/markdown.vim as opposed to ~/.vim/ftplugin/markdown.vim?

1 Answer 1

14

You are looking for :h ftplugin-overrule:

If a global filetype plugin does not do exactly what you want, there are three
ways to change this:

1. Add a few settings.
   You must create a new filetype plugin in a directory early in
   'runtimepath'.  For Unix, for example you could use this file:

    vim ~/.vim/ftplugin/fortran.vim

  You can set those settings and mappings that you would like to add.  Note
   that the global plugin will be loaded after this, it may overrule the
   settings that you do here.  If this is the case, you need to use one of the
   following two methods.

2. Make a copy of the plugin and change it.
   You must put the copy in a directory early in 'runtimepath'.  For Unix, for
   example, you could do this:

    cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim

   Then you can edit the copied file to your liking.  Since the b:did_ftplugin
   variable will be set, the global plugin will not be loaded.
   A disadvantage of this method is that when the distributed plugin gets
   improved, you will have to copy and modify it again.

3. Overrule the settings after loading the global plugin.
   You must create a new filetype plugin in a directory from the end of
   'runtimepath'.  For Unix, for example, you could use this file:

    vim ~/.vim/after/ftplugin/fortran.vim

   In this file you can change just those settings that you want to change.

Basically vim is bundled with a default ftplugin for python, it is in $VIMRUNTIME/ftplugin/python.vim.

If you create ~/.vim/ftplugin/python.vim the ftplugin bundled with vim will be sourced after your own ftplugin potentially overriding it.

If you create ~/.vim/after/ftplugin/python.vim it will be sourced after $VIMRUNTIME/ftplugin/python.vim making sure that your changes are the one applied.

2
  • 1
    Thank you! It's a lot clearer now. So, /after is the way to go. Commented Jun 23, 2020 at 13:35
  • @theairbend3r most of the time, yes it is :)
    – statox
    Commented Jun 23, 2020 at 13:45

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.