Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim insert mode: unambiguous key binding that always works as expected?

Background:

Sometimes when editing in vim it is possible to have extra characters in a file that the user did not expect to be there because he was in "insert mode" when in a hurry and rushing to get something finished.

Fortunately, even if the user is rushing, pressing ESC a couple of times is always sufficient to get them out of insert mode and into normal mode, with no surprises.

Question:

Is there a key binding that works the same way for insert mode? Pressing "i" can get you into insert mode, but if you press it multiple times, you will start inserting the letter "i" into the file.

Goal:

The goal is to have some key binding for getting back into insert mode that the user can even press multiple times with eyes closed, and still not worry about "surprises" of unexpected characters being put into the file.

like image 613
dreftymac Avatar asked Dec 27 '22 06:12

dreftymac


1 Answers

<C-o>i should do the trick. <C-o> gets you temporarily to normal mode, but for only one command, if that command is "go to insert mode" than well, you simply return there.


Edit: I could reproduce your error message now, and it seems the easiest thing to do is this:

:nmap <C-i> i
:imap <C-i> <C-o>i

If do not map <C-i> in insert mode, but in normal mode only, then repeatedly hitting <C-i> will be idempotent.

Thanks to Benoit for mentioning that <C-i> inserts a tab in insert mode.

like image 117
bitmask Avatar answered Mar 29 '23 23:03

bitmask