In vim, prevent caret moving back when I leave edit mode

vim

In vim, if I enter and leave edit mode without doing anything, the caret ends up one character to the left. And if I enter and leave append mode, the caret moves forwards and then backwards.

Any way to configure vim to leave the caret alone in these cases?

Ideally I just want to always enter append mode, but without moving the caret when I enter or exit the mode.

(Currently I usually use insert mode because it doesn't mess up my caret position upon entry. That is, except when I need to append to the end of the line, in which case I swear at vim for behaving in such an archaic fashion, press Esc and enter append mode.)

Best Answer

This question gets asked frequently among new vi/Vim users, and the answer is that while in normal mode, the cursor is always "on" a character, but in insert mode the cursor is always "between" two characters (remember, the end-of-line is a character). You can't really see this illustrated as well in console Vim, but in the GUI you'll notice the cursor becomes a bar between two characters when you enter insert mode, instead of a block over a character when you are in normal mode.

So what you're seeing is not necessarily the cursor moving one character back when you leave insert mode, but merely moving onto a character. The only safe direction of movement is to the left (or back). Thus, you have more than one way of entering insert mode:

  • "a" enters insert mode with the cursor "between" the character the cursor was on and the next character to the right.
  • "i" enters insert mode with the cursor "between" the character the cursor was on and the previous character to the left.

Some people have made efforts to suppress this "movement" that they don't like, but it inevitably interferes with plugins and other Vim scripts they want to run in the future.

My suggestion is to get used to using the "a" and "i" (and "A" and "I") commands in the appropriate circumstances.

The "o" and "O" commands are also useful to learn. See:

:help a
:help i
:help A
:help I
:help o
:help O

Edit: If you're still determined to change this behavior, try this tip: Prevent escape from moving the cursor one character to the left

Related Question