Search code examples
vim

Vim: How do I treat wrapped lines as a separate line so that I can navigate to it with j or k?


Currently if my vim tab is small enough it will wrap the text like so. When I press j I want it to go to the line I visually see as the next line, which doesn't have it's own line number.

enter image description here

However, vim just goes to the next line with a line number, so in this case it just goes to line 7 vim.cmd ...

Is there a way to make it treat the wrapped line as a separate line when pressing j or k?


Solution

  • You're looking for the gj, gk, etc. commands, which navigate "display lines" rather than actual file lines.

    I have the following in my vimrc so that the arrow keys navigate by display lines, but j and k continue to navigate file lines:

    nnoremap <Down> gj
    nnoremap <Up> gk
    vnoremap <Down> gj
    vnoremap <Up> gk
    

    But if you want you could remap j and k instead:

    nnoremap j gj
    nnoremap k gk
    vnoremap j gj
    vnoremap k gk