3

I have tried the relativenumber option several times, and I really don't get the point.

People say that it's easier because they don't have to do the mental math of how many lines to go up or down with j or k—they just look at the numbered lines and do, for example, 6k to go up six lines. Similarly, they can do d3j to delete three lines down.

What I don't understand is why they don't just use G or gg with a count instead. For example, instead of looking at the number column to see how many lines to delete, just look to see the number of the last line to delete, and do d[n]G, where n is the line number.

One thing I do a lot when programming in python is jump up to near the top of the file to import a package or another file, then jump back to where I was before to use the functionality that I just imported. This is actually better with G instead of k for two reasons.

  1. Because it works even when you don't know the line number (or the "relative number"): you can just do gg and move down a couple lines.
  2. Because motions like G and gg are considered "jumps" by vim, so you can just hit <C-o> after you've made the import to jump right back to where you were. If you use k, you would have to find where you were (which isn't always within view) and look at the number in the column, and go back there. You could also set a mark before jumping, but that's just one more thing to remember.

These are just a few examples, and I have yet to find a task that is more easily done with j or k with a count than with G or gg and a count. It is quite possible that I'm missing something, and I am sincerely interested to know what this feature could actually be useful for.

10
  • Well, what does it matter why people use it? If this is not for you, do not use it. Sep 20, 2020 at 19:46
  • 1
    @ChristianBrabandt "It is quite possible that I'm missing something, and I am sincerely interested to know what this feature could actually be useful for." Sep 20, 2020 at 19:48
  • 2
    I don't do it that often myself but don't you think 6k is just a wee bit easier to type than :2086<CR> or 2086<S-G> or 2086gg?
    – B Layer
    Sep 20, 2020 at 19:49
  • 1
    Well consider yourself lucky then, I guess. ;)
    – B Layer
    Sep 20, 2020 at 19:52
  • 1
    I've edited and re-opened this. Let's try to keep our opinions out of it and focus on the good stuff (using vim!)
    – D. Ben Knoble
    Sep 21, 2020 at 13:01

1 Answer 1

4

TL;DR

  • relative numbers are just another tool in the toolbox
  • they can be effective in terms of locality when a text-object doesn't cut it (and for the lazy-minded among us)
  • they pair nicely with commands that take ranges
  • but they aren't the "be-all/end-all" of navigating and editing text

One thing to keep in mind is that G/gg/Ctrl-o and 'relativenumber' are orthogonal. This means that both have uses, depending on what the situation calls for.

Example 1: not using relative number

For jumping to the top for python imports, yes, gg and then <C-o> are the easiest and most natural. (I'm not sure I really do anything else.)

Example 2: relative number when needed

When deleting a chunk of lines, text-objects are often a great way to go (dap, dV%, and dii get heavy use, where ii comes from indent-object). Every now and then, there's not one that's suitable, and so operation with a relative-number is convenient.

Why? Well, relative numbers are generally 2-digits or less (my screen has about 45 lines total, and most text that needs edited near the cursor is, well, near it). Line number can be quite a bit larger. This makes them easier to type.

It's similar to the classic idea of locality: benefit from having quick access to nearby objects. (I happen to rarely use relative numbers greater than 9: usually a text-object works at that point, and I'm too lazy to type counts with more than a single digit, unless it's 999@@ or something: repeated digits are easy.)

But at the end of the day, either relative number or absolute numbering will work.

Example 3: getting more out of :

Another use case of relative numbers is :-ranges: I find that many vim users don't use Ex commands to their fullest extent (:g, :s, and :w are enough for most people, especially with unimpaired making :n, :cn, and more, easily accessible in normal mode). This isn't a critique of that way of using vim! But it does mean that there is a significant part of the tool that's hidden behind :.

A couple of quick commands that I like:

  • :copy (synonym: :t) copies a range of lines to a target. :-3t. is fantastically easy with relative number, compared to (e.g.) :123t. or :123t126
  • d, y and p have line-wise analogues :delete (:d), :yank (:y), and :put (:pu). It's pretty easy to grab text with :+4y, wipe the file with :%d (not even relative numbers!), etc.

Both of these have the advantage of not moving the cursor! But they are line-wise, so they don't work if you wanted a partial line—unless you're willing to do some local editing after!

Generally, any command that takes a range is shorter to enter using relative numbers unless your file is extremely short.

Example 4: avoiding absolute numbers when not needed

Error messages often contain absolute line numbers, and that's the only thing I can think of that really use them. (Occasionally when discussing code with a peer, they make a handy reference point: "see over here on line xxx." But then I often jump there and highlight the text I'm talking about.)

And for error messages, the quickfix list and other features make avoiding typing numbers possible. (I'd rather edit text that's structured by more than lines (see ex. 2).)


If you use k, you would have to find where you were (which isn't always within view) and look at the number in the column, and go back there.

This is what I mean when I say they are orthogonal: the use-case of relative-number is generally for things that are in view!

For things that aren't nearby, another technique is often better-suited.

But every now and then, relative number shines.

5
  • There are faster ways and key combos for sure, but calling "hjkl" anti-pattern? It is all about comfort and goal. I personally prefer holding it to, say, count and use 17j for example.
    – Maxim Kim
    Sep 21, 2020 at 18:01
  • @MaximKim fair enough; i use ctrl-d/u for « paging » through files, and try to use more precise motions for actually going specific places. I’ll remove that paragraph, not really necessary anyway
    – D. Ben Knoble
    Sep 21, 2020 at 18:03
  • @MaximKim hjkl are good for small distances, is really what i meant
    – D. Ben Knoble
    Sep 21, 2020 at 18:04
  • jjjjjj and kkkkk are also good for "meditation" like navigation :)
    – Maxim Kim
    Sep 21, 2020 at 18:21
  • @D.BenKnoble Thank you for your very long and thorough answer! This really cleared things up, and I may just be adding a leader mapping to my config to toggle relative number on and off. Sep 22, 2020 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.