2

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?

  • 1) are you really using the - 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 at 17:56
  • @statox Oh, I'm sorry. I corrected it, no -. 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 at 18:14
2

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
|improve this answer|||||
  • It works well, but it has the problem that when you go through an empty (or shorter) line and keep scrolling afterwards, you lose the column you were following before going through that line. By default it didn't behave like that, I'd like it to be the same and follow the same column. – urely Jan 15 at 18:22
  • 1
    @urely That's not a mapping problem. That's your setup. You must have set virtualedit=all to allow moving the cursor through a "non-existing" space. – Matt Jan 15 at 18:54
  • 1
    @urely These mappings work correctly for me when passing over/through a short line. When you move to the short line, the cursor moves leftwards to the end of that line. When you subsequently move from it, the cursor moves back rightwards to the column it was in the original line. I believe this is the behaviour you desire. Can you try in 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 at 10:47
  • @Matt A plain i_CTRL-O also seems to work for me. Am I missing something? – Rich Jan 16 at 10:49
  • @Rich Well, it looks to work ok too. So it's just the habit, as <c-\><c-o> is sometimes safer to use in mappings than <c-o> – Matt Jan 16 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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