Search code examples
vimcursor-position

How to prevent the cursor from moving back one character on leaving Insert mode in Vim?


Is it possible to cancel the said behavior?

A task for extra credit: Figure out a way to force Vim to refresh the cursor position immediately after exiting Insert mode.


Solution

  • Although I would not recommend changing the default cursor mechanics, one way of achieving the behavior in question is to use the following Insert-mode mapping.

    :inoremap <silent> <Esc> <Esc>`^
    

    Here the Esc key is overloaded in Insert mode to additionally run the `^ command which moves the cursor to the position where it had been the last time Insert mode was left. Since in this mapping it is executed immediately after leaving Insert mode with Esc, the cursor is left one character to the right as compared to its position with default behavior.

    Unlike some other workarounds, this one does not require Vim to be compiled with the +ex_extra feature.