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?
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?
From the VIM FAQ:
10.2. In insert mode, when I press the <Esc> key to go to command mode, the
cursor moves one character to the left (except when the cursor is on
the first character of the line). Is it possible to change this
behavior to keep the cursor at the same column?
No. It is not possible to change this behavior. The cursor is *always*
positioned on a valid character (unless you have virtual-edit mode
enabled). So, if you are appending text to the end of a line, when you
return to command mode the cursor *must* drop back onto the last character
you typed. For consistency sake, the cursor drops back everywhere, even if
you are in the middle of a line.
You can use the CTRL-O command in insert mode to execute a single ex
command and return back to insert mode without moving the cursor column.
See this vim tip if you'd like to change this behavior in your .vimrc and position to the left only on EOL.
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?
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:
Well, the convention is vim is different than what we are used to. See, for example, how the default paste
works. If you press p
- the yanked text appears AFTER the cursor instead of AT the cursor like we are used to.
And where is the cursor located at the end of the paste
? it's located one character BEFORE the end of the new text! So if you want to continue to write after the text you need to move to the right and then press i
!
You need to just switch to thinking that the actual cursor is one character to the right (or down) than the one you see. That's the vim convention.
If you start using a
/o
to enter insert mode by default instead of i
/O
, it might help you do this. Just transition your thought so a
is the default, and then the paste, and the location of the cursor exiting insert
mode, and many other things will make more sense.
Edit
A few more words to actually answer your question (rather than help you cope with the behaviour)
The most important feature in vim is the consistency of behaviour. The goal is to allow you to know exactly what will happen without even looking at the screen.
So, when you exit insert
mode, where should the cursor be? It can't be after the text, because if the text ends at the end of the line, the cursor can't get there! (can't be after end of line). Same for paste
- if you paste at the end of a line the resulting cursor can't be after the inserted text!.
Because of consistency requirement - if it sometimes can't be after the inserted text, then is should never be there. Otherwise, if you type without looking at the screen, you don't know where the cursor will be - you'll have to look up and check!
Many of the behaviours of vim can be explained like this: why does '^' get you to the first character and not one-before-first? (so that a
would work to insert, like it does with paste
, $
etc.) because if the first char is at the start of the line - you can't go "one before it". And you want to be consistent.
It could be because you are only allowed to move the cursor in the written area for example bar[] should become ba[r] when not in insertion mode, but you are right it doesn't need to happen always