Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Don't move back 1 character when exiting Insert mode

Open verhovsky opened this issue 2 years ago • 6 comments

To replicate, write a sentence like

hello world

then move the cursor to the end of a line and repeatedly type i, Esc, i, Esc (i.e. enter and exit insert mode). The cursor will eventually end up at the beginning of the line instead of staying where it started.

Describe the solution you'd like I would like a config option like

"vim.moveCursorBack": false,

that would keep the cursor where it is when exiting insert mode (default to true).

Describe alternatives you've considered I think I could use NeoVim integration because there's definitely ways of configuring it to behave this way, I think I do it with autocmd InsertLeave * :normal `^. See https://vim.fandom.com/wiki/Prevent_escape_from_moving_the_cursor_one_character_to_the_left#Programmatic_Alternative

verhovsky avatar Mar 10 '22 04:03 verhovsky

This is a niche enough use case that I'm unlikely to support it explicitly (the config is already much more bloated than I'd like). More complete support of vimscript is planned, but it'll be a while until autocmd and such work well enough for that to work.

inoremap <Esc> <Esc>l gets you close - it just moves your cursor to the right on the first column. There may be some clever way of getting around this; I'm not sure.

J-Fields avatar Mar 10 '22 05:03 J-Fields

Evil (vim bindings for Emacs) has a dedicated evil-move-cursor-back config option for this and it's also a somewhat common question on stackoverflow

  • https://unix.stackexchange.com/questions/11402/why-does-esc-move-the-cursor-back-in-vim
  • https://vi.stackexchange.com/questions/3138/cursor-moves-one-character-backwards-on-exiting-insert-mode
  • https://emacs.stackexchange.com/questions/18052/cursor-moves-one-position-left-after-exiting-insert-mode

so it's not that niche. I'm honestly surprised that I'm the first one to ask about this.

verhovsky avatar Mar 10 '22 05:03 verhovsky

Another place where this matters and where it should also have an effect is when pasting text. If you copy some text like abcd and move your cursor to be in the space between the words

hello world
     ^

then press p then press C you will delete the last letter you pasted

hello abc
         ^

So pretty much every time I paste something I have to press l, whereas in Emacs using Evil with the above mentioned option, I don't.

verhovsky avatar Mar 16 '22 10:03 verhovsky

This is long past, but I'd also really appreciate this functionality!

gabrieljreed avatar Dec 01 '23 01:12 gabrieljreed

I just realized about this issue and is driving me crazy

darianmorat avatar Dec 15 '23 17:12 darianmorat

chatgpt$ rewrite the evil-move-cursor-back function from emacs lisp into javascript. 🤣

catesandrew avatar Jun 18 '24 04:06 catesandrew