I want to replace the default behavior of the arrows in insert mode, so that they behave like gk and gj, I have used:
inoremap <up> <Esc>gka inoremap <down> <Esc>gja
But this doesn't hold the cursor well. What's the right way to do it?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
I assume "doesn't hold the cursor" applies to the corner case when the cursor is at the end of the line and
virtualedit is empty (then
<esc> will move cursor one position backward, to the last existing character). That's often an issue for insert-mode mappings.
There's :h i_ctrl-\_ctrl-o which executes one normal mode command without touching the cursor position.
inoremap <down> <c-\><c-o>gj inoremap <up> <c-\><c-o>gk