45

I'd like to see line numbers, starting with 1 at the top, on the left side of Vim. Ideally it would look like this:

1 | foo = Foo.new
2 | bar = Bar.new
3 | baz = foo.baz(bar)
...
10| test = AwesomeSauce.test

How can I do this in Vim?

2 Answers 2

57

You have two options: set number for regular line numbers

And also set relativenumber which will show relative line numbers. i.e. current line is always 0. This is useful for moving up/down N number of lines using 5j for example.

What is cool is that you can combine them. I have the following in my .vimrc

set number                     " Show current line number
set relativenumber             " Show relative line numbers

This will make it use relative numbers for all lines except the current line, which will show you the actual number.

3
  • Now I tried this, and it's a schyzo! I mean, confusing. :D
    – yo'
    Feb 3, 2015 at 19:02
  • 6
    You can also toggle the commands by appending a "!" to them. E.g. set number! will turn on line numbering if it's off, and turn it on if it's off. set relativenumber can be shortened as set rnu.
    – orjan
    Feb 3, 2015 at 21:48
  • here is a shortcut: :se nu to turn on line numbers and :se nu! to toggle - on if it is off, off if it is on
    – jmarina
    Oct 9, 2023 at 7:57
23

You can use the command:

:set number  

to turn on line numbering. To turn it off again you can use:

:set nonumber   

If you want vim to always default to showing line numbers you can add the command to your vimrc file.

nu and nonu can be used as aliases for number and nonumber respectively.

2
  • 3
    Here's a small abbreviation: :set nu " to enable :set nonu " to disable
    – stdcall
    Feb 13, 2015 at 19:30
  • 1
    another shortcut: :se nu to turn on line numbers and :se nu! to toggle - on if it is off, off if it is on
    – jmarina
    Oct 9, 2023 at 7:57

Your Answer

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