0

This question is a sort of follow-up to this: https://stackoverflow.com/questions/66466202/how-to-go-back-in-vim-to-last-cursor-position, which asks how to be able to do something like 17j and then 17k to jump back to the current position.

One answer suggested using something like m'17j to jump down and then to go back ''. Is there a way to do a shortcut such that:

NUM <j|k> --> m' NUM <j|k>

A more concrete example:

  • 17j --> m'17j
  • 7k --> m'7k

Or a way to modify the vim jumplist so it includes any multi-line j|k entries?

0
1

These should do...

nnoremap j :<c-u>exe "norm! m'" . v:count1 . "j"<cr>
nnoremap k :<c-u>exe "norm! m'" . v:count1 . "k"<cr>

Breaking it down...

  1. <c-u> : This clears out the line range that will be added when you start a command with a number.
  2. exe : We are using a variable in the command so we have to stringify and pass through :execute. We also need to use :norm for similar reasons.
  3. "norm! m'" : The first static part of the string we submit to :exe: :norm plus mark setting. The ! after :norm ensures we don't use remapped commands.
  4. v:count1 : This special variable holds the number you entered to start the command or "1" if you didn't include one.
  5. "j" or "k" : The last static part for :exe; the up/down command.
  6. <cr> : Submit the whole thing for processing.

Also, the two . are string concatenation operators.

Update: I should note, since the question suggests it's what OP wants, that you can map the above to the original keys j and k. In fact, I updated the mappings to do exactly that.

Use them just like you'd have used the unmapped versions, e.g. 17j, 7k, j, etc. If you use either on the RHS of a mapping, though, you'll probably want, in most cases, to use the noremap type of map commands so you get the native behavior.

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.