Barebones vim with `nocp`

3.5k views Asked by At

I am trying to load a barebones vim. So I defined a ~/.slimvimrc which has the following contents

syntax on map q :q <cr>

Then I load vim using vim -u ~/.slimvimrc. When I press q I expect vim to close because of the mapping above. However, vim just 'sits' on the command line printing q<CR> and waits for input.

However , if I add nocp setting to the above vimrc :

syntax on set nocp map q :q <cr>

the mapping works fine. I looked up the documentation of nocp, but unable to locate it's relation with <CR>. Can you point me the right place?

2

There are 2 answers

0
romainl On

Your mapping doesn't work in "compatible mode" because < is not recognized as starting a key code.

Your ~/.slimvimrc should look like this:

set nocompatible
syntax on
nnoremap q :q<CR>

Note the specificity of nnoremap:

  • the first n means "normal mode",
  • nore means "non-recursive",

and the removed space between :q and <CR>.

Anyway, q<char> is used to start a recording in register <char>. That feature is awesome! Overriding it with a custom mapping doesn't sound like a very good idea.

Also, you can call Vim with the -N argument to force 'nocompatible':

$ vim -N -u ~/.slimvimrc
0
Ingo Karkat On

The reason is that in vi-compatible mode, Vim does not parse the special keys like <CR>; this is controlled by the 'cpoptions' setting, in particular :help cpo-k and :help cpo-<.

Why is Vim in vi-compatible mode? Usually, Vim sets 'nocompatible' when a ~/.vimrc is found, but your use of -u turns this off. From :help 'compatible':

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. Effectively, this means that when a vimrc or gvimrc file exists, Vim will use the Vim defaults, otherwise it will use the Vi defaults. (Note: This doesn't happen for the system-wide vimrc or gvimrc file, nor for a file given with the -u argument).

The solution is to explicitly set nocompatible at the top of your ~/.slimvimrc, or pass -N in addition to -u.