Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, why is temporary Normal Mode (Ctrl-O from Insert Mode) executing commands much slower?

Tags:

vim

Usually, when I want to undo changes while editing text in Vim, I have to:

  1. Exit Insert mode with Esc
  2. Press u to undo changes
  3. Enter into Insert mode with various commands (i, 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:

  1. Escape into temporary Normal mode for one command, using Ctrl-O
  2. Press u to undo changes
  3. I am automatically in Insert mode and continue editing.

It 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?

like image 676
Eddo Hintoso Avatar asked Feb 17 '16 10:02

Eddo Hintoso


People also ask

What does Ctrl o do in Vim?

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.

What is O mode in Vim?

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.

What are the two main modes in Vim and what is each used for?

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.

What are the three modes of Vim and how do you switch between the three modes?

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”.


1 Answers

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'.

like image 107
maniacalrobot Avatar answered Nov 15 '22 10:11

maniacalrobot