10

I recently have noticed that entering and then exiting insert mode will move your cursor one space to the left. This is a default behavior of vim, since it still occurs with vim -u NONE -U NONE -N. I found this answer and stackoverflow, but it doesn't really explain why this is default behavior. Why is this default behavior, and should I use I use a workaround like his answer suggests?

If I were to use a workaround, it would look something like this:

inoremap <Esc> <Esc>l

Also, I did try set virtualedit=onemore but that just seems to do what it says on its help page and nothing more.

2
  • That is what I thought of when I asked this question. The solution to that would just be to move the cursor back if it is on the end of the line in insert mode, and not every time you exit it. Apr 30, 2015 at 23:00

1 Answer 1

14

First you should understand that the cursor is between characters in insert mode and on a character in normal mode. This means that when you exit insert mode the cursor needs to move onto a character. (This is more noticeable in gvim or MacVim, where the default insert mode cursor is a pipe instead of a block)

If you enter insert mode with a then hitting <esc> will place the cursor back exactly where it was. When vim exits insert mode, it needs to move the cursor back onto some character. However since appending is indistinguishable from inserting, one of them was chosen as a default. In this case moving left was chosen.

If you changed the default so that <esc> went to the character on the right, people would be complaining that a<esc> was moving the cursor to the right.

6
  • Related: unix.stackexchange.com/questions/11402/…
    – FDinoff
    May 1, 2015 at 0:43
  • 2
    Ah, that makes sense. Really the only way of avoiding this behavior is to make it so that if you exit from an insert mode started with i that the cursor moves to the right, and if it is started with a it moves to the left. May 1, 2015 at 0:49
  • @EvergreenTree Correct. You could probably add some mappings that create a buffer local variable that say whether you entered with i or a and then check that variable when you exit insert mode. However I don't know what you would want to happen if you entered insert mode with s or c. I'm probably also missing other ways to enter insert mode.
    – FDinoff
    May 1, 2015 at 3:37
  • that is true. That is the problem with workarounds in vim, there is always some case that you didn't account for. May 1, 2015 at 11:53
  • "cursor is between characters" is an absurd statement basing on a illusion of a vertical bar cursor which you may personally use. Others may see a block cursor even in Insert mode and that proves where the cursor really is. You enter a character at a position where the cursor is and the following text is just moved forward. No text editor supports granularity/resolution up to anything "in between" characters, does it?
    – bloody
    Dec 30, 2021 at 15:53

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.