Skip to content

set nocompatible not idempotent - setting produces side effects when already set #1131

@HaleTom

Description

@HaleTom

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions