Search code examples
vimconfigurationconfigconfiguration-filesmacvim

How to set a VimScript to be executed last?


Problem

A setting in my vimrc (set noshowmode) is being overridden by a plugin later in the loading process.

Goal

Have a VimScript file be executed last (or at least after plugins).

What I Know

  • Plugin VimScripts are executed after the vimrc (Source).
  • The after-directory is run close to last and holds user overrides (Source: :h after-directory).
  • Vim's runtimepath determines the order of what is run.

Failed Attempts

  1. I tried appending a VimScript file (containing set noshowmode) to the end of $VIMRUNTIME with set runtimepath=$VIMRUNTIME,~/.vim/nosmd.vim, but this method ended up messing up other plugins (namely vim-airline, which did not load).

  2. I also tried creating the ~/.vim/after directory and putting my VimScript in there, but this had no effect.


Solution

  • Your attempts

    set runtimepath=$VIMRUNTIME,~/.vim/nosmd.vim
    

    That cannot work. 'runtimepath' contains a list of root configuration directories; you cannot directly place script files in there. Instead, point to a directory that contains plugin/yours.vim.

    I also tried creating the ~/.vim/after directory and putting my VimScript in there, but this had no effect.

    You can check with :scriptnames to see whether your script was executed (and at the end of the plugin load sequence!)

    Solutions

    An ounce of prevention is better than any workaround. I would first try to locate the culprit who changes the 'showmode' option; a plugin shouldn't do this (or at least have a configurable option to disable it).

    :verbose set showmode?
    

    might already tell you who changed this setting.


    As @romainl already commented, ~/.vim/after/plugin/myplugin.vim should work for undoing this. If it doesn't try the following autocmd (in your ~/.vimrc) as a last resort:

    autocmd VimEnter * set noshowmode