AZAhmed
AZAhmed

Reputation: 376

In vim how do I change the order that scriptnames get sourced?

I noticed that vim executes the global vimrc and my personal ~/.vimrc first then followed by syntax files, then plugin .vim files. All too often this causes some of the customizations a user makes in their .vimrc to be overridden by one of these plugins. I have seen this, for example, in formatoptions, iskeyword, etc.

The question is how do I control the order of execution to guarantee that my customizations are applied last and don't get overriden.

I have seen many people asking about how to find the order of execution (:scriptnames), but no one who gave answers tried to address anything about controlling the order of executing these file, when it's obvious that the people asking the question had their changes overridden by some plugin or such.

Upvotes: 7

Views: 2184

Answers (3)

Peter Rincker
Peter Rincker

Reputation: 45157

Some settings should be sourced after the vimrc file. Filetype specific settings are a prime example of this behavior.

Examples:

  • Python's pep8 indention settings which are found in $VIMRUNTIME/ftplugin/python.vim.
  • iskeyword for lisp like languages should allow - in words and most other languages should not.

Your example settings of 'iskeyword' & 'formatoptions' are both buffer local options which should probably should be set on a 'filetype' by 'filetype' basis.

If you do want to change these settings for a specific 'filetype' you can use the after-directory to add your customizations after ftplugins have been sourced.

Example: if you want to add - to 'iskeyword' for css files, add the following to your ~/.vim/after/ftplugin/css.vim file:

setlocal iskeyword+=-

If a plugin is misbehaving then you can use the after-directory as well to make changes after a plugin has been sourced. e.g. ~/.vim/after/plugin/{plugin}.vim will be sourced after {plugin} has been sourced.

For more help see:

:h after-directory
:h ftplugin
:h :setlocal

Upvotes: 5

weibeld
weibeld

Reputation: 15312

If you just want to make sure that your ~/.vimrc is sourced after all other scripts, then you can start vim like this:

vim -S ~/.vimrc

In this way, your ~/.vimrc will first be sourced before all other scripts, as defined by :scriptnames, but then again after all the other scripts have been sourced. It is the same as sourcing .vimrc with :so ~/.vimrc when vim is already running.

So, this will re-enable all the settings in your .vimrc, no matter which plugins have been loaded before.

Upvotes: 1

phd
phd

Reputation: 94764

Put your plugins into ~/.vim/after — they will be sourced after all others.

Upvotes: 8

Related Questions