Wikia

Vim Tips Wiki

Changes: Execute accidentally inserted commands

Edit

Back to page

(Remove html character entities)
(Reviewed and added autocmd that makes it more useful. None of the comments added anything worth keeping (and the first was even incorrect))
 
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=637
 
|id=637
Line 20: Line 19:
 
This mapping switches to normal mode, undoes the last insertion and takes it as a command.
 
This mapping switches to normal mode, undoes the last insertion and takes it as a command.
   
Most times I need it, if I typed dd and the line still remains because of Insert-Mode and the unwanted 'dd' ist somewhere in my text.
+
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 [[Recover_from_accidental_Ctrl-U#Explanation|undo sequence]]. Therefore, it only works if the ONLY text you enter is a normal-mode command.
   
==Comments==
+
You can make this mapping more useful by combining it with the following autocmd:
I guess you meant:
 
   
 
<pre>
 
<pre>
inoremap <somekey> <Esc>u:@.
+
autocmd CursorHoldI * call feedkeys("\<C-G>u", 'tn')
 
</pre>
 
</pre>
   
----
+
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.
I prefer
 
   
<pre>
+
==References==
inoremap <somekey> <Esc>u@. i
+
*{{help|@}}
</pre>
+
*{{help|quote.}}
  +
*{{help|CursorHoldI}}
  +
*{{help|feedkeys()}}
  +
*{{help|ins-special-special}}
   
----
+
==Comments==
Also
 
 
<pre>
 
yy@"
 
</pre>
 
 
Explanation:
 
 
<pre>
 
yy yank current line into default register
 
@" execute default recording
 
</pre>
 
 
----
 

Latest revision as of 04:19, May 20, 2009

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