Vertical Navigation – without Relative Line Numbers

· Nano Tips for Vim

#keymap #motion

While there are plenty of commands for horizontal navigation in vim – hlWwEeBbFfTt^0$ – the options for vertical navigation are far more limited: You have only jkGgg as basic movement.

The only other options you really have are {/} to navigate to the next/previous paragraph, <C-d>/<C-u> to go half a screen down, or enabling relative line numbers (:set relativenumber) to use {count}j/{count}k to navigate {count} lines down/up. However, all these have certain downsides in my view:

My approach is therefore to add a new mapping:

1vim.keymap.set("", "J", "6j")
2vim.keymap.set("", "K", "6k")
3-- shadows the built-in `J` and `K`, which therefore need to be remapped

First, this mapping is actually consistent with vim's logic of making shift + {key} be a modification of what {key} would do alone (in this case: amplification). Second, it is more predictable than {/} and <C-d>/<C-u>, since not dependent on blank lines or screen sizes. Third, if you compare the resulting movements with the equivalent {count}j movements, you will notive that in most cases, this takes you only three keypresses on the home row, which is in my opinion quicker than going to the number row and back. (Note that the 6 has been chosen deliberately – as opposed to 5 or 7, it ensures that all movements up to 8 lines down require at most 3 key presses.)

 1jj   ≙ 2j
 2jjj  ≙ 3j
 3Jkk  ≙ 4j
 4Jk   ≙ 5j
 5J    ≙ 6j
 6Jj   ≙ 7j
 7Jjj  ≙ 8j
 8Jjjj ≙ 9j
 9JJkk ≙ 10j
10JJk  ≙ 11j
11JJ   ≙ 12j
12JJj  ≙ 13j

And lastly, you often do not really require to look at the gutter, since the lines you want to go down/up are either small enough to do without, or if they are large, you can make do with JJJ… for bigger leaps, followed by j or k for the precise positioning. As all keys are on the home row here, I find the occasional extra key presses to be almost negligible. This allows me to completely turn of line numbers, which saves precious screen real estate if you work with many splits, too.