JVApen
JVApen

Reputation: 11317

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

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

Upvotes: 8

Views: 1808

Answers (2)

Thlv
Thlv

Reputation: 33

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.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172510

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 :-)

Upvotes: 2

Related Questions