Why does `ESC` move the cursor back in vim?

In insert mode, the cursor is between characters, or before the first or after the last character. In normal mode, the cursor is over a character (newlines are not characters for this purpose). This is somewhat unusual: most editors always put the cursor between characters, and have most commands act on the character after (not, strictly speaking, under) the cursor. This is perhaps partly due to the fact that before GUIs, text terminals always showed the cursor on a character (underline or block, perhaps blinking). This abstraction fails in insert mode because that requires one more position (posts vs fences).

Switching between modes has to move the cursor by a half-character, so to speak. The i command moves left, to put the cursor before the character it was over. The a command moves right. Going out of insert mode (by pressing Esc) moves the cursor left if possible (if it's at the beginning of the line, it's moved right instead).

I suppose the Esc behavior sort of makes sense. Often, you're typing at the end of the line, and there Esc can only go left. So the general behavior is the most common behavior.

Think of the character under the cursor as the last interesting character, and of the insert command as a. You can repeat a Esc without moving the cursor, except that you'll be bumped one position right if you start at the beginning of a non-empty line.


Visually, it makes more sense in gvim:

When editing, your cursor is in between the characters:
enter image description here

When in normal mode, it is on top of the last character:
enter image description here

So it does not really go back a character, just from being between r and s to being on r


This behavior is editable as answered here, but stop and think about what's going on for a second. When you are in insert mode, you are not actually over a character but BETWEEN them. When you insert something, the cursor jumps to the end of what you inserted so that the next thing inserted will be after that. Now think about if you just typed a letter, then wanted to do something to it. Hitting Esc would put the select cursor directly over the last character you inserted. If it didn't do this, it would actually be rather awkward.

The situation you are probably thinking of is when you are in insert mode moving around as if you were normal mode and then switch. In that case the cursor does appear to go back one character, but if you think that way it shows that you were in insert mode and the last thing you did was NOT insert. Maybe you should spend more time in normal mode?

Tags:

Vi

Vim