loading...

Using Vim Wrong - Relative Line Numbers

ug02fast profile image Arthur Zhuk ・2 min read

Learning Vim starts off as a daunting task. Setting up your colors, tab key to spaces, space count, configuring your linter, and on and on to infinity. These are all important steps in your journey to learning vim. Just make sure you don't overlook Relative Line Numbers.

The relative line numbers setting allows you to jump exactly where you want without having to do a mental addition or subtraction. The strategy is to look at the line number column, jump to the line you want to edit using a command like 10k, jump to the part of the line you want to change, and perform the edit.

relative

This defeats having to look at the line number column and for example, calculate the difference between 16 and 9 in your head then execute the 7k command to jump there. For those who aren't using Vim, it completely blows bashing your arrow keys like you're playing snake to get the location you want to edit.

arrows

The beautiful thing with learning more Vim mechanics is it compounds with the rest of the efficiencies you pick up throughout your programming journey. As it becomes deeper ingrained in your muscle memory you are able to chop up your code in every which way with ease. You experience less fatigue from typing and have more energy to organize and refactor your rough draft implementations.

snake

Do you use relative line numbers when you use Vim? Do you know of a more effective way to write code that allows for more lines of code written for the effort you exert? Let me know!

Discussion

pic
Editor guide
Collapse
vonheikemen profile image
Heiker

I don't use line numbers anymore. To move around in the same screen I use sneak.vim in "label-mode".

sneak demo

In here I press s and then look for the ar and move to the one I want.

Collapse
ug02fast profile image
Arthur Zhuk Author

This looks good. Have you noticed if you're making fewer keystrokes overall to get your cursor where you want to be more consistently? Strong discipline using sneak could be better than relative line number jumping. I need to try it for a week at least and see how it plays out.

Collapse
vonheikemen profile image
Heiker

Yes, when I use it really does save keystrokes. I find it particularly useful when I randomly notice a typo but it is really far away from where my cursor is, what I do is press s, search the term (that's just two keys), fix the typo, then I press ctrl + o to move back where I was, and I just keep working. Everything happens fast and I don't lose my train of thought.

I do sometimes find myself "playing snake" to move around... but that's just because it's very hard to "unlearn" that habit.

Collapse
mainendra profile image
Mainendra

If you are using neovim and coc, I would suggest github.com/neoclide/coc-smartf

Collapse
ug02fast profile image
Arthur Zhuk Author

This has its advantages. I'm using coc outside of work environment.

Collapse
moopet profile image
Ben Sinclair

I do use relative line numbers in my active window, because I've been trying to get used to them, but it's not working that well for me.

If it's a couple of lines, like less than 10, I'm find just eyeballing it. If it's more than that I'll either use some combination of H M and L to get close, then eyeball it, or I'll use the actual line number like d107G which is more keypresses but is just as targetted as the relative numbers.

To be honest, my brain kind of gets upset with them changing as I move around!

Collapse
ug02fast profile image
Arthur Zhuk Author

I feel the mental overhead of deciding between eyeballing the movement and using relative line numbers in those scenarios as well. Pay attention to what you do when you're in flow. That might be the most effective for you. Feels like any movement ~+/- 5 lines can be easily eyeballed.

Collapse
waylonwalker profile image
Waylon Walker

♥ relative line numbers. A big part of it is the language of yank 4 lines, or delete up 10 lines to move code around.

I don't worry too much about the keystrokes as I do about the mental overhead. I like things like this that are very natural and let me focus my energy on my task.

Collapse
ug02fast profile image
Arthur Zhuk Author

Managing mental overhead is the crux when it comes to coding for me as well. That's one reason I don't like using sneak as much. I like to be in the habit of making those constant vim motion commands instead of typing out the word I want to jump to. Should be an interesting case study to say the least.

Collapse
waylonwalker profile image
Waylon Walker

Honestly a lot of time I even do a dj.. rather than d6j as it is easier to see. I can have 2 lines gone faster than my eyes move to the numbers and find it on the number row. The main reason I us relative lines are when I need the whole block in my paste register.

Collapse
bretgeek profile image
bretgeek

The line , column number at the bottom right is enough for me:)