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...
<c-u>
: This clears out the line range that will be added when you start a command with a number.
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.
"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.
v:count1
: This special variable holds the number you entered to start the command or "1" if you didn't include one.
"j"
or "k"
: The last static part for :exe
; the up/down command.
<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.