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.
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.
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.
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!
Top comments (15)
I don't use line numbers anymore. To move around in the same screen I use sneak.vim in "label-mode".
In here I press
s
and then look for thear
and move to the one I want.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.
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 pressctrl + 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.
If you are using neovim and coc, I would suggest github.com/neoclide/coc-smartf
This has its advantages. I'm using coc outside of work environment.
but you sure can do
666G
to go to absolute line number 666?this does not require mind aripthmetic at all.
Nor fancy settings with relative numbers.
Plus people on a live call can point you to the desired line without having to readjust every time you jump somewhere.
You will just be making additional keystrokes every time you want to jump around your file instead of getting in the habit of making 2-3 strokes to navigate. I'm personally all about less keystrokes. I do use
G
occasionally when I know a particular error is happening on some line from a stack trace.β₯ 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.
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.
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.
Yank works with the absolute line numbers as well.
type
V
as usual to visually select linestype
123G
to go to desired linenow yank. Or delete.
thats all
Benefit is that you are seeing what you are doing
The line , column number at the bottom right is enough for me:)
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
andL
to get close, then eyeball it, or I'll use the actual line number liked107G
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!
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.
Why not just type the line number you want to jump to and press
G
! like80G
to jump to line80
! it's easier and the file is still readable