When I have a very long line in vim it will be shown on the screen as multiple lines, like this:

line no. | content                                           |
100      | this is a very looooooooooooooooooooooooooooooooo | 
         | ooooooooooooooooooooooooooooooooooooooooooooooooo | 
         | ooooooong line!                                   |
101      | this is the next line                             |

when I type k on line 101 I will jump to the first line of line 100 (which is very counter-intuitive for me), is there a way that I could type 3 ks from line 101 to navigate the line 100?

  • 2
    Have you tried gj and gk?
    – D. Ben Knoble
    Dec 3 '19 at 16:43
  • I was pretty sure we have a duplicate for this one but I can't find an exact duplicate. This question is at least very related (and has the same content as Maxim's answer)
    – statox
    Dec 3 '19 at 16:55
  • 1
    @statox I had the same feeling but, while gj and gk appear in a lot of answers, I also couldn't find a question closer than the one you linked.
    – B Layer
    Dec 4 '19 at 2:14
  • 1
    @BLayer I guess we'll keep this one and use it for a future duplicate then :)
    – statox
    Dec 4 '19 at 9:05

You can use the gj and gk commands to move the cursor to the character in the next or previous display lines, even when lines wrap.

See :help gj and :help gk for details.

If you would like to navigate those display lines using j and k, you can remap them in your .vimrc:

nnoremap j gj
nnoremap k gk

Beware that these remappings have side effects, for instance commands such as dj will behave differently even on lines that are not wrapped, since gj is not a linewise motion while j is. (The original dj will delete two lines, while the modified one won't.)

  • This answer has the right ketstrokes... But remapping is quite overkill. Maybe start the answer by simply stating you can use gj and gk to navigate long lines would be best. Follow that with a suggestion to map them if you find that useful, but don't lead with that. Would you please edit the answer to rephrase it that way? (Also, adding links to :help gk and :help gj is useful for someone reading that who's not in front of Vim right now...) Thanks!
    – filbranden
    Dec 4 '19 at 11:08
  • @filbranden, gj and gk are already mentioned in a comment below OP question. This answer is to specific question on how to use k and j to navigate those lines. If you feel it is incorrect and the right way to navigate is to use gj and gk then I don't mind if you edit the answer.
    – Maxim Kim
    Dec 4 '19 at 11:14
  • I mean, I am not that expert to tell ppl this is right vim way and this is overkill and shouldn't be used :)
    – Maxim Kim
    Dec 4 '19 at 11:20
  • Of course it's alright to suggest the mappings, but I think mentioning the normal commands first is best since they might be enough in many cases (as in, I know I'm in a long line, I'll use the alternate command here.) The mappings have side effects, so I think that should be addressed too. I edited the answer, I hope that's OK. If you disagree please revert (or further edit it to clarify.)
    – filbranden
    Dec 4 '19 at 11:28
  • 1
    @filbranden thx for the edit -- it looks really good!
    – Maxim Kim
    Dec 4 '19 at 12:47

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.