Usually, when I want to undo changes while editing text in Vim, I have to:
Esc
u
to undo changesi
, o
, etc...)Now, I am just a beginner, but I understand that while in Insert mode, you can temporarily escape into Normal mode for one command by using Ctrl-O
.
Knowing this, when I want to undo changes in text, I figured: why not just escape into temporary Normal mode and press u
and not have to deal with all the shenanigans of Esc
and switching between modes?
So I did the following:
Ctrl-O
u
to undo changesIt works... except it's VERY, VERY. slow. The cursor would be immediately placed at the position after the undo is executed, but the changes in the text aren't displayed until after ~2 seconds. That's a very long time.
I thought it was just my computer running slowly for some reason but I did the old-school way and the undo was executed instantly. Granted, this isn't the case for all commands. When I try to dd
and delete the whole line, it works just as expected - instantly.
So my question is: why? Is temporary Normal mode not the same as the Normal mode one would enter from pressing Esc
? Does it have anything to do with buffers, or swap files, or whatever is going on behind the scenes? Is it the way undo
is implemented in the Vim editor?
In insert mode, Ctrl-o escapes user to do one normal-mode command, and then return to the insert mode. The same effect can be achieved by <ESC> ing to normal mode, doing the single command and then entering back to insert mode. Ctrl-i is simply a <Tab> in insert mode.
Insert commands include: i for 'insert', this immediately switches vim to insert mode. a for 'append', this moves the cursor after the current character and enters insert mode. o inserts a new line below the current line and enters insert mode on the new line.
vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.
Changing the modes The most commonly used command to enter in to insert mode is “i”. To shift back to normal mode, press Esc. To switch to the visual mode from Normal mode, different commands are v, V, Shift + v, and Ctrl + v. The most commonly used command to enter in to insert mode is “v”.
It would appear that Vim is waiting for a possible second keypress, i.e., the motion to the command (e.g., dw
, which executes normally). Obviously the u
command doesn't have a motion, but double tapping uu
does execute the undo quickly, it just then inserts the next u into the document. I suspect this is a quirk of how the 'temporary normal mode' decides when to return to 'insert mode'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With