nfarrar
nfarrar

Reputation: 2381

Vimrc Command Ordering

I'm curious about the ordering and loading of vimrc commands. I've been searching the vim documentation for days now trying to find and answer and have yet to find what I'm looking for. The most relevant pages I've found are:

To be a little more explicit:
It's obvious that the lines of a vimrc file are executed and evaluated sequentially by the order in which they appear in a vimrc file when executing vim, but it's less obvious to me in some cases what the relationships are between settings and what order they should occur in (or even if it matters). I have been unable to find a page in the user manual that has additional information about this. Is there one that I'm missing?

I'm hoping that there may be a general rule that applies here, but as I think through it I think it's probably more likely that the answers are on a case-by-case basis.

The implication of vimrc command ordering is very obvious in some cases:

However in many cases, I don't understand if the vimrc command ordering is important and what affects it might have:

To Summarize:

Thanks!

Upvotes: 2

Views: 1001

Answers (1)

romainl
romainl

Reputation: 196789

Plugins typically provide default settings, so it's safer to configure your settings after they have been registered & loaded.

Plugins are sourced after your ~/.vimrc. Where you put your let g:plugin_option = 1 lines doesn't matter from a technical point of view as long as Vim sees them before the corresponding plugins are sourced.

It's common practice to set filetype plugin indent on directly after your plugin manager has finished registering it's plugins and before any other commands appear, but is this necessary?

The entire point of that practice is only to activate filetype detection and filetype-specific plugins and indent scripts. It is usually done near the top because it is a basic and very useful setting. It's simply pushed down by the plugin manager stuff, no more no less.

What happens if you create these autocommands prior to enabling filetype detection, filetype plugins, and filetype indentation?

The autocommand is a passive event listener: no event, no execution. You can define it before or after filetype detection, it doesn't matter.

scriptencoding utf-8 specifies the character encoding of the current script and I commonly see this specified at the start of a script. I don't see any information in the usermanual that specifies if the location of this command is important or what impact it might have.

Before that command is executed, the script is sourced with the default encoding, after that command is executed, the rest of the script is executed with the new encoding. The earlier the better.

:h initialization specifies that on startup, one of the first things vim does is check for the values of $SHELL & $TERM and sets the corresponding vim term & shell variables. I typically put my encoding & terminal settings directly after my filetype plugin indent on because I've seen other people do it and it seems like it may impact other settings, but honestly I've no clue and I can't find docs to shed light.

set shell… is generally useful but there's no reason whatsoever to redefine your TERM in your ~/.vimrc. Do that in your shell init files or directly in your terminal emulator. Neither 'shell' nor 'term' have any impact on other Vim options.

Settings like backup and backupdir. The order seems unimportant - I can set backupdir and if backup is not set, then the setting is simply ignored. I can enable backup before or after I set the backupdir value and things work as expected. I'm unsure if that's relevant information for figuring out the larger question.

Yes, the order of options generally doesn't matter.

Is there a general rule for determining the order in which commands should appear in a vimrc file? Is there documentation on this subject that I have not found in the vim user manual somewhere?

There is no general rule AFAIK. The specifics of every option/command is usually clearly explained in the documentation and you are supposed to use common sense:

  • source a function/command before calling it
  • group related lines together
  • use only longform option/command names

Upvotes: 4

Related Questions