Wikia

Vim Tips Wiki

Execute accidentally inserted commands

Talk0
1,612pages on
this wiki
Revision as of 04:19, May 20, 2009 by Fritzophrenic (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 637 Printable Monobook Previous Next

created January 17, 2004 · complexity basic · author Jochen Behrens · version 5.7


If you are in insert mode and typed a command for normal mode, you can use it.

inoremap <somekey> <Esc>u@.

This mapping switches to normal mode, undoes the last insertion and takes it as a command.

This can allow you to easily do what you intended when you enter insert mode but change your mind later (and forget you're still in insert mode). Unfortunately, it isn't all that useful by itself, because a text insertion counts as a single operation in the undo sequence. Therefore, it only works if the ONLY text you enter is a normal-mode command.

You can make this mapping more useful by combining it with the following autocmd:

autocmd CursorHoldI * call feedkeys("\<C-G>u", 'tn')

This will break the undo sequence if you leave Vim in insert mode for too long. This means if you accidentally leave Vim in insert mode after a lengthy insertion, the mapping given at the beginning of the tip will only undo and execute the last inserted text.

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki