Why leave insert mode moves the cursor position to the left

When we are in insert mode in vim:

foo b[a]r

Where [] indicates the cursor position

And we will return to normal mode, the cursor will move one position to the left:

foo [b]ar

Does this have any advantages?

+7
vim
source share
5 answers

Your initial “state” is incorrect, this is what you get in insert mode:

 bar[] 

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

One hint would be the command used to enter the insert mode: after leaving insert mode i , may have left the cursor to the left, and a , probably, would have left him on the right side. But what was to have the cursor on the character to the right of the last character that we gained?

In any case, the insert mode is intended solely for inserting text. i<Esc> or a<Esc> have no meaning and do not serve any practical purpose. On the other hand:

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

makes much more sense than:

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

Is not it?

+3
source share

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. except when the cursor is on 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 the vim review, if you want to change this behavior in my .vimrc and position on the left only to EOL.

+5
source share

I do not see a direct advantage over typing h .

The observed movement to the left is a byproduct of convention in VIM - always leave the insert mode “left”, which I find very convenient.

Convenient, because there are many ways to get into insert mode, and making some parts, I care about how to get out, but as I entered the insert mode.

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

  • cursor-positioning-when-entering- insert-mode
  • prevent-cursor-from-moving-back-one-character-on-insert-mode-exit
+2
source share

This may be because you are allowed to move the cursor only in the writing area, such as a bar [] must be ba [r], when it is not in insert mode, but you're right that this is not necessarily always

+2
source share

Well, the vim convention is different from what we're used to. See, for example, how paste works by default. If you press the p - plucked text appears after the cursor instead of AT cursor, as we used to.

Where is the cursor at the end of paste ? he placed one character to the end of the new text! Therefore, if you want to continue writing after the text, you need to go to the right, and then press i !

You just need to switch on the idea that the actual over - one character to the right (or down) than the one that you see. This is a vim convention.

If you start using a / o to enter the default insert mode instead of i / o , it can help you do this. Just get to the idea that a is the default, and then inserting and positioning the cursor exiting insert mode, and much more will make more sense.

Edit

A few words to answer your question (and not help you cope with the behavior)

The most important feature in vim - a sequence of behavior. The goal is to let you know exactly what will happen without even looking at the screen.

So, when you exit insert mode, where should the cursor be? It can not be after the text, because if the text ends at the end of the line, the cursor can not get there! (cannot be after the end of the line). Same for the paste - if you stick to the end of the line, you'll get the cursor can not be inserted after the text!

Because of the requirement of consistency - if it sometimes can not be after the pasted text, then there should never be. Otherwise, if you type without looking at the screen, you do not know where the cursor is - you have to look and check!

Many of vim behavior can be explained as follows: why is "^" brings you to the first character, and not from the first to the first? (so a will work to paste, for example, using paste , $ , etc.), because if the first char is at the beginning of the line - you cannot go “one before this”, and you want to be sequential.

+2
source share

All Articles