How to display line number in vim

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.

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Durga Apr 12, 2012 @ 20:21

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.