Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why leaving insert mode moves cursor position to left [duplicate]

Tags:

vim

When we are in insert mode in vim:

foo b[a]r

Where [] denotes cursor position

And we go back to normal mode, cursor is going one position left:

foo [b]ar

Is there advantage of this?

like image 625
Sławosz Avatar asked Jul 31 '13 15:07

Sławosz


2 Answers

Your initial "state" is wrong, this is what you get in insert mode:

bar[]

In insert mode, the cursor is between characters while it is on a character in normal mode. When you leave insert mode, the cursor has to be on a character: what character could it be? The one on the left of the insert mode cursor or the one on the right? How is Vim supposed to decide which side is the good one?

One hint would be the command used to enter insert mode: leaving insert mode after i could probably leave the cursor on the left side and a could probably leave it on the right side. But, what would be the point of having the cursor on the character that's on the right of the last character we typed?

Anyway, insert mode is for inserting text exclusively. i<Esc> or a<Esc> make no sense and serve no practical purpose. On the other hand:

Lorem[] dolor sit amet.
Lorem ipsum[] dolor sit amet.
<Esc>
Lorem ipsu[m] dolor sit amet.

makes a lot more sense than:

Lorem[] dolor sit amet.
Lorem ipsum[] dolor sit amet.
<Esc>
Lorem ipsum[ ]dolor sit amet.

Doesn't it?

like image 86
romainl Avatar answered Nov 15 '22 23:11

romainl


I don't see a direct advantage over just typing h.

The observed movement to the left is a side product of a convention in VIM - always leave insert mode "to the left" - which I find very convenient.

Convenient, because there are many ways to enter insert mode and, having done some insertions, I care about how to exit rather how I have entered insert mode.

There are ways to change this behavior, cf. these SE posts:

  • cursor-positioning-when-entering-insert-mode
  • prevent-cursor-from-moving-back-one-character-on-insert-mode-exit
like image 21
Jan Avatar answered Nov 15 '22 21:11

Jan