How to display line number in vim

last updated in Categories , ,

How do I display line numbers in vim running on Linux or Unix-like operating systems?

Vim user need to type :set number command within vim text editor session when you are editing the file to display line number in vim. In this tutorial we will show you how to show line number in vim on Linux based desktop or server systems.

Start the vim editor

You need to start vim text editor. Open the terminal application and type:
vim

How to display line number in vim

Simply type the following commands to display line number in the left margin:

  1. Press the ESC key
  2. At the : prompt type the following command to run on line numbers: set number
  3. To turn off line numbering, type the following command at the : prompt again: set nonumber
  4. One can set relative line numbers by typing: :set relativenumber

Let us see all commands and examples in details.

How to show line number in vim

To show line number along the left side of a vim window, type any one of the following command while using vim text editor. First press the Esc key. Press : and type any one of the following command at a : prompt –
set nu
Alternatively, type:
set number
How to display line number in vim

How to display relative line numbers in vim

Execute the following command withing vim:
:set relativenumber

How to Show Line Numbers in Vim
How to Show Relative Line Numbers in Vim

The above vim command display the line number relative to the line with the cursor in front of each line. Relative line numbers help you use the count you can precede some vertical motion commands (e.g. j k + -) with, without having to calculate it yourself.

How to make the vim editor display line number by default

If you need line number every time you start vim text editor, append the following line to your ~/.vimrc file:
vim ~/.vimrc
Append the following line:
set number
Save and close the file in vim.

Change gutter column width for numbering in vim

The vim syntax is:
:set numberwidth=2
Try the following in ~/.vimrc config file:
set numberwidth=2

Configure text wrapping for the number column in vim

Run the following syntax within vim:
:set cpoptions+=n
Or as usual add it to ~/.vimrc:
set cpoptions+=n

How to disable the line numbers off in vim

Simply run the following command within vim session:
:set nonumber
OR
:set nonu
To disable relative line number type any one of the following command:
:set norelativenumber
OR
:set nornu

Conclusion

In this tutorial, you learned how to display line number in vim and permanently show line number in vim using $HOME/.vimrc file. See vim home page here for more info.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

  1. I add to $HOME/.vimrc and boom boom. Every time I start I see line numbers on Linux server. Very nice.

    Still, have a question? Get help on our forum!