Type 0 to show relative line numbers for the next command

How about 0 to show line numbers relative to the main cursor line for the next command?

Highlighter

add-highlighter window/number-lines number-lines -relative

Example – Jump down 6 lines:

06j
1 Like

06j already works :smiley: I suspect you meant 06g but as you can see its redundant with 6j, so this is not a really strong motivating use case in my book.

I think you’ve misunderstood the point. When using relative line numbers it is extremely easy to move by several lines down or up with j and k because you can see how many lines you need to jump. What @alexherbo2 wanted is to enable relative line numbers for next movement when 0 is pressed. For example:

433|
434|
435| some text█
436|
437|
...
452| needed text

We have such buffer, and we need to go to line 452 really quick. Our options are: 452g - 4 keys, jjjjjjjjjjjjjjjjj - 17 keys, or we could type 17j which is 3 keys, but we need to calculate 17. By pressing 0 we turning on relative line numbers:

  2|
  1|
435| some text█
  1|
  2|
 ...
 17| needed text

And we can immediately see that we need to press 17j, so this is 4 keys in total: 017j. And then we have our normal line numbers back. Though I use relative line numbers all the time.

This also can be applied to C, e.g: 07C will place cursors on each line without the need of always on relative line numbers

2 Likes

My issue with relative line numbers is they draw many attention to be enabled all the time. I’m exploring a setting which could work for me, with number-lines -relative enabled for a single command. I guess the general issue to this is the count argument before the command. It’s similar to 1s{regex-with-capture}<ret>, I usually think the capture when building the regex, and not before. The command works incrementally, but it’s not that incremental if in my head I have to think the argument before. xD

def switch-number-line -params .. %{
    try %{ remove-highlighter window/number_lines }
    add-highlighter window/number_lines number-lines %arg{@}
}

hook global NormalKey 0 'switch-number-line -relative'
hook global NormalKey \D.* 'switch-number-line'
2 Likes

This method works well. A small drawback: the git diff indicator and linters indicator pass on the right of the number_line column. This can be confusing.

Is there a way to keep the line numbers gutter to the right of the git gutter (so that the line numbers and separator | are closest to the text)?

Following snippet partially solves this. It adds git-diff and numbers highlighter into a group. But any new gutter highlighter appends to the left side, messing the current order. If there was a way to tell like for example: pin this hl-group always to the most right position then it probably would work.

define-command switch-number-line -params .. %{
    try %{ remove-highlighter window/gutter }
    add-window-gutter %arg{@}
}
define-command -hidden add-window-gutter -params .. %{
    add-highlighter window/gutter group -passes move
    add-highlighter window/gutter/ number-lines %arg{@}
    add-highlighter window/gutter/ flag-lines Default git_diff_flags
}
hook global WinCreate .* %{
    add-window-gutter
}
hook global BufOpenFile .* %{
    evaluate-commands -draft %sh{
        cd $(dirname "$kak_buffile")
        if [ $(git rev-parse --git-dir 2>/dev/null) ]; then
            for hook in WinCreate BufReload BufWritePost; do
                printf "hook buffer -group git-update-diff %s .* 'git update-diff'\n" "$hook"
            done
        fi
    }
}

Maybe I am trying to solve my issue the wrong way. Maybe someone can point out a better way to achieve what I want to do.

I prefer absolute line numbers. However, there is one use case where my current workflow requires relative line numbers.

Often, I am in the middle of a line and want to select the current line and the next n number of lines, let’s say 22. What I do now is I type 23X. I need relative numbers to quickly see how many Xs I need.

The only option I See with absolute numbers would be something like X263G, which feels much longer. Maybe it isn’t?

Is there a quicker way with absolute line numbers? I work in code, so maybe often I could instead use <a-i>{ or something line that, but I don’t think so. It’s more about refactoring parts from within a function to a new function. I will be more conscious about it and see what I can do.

If your codebase uses blank lines to group related statements together, you may be able to use paragraph-based keystrokes like <a-i>p to manipulate them.

Personally, I have my key-repeat speed set quite high, so I’m happy enough just holding down X until I can see the selection cover the text I want to manipulate - it may take a bit longer by the stopwatch, but I can do it with the visual-processing parts of my brain instead of using the language-and-mathematics parts to type in specific numbers.

Good point, thanks. I’ll see if I feel comfortable doing it similarly :blush:

You can first save an upper bound line with XZ and then move/scroll down to the desired line and press X<a-z>u in order to union a gap in between. There could be any arbitrary selection instead of X.