62

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?

  • 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
  • 5
    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
40

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.

  • Count me among the new-ish vim users who learned "i means you can start typing characters instead of moving around", and didn't know that a is another way to enter insert mode with appreciably different behavior. – Ari Lacenski Nov 4 at 19:47
21

Visually, it makes more sense in gvim:

When editing, your cursor is in between the characters:
enter image description here

When in normal mode, it is on top of the last character:
enter image description here

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

  • 1
    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 '14 at 6:27
  • 1
    image links now dead. – Steven Lu Sep 14 '16 at 14:11
  • 1
    Yeah manually hosting your images is both more work and less reliable than just using the tool in the markdown editor on this site. – Steven Lu Sep 14 '16 at 17:19
  • 1
    Is there everything ok with them again? It's "Cursor" what they have typed in and your text mentions "j" – poige Nov 5 '18 at 14:45
15

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?

  • 15
    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
  • 4
    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
  • 9
    @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
  • 3
    In general, after each change, when I leave that change, I'm done with that change, and the next thing I would like to do will probably involve making a different change. Thus, to me, upon leaving insert mode (no matter how I entered it), I'd generally prefer the cursor to be on the next character so I'm ready for my next change, rather than being ready to change what I just inserted. – Kyle Strand Feb 7 '14 at 21:11
6

Type Alt+L to return to command mode.

It doesn't require any remapping or vim config change. It works because on most terminal emulators Alt+KEY sends an Esc followed by KEY (on xterm you might need to add a Xterm*metaSendsEscape: true line into your ~/.Xdefaults file). That behavior allows you to even "create" other insert mode combinations that work right out the box — like Alt+S to Backspace.

By the way, having the cursor on top of the character you've just written can be very inconvenient. For instance, Escdw won't delete the word following the text you've just inserted.

  • The inconvenience you refer can be avoided by always typing a space in insert mode before the escape. Then the cursor leaving the insert mode is put over the space, and you can type Esc d e to delete the word in front. I reverted back to the default behavior because I felt it changed other behaviors already encoded in my head. – mljrg Oct 23 '18 at 14:42
4

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])
  • +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 '14 at 21:44
  • You will note if you dig far enough into Vim that many plugins do rely on the default (slightly unintuitive) behavior, so if you see some stuff randomly eat or mangle a single character, could be caused by this. I don't use this autocommand myself; it works well, though. – Steven Lu Sep 9 '16 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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