Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In vim, when I hit ESC to return to command mode, the cursor moves one character to the left. This is not what I would hope for, occasional I immediately hit l to move back to that spot, perhaps to delete a character.

Is there a reason for this behavior? Is this convenient for a pattern of usage that I'm missing? I am new to vim.

share|improve this question
1  
I've never even noticed this until just now... and I use vim everyday. I have no idea why, but I'm interested in hearing the answers. –  gabe. Apr 15 '11 at 20:16
3  
when going back to insert mode, you can press 'a' for append instead of 'i' for insert. You will then move back to the position you were in before pressing escape. –  penguin359 Apr 18 '11 at 23:39

4 Answers 4

up vote 20 down vote accepted

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.

share|improve this answer

This behavior is editable as answered here, but stop and think about what's going on for a second. When you are in insert mode, you are not actually over a character but BETWEEN them. When you insert something, the cursor jumps to the end of what you inserted so that the next thing inserted will be after that. Now think about if you just typed a letter, then wanted to do something to it. Hitting Esc would put the select cursor directly over the last character you inserted. If it didn't do this, it would actually be rather awkward.

The situation you are probably thinking of is when you are in insert mode moving around as if you were normal mode and then switch. In that case the cursor does appear to go back one character, but if you think that way it shows that you were in insert mode and the last thing you did was NOT insert. Maybe you should spend more time in normal mode?

share|improve this answer
5  
That doesn't make sense to me. How do you explain the walking-backwards behavior of repeated i and ESC keypresses? –  Warren Young Apr 15 '11 at 19:41
4  
Going into insert mode, not inserting anything, then leaving it is not a fair test. Why would you be in insert mode if you didn't insert something. If you do insert something, going back to normal mode leaves you with the character you inserted selected. Very intuitive. –  Caleb Apr 15 '11 at 19:45
1  
It's not about "fair", it's about explanatory power. If your theory of operation doesn't explain all the behavior, either it is incomplete or vi's behavior is inconsistent. I prefer to believe that vi is perfect in every way, and therefore consistent. :) –  Warren Young Apr 15 '11 at 19:54
1  
My "theory" does explain all the behavior, it precisely defines why the "moving backward" behavior you describe is happening and why that is the most consistent way the interface could handle the scenario. –  Caleb Apr 15 '11 at 20:00
5  
@Warren The "walking backwards" behavior of i followed by ESC is a function of i and is fully independent of ESC; specifically, when you hit i you are asking vim to insert a character, which by definition means "insert a character prior to the one I am on", as opposed to a which is "append a character after this one". –  Kromey Apr 15 '11 at 20:14

Visually, it makes more sense in gvim:

When editing, your cursor is in between the characters:

When in normal mode, it is on top of the last character:

So it does not really go back a character, just from being between j and u to being on j

share|improve this answer
    
Great picture. To be honest, I'm in the camp that says the character m should remain highlighted upon returning to normal mode... –  Steven Lu Jun 11 '13 at 21:48
    
Oops. picture links are now dead. –  Steven Lu Feb 8 at 6:27
    
Great. I'll reproduce them and upload them to my server next week. –  queueoverflow Feb 9 at 12:36

Here's my solution.

It's a more terse version of the solution offered on the wikia page about this.

au InsertLeave * call cursor([getpos('.')[1], getpos('.')[2]+1])
share|improve this answer
    
+1 here as well, for citing the wiki page, even though the user wasn't actually asking for a way to change this behavior. –  Kyle Strand Feb 7 at 21:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.