3

I have this mapping to move by visible line when wrap is enabled

nnoremap <silent> j gj
nnoremap <silent> k gk

But when I use a count (like 10 j), I expect to move by actual lines

How to make something like this

nnoremap <silent> j (moving_with_repeat ? j : gj)
nnoremap <silent> k (moving_with_repeat ? k : gk)
3
  • @DJMcMayhem Title is incorrect. I didn't want to know how to change mapping, I wanted to know whether I'm using command with count
    – srghma
    Nov 1, 2017 at 21:05
  • FWIW: I prefer DJMcMayhem's suggested title, because it's a bit more specific. I think you were misreading it slightly, it's not suggesting that you want to know how to change the mapping, it's saying you want to create a mapping that changes based on the count.
    – Rich
    Nov 3, 2017 at 9:24
  • 2
    I didn't quite understand the title so I updated with what I think is a clearer version. Hope you agree.
    – B Layer
    Nov 24, 2017 at 12:17

1 Answer 1

10

You can use expression maps and Vim's predefined v:count variable to do this:

nnoremap <expr> j v:count > 1 ? "j" : "gj"
nnoremap <expr> k v:count > 1 ? "k" : "gk"

This checks the count and then performs the desired operation.

Further reading

  • :help :map-expression
  • :help v:count
  • :help expr1
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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