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

Background:

Sometimes, when editing in vim, there may be additional characters in the file that the user did not expect to be there, because he was in "insert" mode when he was in a hurry and in a hurry to finish something.

Fortunately, even if the user is in a hurry, pressing ESC several times is always enough to bring them out of the insert mode and into normal mode without surprises.

Question:

Is there a key binding that works the same for insert mode? Pressing "i" may lead you to insert mode, but if you press it several times, you will begin to insert the letter "i" into the file.

Goal:

The goal is to have a key binding to return to insert mode, which the user can even click a few times with his eyes closed and still not worry about the โ€œunexpectednessโ€ of unexpected characters that fit into the file.

+4
source share
3 answers

<Co>i should do the trick. <Co> temporarily goes into normal mode, but only for one command, if this command "goes into insert mode", which is good, you will simply return there.


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

 :nmap <Ci> i :imap <Ci> <Co>i 

If you do not display <Ci> in insert mode, but only in normal mode, then repeatedly pressing <Ci> will be idempotent.

Thanks Benoit, noting that <Ci> inserts a tab in insert mode.

+4
source

You have to do a mapping that behaves differently in different modes:

 :inoremap <F1> <NOP> :nnoremap <F1> i :vnoremap <F1> <esc>i :cnoremap <F1> <CC>i :onoremap <F1> <esc>i 

Press F1 to enter insert mode.

You can also switch the 'insertmode' parameter ( :set insertmode ): in this mode, the insert mode is the default mode (to which you switch from Escape, and you switch to normal mode using CTRL-L.

+4
source

The answer given by the bitmask works, but apparently has the side effect of creating an error message:

E37: There is no record since the last change (add! Override)

If you did not configure vimrc to disable this message.

But another alternative that works without error messages:

 CTRL-C i 

Which seems to work on standard vim.

+2
source

Source: https://habr.com/ru/post/1394194/


All Articles