How to remap Enter key in vim?

1

I just got a Kinesis Advantage keyboard and want to remap Enter to Esc in insert mode in vim. I'm using iTerm2 as well.

Here's what I've got so far:

inoremap <CR> <Esc> inoremap jj <Esc> nnoremap <CR> i

On startup, this does not work: Enter enters insert mode, and in insert mode, jj exits insert mode. However, Enter does not exit from insert mode.

Then I reload my vimrc file:

:so $MYVIMRC

And viola, my Enter key now exits insert mode and everything works fine.

Any thoughts on why this would be happening?

Thanks

thekevinscott

Posted 2016-08-19T14:07:49.697

Reputation: 623

It is likely you have something overriding the mapping. What does ":verbose imap <cr>" show? – Heptite – 2016-08-24T23:58:37.200

Aha, that's exactly what's happening. Before reloading, it reads: i <CR> * pumvisible() ? "\<C-N>" : "\<CR>" Last set from ~/vim/bundle/YouCompleteMe/autoload/youcompleteme.vim, and after: i <CR> * <Esc> Last set from ~/.vimrc. Thank you! Want to make an answer and I'll accept it? – thekevinscott – 2016-08-25T13:06:16.113

Answers

2

This happens when you have a plugin or other script sourced after your mapping is defined that overrides your mapping. The easiest way to find out which is to run this command:

:verbose imap <cr>

This will tell you what the key sequence is mapped to, and what script defined the mapping.

Heptite

Posted 2016-08-19T14:07:49.697

Reputation: 16 267