1

I have the following line in my .vimrc:

inoremap kj <Esc>

It had been working fine, ie it returned me to normal mode. But now it inserts the string "< Esc>" (I use the initial space here just so my post displays properly) into the file at the cursor and I remain in insert mode.

My full .vimrc is as follows

syntax on
set smartindent
set shiftwidth=4
set expandtab
set tabstop=4
inoremap kj <Esc>
nnoremap zz :update<cr>
nnoremap qq :wq<cr>
nnoremap mm :vsplit<cr><C-w><C-w>
" Ctrl-j/k deletes blank line below/above, and Alt-j/k inserts."
nnoremap <silent><C-j> m`:silent +g/\m^\s*$/d<CR>``:noh<CR>
nnoremap <silent><C-k> m`:silent -g/\m^\s*$/d<CR>``:noh<CR>
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
nnoremap <Enter> i<Enter><Esc>
nnoremap <S-Enter> o<Esc>
nnoremap <S-tab> i<tab><Esc>l
nnoremap <C-x> ^i#<Esc>
nnoremap <C-d> yy^i#<Esc>p

Why has this started happening and how can I fix it?

EDIT: output of the following commands

:set compatible?
nocompatible

:set cpoptions?
cpoptions=aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZ$!%*-+<>;

EDIT2: I'm running Arch Linux, 4.19.56-1-lts, and the latest version of Vim. Output of "vim --version":

VIM - Vi IMproved 8.1 (2018 May 18, compiled Jul  4 2019 23:02:48)
Included patches: 1-1635
Compiled by Arch Linux
Huge version without GUI.  Features included (+) or not (-):
+acl               -farsi             -mouse_sysmouse    -tag_any_white
+arabic            +file_in_path      +mouse_urxvt       +tcl/dyn
+autocmd           +find_in_path      +mouse_xterm       +termguicolors
+autochdir         +float             +multi_byte        +terminal
-autoservername    +folding           +multi_lang        +terminfo
-balloon_eval      -footer            -mzscheme          +termresponse
+balloon_eval_term +fork()            +netbeans_intg     +textobjects
-browse            +gettext           +num64             +textprop
++builtin_terms    -hangul_input      +packages          +timers
+byte_offset       +iconv             +path_extra        +title
+channel           +insert_expand     +perl/dyn          -toolbar
+cindent           +job               +persistent_undo   +user_commands
-clientserver      +jumplist          +postscript        +vartabs
-clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +virtualedit
+cmdline_hist      +langmap           +python/dyn        +visual
+cmdline_info      +libcall           +python3/dyn       +visualextra
+comments          +linebreak         +quickfix          +viminfo
+conceal           +lispindent        +reltime           +vreplace
+cryptv            +listcmds          +rightleft         +wildignore
+cscope            +localmap          +ruby/dyn          +wildmenu
+cursorbind        +lua/dyn           +scrollbind        +windows
+cursorshape       +menu              +signs             +writebackup
+dialog_con        +mksession         +smartindent       -X11
+diff              +modify_fname      -sound             -xfontset
+digraphs          +mouse             +spell             -xim
-dnd               -mouseshape        +startuptime       -xpm
-ebcdic            +mouse_dec         +statusline        -xsmp
+emacs_tags        +mouse_gpm         -sun_workshop      -xterm_clipboard
+eval              -mouse_jsbterm     +syntax            -xterm_save
+ex_extra          +mouse_netterm     +tag_binary        
+extra_search      +mouse_sgr         -tag_old_static    
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -D_FORTIFY_SOURCE=2  -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: gcc   -L. -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.30/core_perl/CORE  -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -L/usr/local/lib -Wl,--as-needed -o vim        -lm -ltinfo -lelf -lnsl    -lacl -lattr -lgpm -ldl   -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.30/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/perl5/5.30/core_perl/CORE -lperl -lpthread -ldl -lm -lcrypt -lutil -lc   -L/usr/lib -ltclstub8.6 -ldl -lz -lpthread -lm 
  • 1
    Is it normal that you mention the line inoremap kj <Esc> but it doesn't appear in your vimrc? (There is an equivalent line inoremap jj <Esc>). – statox Jul 23 at 15:42
  • Please post output of the following commands: :set compatible? and :set cpoptions. (Edit the question to include the output.) Thanks! – filbranden Jul 23 at 16:50
  • 1
    sorry, I changed the shortcut to jj to see if that would make a difference and forgot to change back, fixed the typo in my post now – ludog Jul 23 at 17:03
  • also please check :verbose set cpoptions? – Christian Brabandt Jul 24 at 8:59
  • Ah this looks interesting. :verbose set cpoptions? shows Last set from /usr/share/vim/vim81/syntax/vim.vim line 989, (in addition to the list of characters above). The offending line in the listed file reads let &cpo = s:keepcpo So maybe this means that, when compatible mode is later switched off because of my vimrc, the cpoptions don't change to the Vim options as they normally would, but instead remain as the Vi options? – ludog Jul 24 at 10:06
1

TL;DR: You're in compatible mode, add this to the top of your .vimrc:

set nocompatible

Key specifiers using the <...> syntax are incompatible with the traditional vi and are specific to Vim.

This particular feature can be enabled by removing < from the ['cpoptions'] setting, which controls compatibility options. The < option is documented as:

Disable the recognition of special key codes in <> form in mappings, abbreviations, and the "to" part of menu commands. For example, the command :map X <Tab> results in X being mapped to:

  • < included: <Tab> (5 characters)
  • < excluded: ^I (^I is a real )

When in compatible mode, all options in 'cpoptions' are set, while only a handful (and not including <) are set in nocompatible mode.

So ensuring you're in nocompatible mode is the first step in making sure these mappings are going to work for you.

  • 2
    I'm not convinced: :h 'compatible' says off when a vimrc or gvimrc file is found so putting set nocompatible in your .vimrc should not change vim's behavior and since OP's vimrc doesn't contain set compatible I would be curious to understand how they ended up in compatible mode. – statox Jul 23 at 16:44
  • 1
    Thanks, this worked. My shortcuts now behave as expected again. However, based on the response to :set compatible? it seems I wasn't in compatible mode, but that somehow the <> characters were still in the list of compatible options. Is that right? – ludog Jul 23 at 17:21
  • Having < in 'cpoptions' is the behavior you don't want... Makes the <> keys become literal symbols, instead of specifications for special keys. – filbranden Jul 23 at 17:27
  • 1
    ludog also please accept the answer, so this can indicate it solved your issue. This might help others who are looking for the same issue in the future. – filbranden Jul 23 at 17:30
  • 1
    Yes I understand it was the undesirable behaviour. The reason I haven't accepted this answer yet is that it says I was in compatible mode and I wasn't sure that is right. Doesn't the output from set compatible? that I have put in the EDIT to my post indicate that I wasn't in compatible mode? – ludog Jul 23 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.