This is actually your terminal doing something weird, not Vim. Terminals have two sets of control sequences associated with cursor keys, for historical reasons: one for full-screen applications, often called “application cursor keys mode”, and one for read-eval-print applications (e.g. shells).
In the old days, read-eval-print applications didn't have any line-editing features, and it was intended that the terminal, or the OS terminal driver, would eventually become more sophisticated. So the terminal sent control sequences intended for the terminal driver. Somehow the Unix terminal drivers never gained decent line-editing features; these were added to applications instead (e.g. through the readline library).
Your terminal is sending ␛OD
for Ctrl+Left in line edition cursor keys mode, and ␛[D
in application cursor keys mode. You have two options:
- Configure your terminal not to make a difference between the two modes. How to do this is entirely dependent on your terminal emulator.
- Live with it. Since any given application always sets the terminal in the same mode, just configure its key bindings according to the mode it uses.
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.
Best Answer
Existing solutions notwithstanding, the conventional solution in Vim is to remap keys in your
.vimrc
configuration. In fact, many Vim users have an easier reachable key remapped to Esc. Popular choices are Ctrl+Enter, or jj, etc.To enable this, just put something like the following into your
.vimrc
and reload it/restart Vim:More information and alternatives