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 communityI 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?
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
set virtualedit=all
to allow moving the cursor through a "non-existing" space.
– Matt
Jan 15 '20 at 18:54
vim --clean
to check whether it's a conflict with some other part of your config, and if not, could you describe more precisely what it is you're doing and how the behaviour is incorrect?
– Rich
Jan 16 '20 at 10:47
i_CTRL-O
also seems to work for me. Am I missing something?
– Rich
Jan 16 '20 at 10:49
<c-\><c-o>
is sometimes safer to use in mappings than <c-o>
– Matt
Jan 16 '20 at 11:36
-
in your mappings or is that a typo? 2) What do you mean by this doesn't hold the cursor well? What is the actual behavior vs. the expected behavior? 3) Have you read How to debug a mapping it could be useful – statox♦ Jan 15 '20 at 17:56-
. What I mean is that originally when you press up/down in insert mode the cursor moves over a column and when it passes over an empty or shorter line, it positions itself at the end of the column, but when it continues to move and finds a line with enough length, it remains over the same column. – urely Jan 15 '20 at 18:14