How to let Vim use relative line numbers counting from 1 instead of 0
Asked Answered
C

2

7

I've activated showing the line numbers with the relative line numbers active by including the following lines in my .vimrc:

" Set line-numbers
set nu
set relativenumber

This all works nicely, though Vim starts counting the current line from 0, while I like it to count from 1.

So currently it looks like this (where, for example, 57 is the current line number):

 2 something else
 1 
57 current line
 1 one line below
 2 another line below

Let's assume that I like to delete the 3 lines current line, one line below, and another line below. I have to use the command 3dd while the relative line number states "2". Does anyone know how to change this to:

 3 something else
 2 
57 current line
 2 one line below
 3 another line below
Cleavland answered 7/11, 2015 at 11:23 Comment(3)
I think most people use relative line numbers with j and k and a count: d2j and d2k. Then the numbers are just right.Geesey
I guess I'm not most people :( Somehow I don't use hjkl that oftenCleavland
@Geesey Yes, I agree, but still I find a bit weird the fact that doing 3dd and d3j give a different result by one line. I mean, I understand that they are different commands, but I feel like they should be interchangeable without having to think "oh I should add +1 to do what I want to do".Slave
B
2

The counting is built into Vim's core; you'd have to change the source code and recompile your custom binary. Alternatively, there's the RltvNmbr.vim plugin, which emulates the setting in Vimscript. By modifying that, you'd avoid the recompilation, but only get an emulation that's far from perfect. Better adapt to Vim's way of counting :-)

Bleat answered 10/11, 2015 at 10:39 Comment(2)
From what I can find, also this extension counts from 0, though off course it will be easier to adapt. A bit unfortunate that it's not supported by default (both by vim as the extension) though I guess I'll have to adapt instead of configuring my tools :(Cleavland
Right, that's what I meant. The plugin is easier to modify, but still I would recommend to rather adapt to the current behavior.Bleat
A
0

enter image description here

This way is used in lazyvim, and nvim version for nvim-0.10. If you use another nvim distro, its same I think.

There is a way to set relative number from 1 in neovim, in lazyvim disrtro, you can find file ~/.local/share/nvim/lazy/LazyVim/lua/lazyvim/util/ui.lua, find this lua function and edit it:

if vim.fn.has("nvim-0.11") == 1 then
  components[2] = "%l" -- 0.11 handles both the current and other lines with %l
else
  if vim.v.relnum == 0 then
    components[2] = is_num and "%l" or "%r" -- the current line
  else
    -- components[2] = is_relnum and "%r" or "%l" -- other lines
    components[2] = is_relnum and tostring(vim.v.relnum + 1) or "%l" -- other lines
  end
end

Replace components[2] = is_relnum and "%r" or "%l" to components[2] = is_relnum and tostring(vim.v.relnum + 1) or "%l"

Save and exit this file, restart nvim and open a file, you can see the relative number in vim is counting from 1, both show the current line number.

Acidity answered 29/7, 2024 at 8:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.