Vim: `set nocompatible` not idempotent - setting produces side effects when already set

Created on 29 Sep 2016  路  4Comments  路  Source: vim/vim

TL;DR

This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment.

As a workaround, either:

  1. Ensure that you set nocompatible (or the equivalent set nocp) only _once_, and _at the top_ of your vimrc.
  2. Don't set it if it's already set:

if &compatible | set nocompatible | endif " Avoid side effects if `nocp` already set

Explanation and bug illustration

From :help compatible (empahsis mine):

This is a special kind of option, because when it's set or reset,
other options are also changed as a side effect. CAREFUL: Setting or
resetting this option can have a lot of unexpected effects
: Mappings
are interpreted in another way, undo behaves differently, etc. If you
set this option in your vimrc file, you should probably put it at the
very start.

Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo:

set nocompatible
set viminfo+=nWatch-my-viminfo-file-location-be-ignored
echom &viminfo
set nocompatible " do side effects even though nocomptible is already set
echom 'After 2nd "set nocompatible":'
echom &viminfo

Output:

'100,<50,s10,h,nWatch-my-viminfo-file-location-be-ignored
After 2nd "set nocompatible":
'100,<50,s10,h

vim --version | head -1

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 05 2016 16:48:20)

See also this question on Vi Stackexchange

Most helpful comment

For future googlers, I found the quote to support @mattn saying that compatible is off if there's a vimrc further down:

When a |vimrc| or |gvimrc| file is found while Vim is starting up,
this option is switched off, and all options that have not been
modified will be set to the Vim defaults.

Thanks for your time on this @mattn and @chrisbra.

In summary, the important part is (emphasis mine):

Setting or resetting this option can have a lot of unexpected effects

All 4 comments

If you put .vimrc, it should be always nocompatible.

See :help 'compatible'

'compatible' 'cp'   boolean (default on, off when a |vimrc| or |gvimrc|
                    file is found, reset in |defaults.vim|)
            global
            {not in Vi}

So you don't need to set or reset compatible option if you have .vimrc.

I'm reading the default on being related to compatible, meaning that nocompatible defaults to off, implying it needs to be set to use iMproved features.

Either way, re-running the side-effects as illustrated is downright weird.

Hi Tom!

On Do, 29 Sep 2016, Tom Hale wrote:

TL;DR

This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment.

As a workaround, either:

  1. Ensure that you set nocompatible (or the equivalent set nocp) only _once_, and _at the top_ of your vimrc.
  2. Don't set it if it's already set:

if &compatible | set nocompatible | endif " Avoid side effects if `nocp` already set

Explanation and bug illustration

From :help compatible (empahsis mine):

This is a special kind of option, because when it's set or reset,
other options are also changed as a side effect. CAREFUL: Setting or
resetting this option can have a lot of unexpected effects
: Mappings
are interpreted in another way, undo behaves differently, etc. If you
set this option in your vimrc file, you should probably put it at the
very start.

It says there:
when it's set or rest ... have a lot of unexpected effects ... , etc

Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo:

So the list of changes is not limited to those mentioned.

set nocompatible
set viminfo+=nWatch-my-viminfo-file-location-be-ignored
echom &viminfo
set nocompatible " do side effects even though nocomptible is already set
echom 'After 2nd "set nocompatible":'
echom &viminfo

It's not a good idea to set/reset compatible in your .vimrc several
times for reasons you have quoted above.

Best,

Christian

Letzte Worte eines Henkers:
"Das Fallbeil klemmt? Kein Problem, ich schau mal nach"

For future googlers, I found the quote to support @mattn saying that compatible is off if there's a vimrc further down:

When a |vimrc| or |gvimrc| file is found while Vim is starting up,
this option is switched off, and all options that have not been
modified will be set to the Vim defaults.

Thanks for your time on this @mattn and @chrisbra.

In summary, the important part is (emphasis mine):

Setting or resetting this option can have a lot of unexpected effects

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skywind3000 picture skywind3000  路  3Comments

dylnmc picture dylnmc  路  4Comments

bjce picture bjce  路  4Comments

Near-Tam picture Near-Tam  路  4Comments

Yggdroot picture Yggdroot  路  3Comments