6

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.

  • 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. – EvergreenTree Apr 30 '15 at 23:00
10

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.

  • Related: unix.stackexchange.com/questions/11402/… – FDinoff May 1 '15 at 0:43
  • 1
    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. – EvergreenTree May 1 '15 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 '15 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. – EvergreenTree May 1 '15 at 11:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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