How to Show Line Numbers in VI/VIM Text Editor

VIM or VI are two of the most powerful and versatile text editors widely used in the programming and system administration community. Despite their array of features, newcomers to Vi/Vim might find it challenging to display line numbers while editing files. In this guide, we will walk you through enabling the line number display feature, making it easier for you to keep track of each line’s position as you edit files in Vi/Vim.

Read more

The Issue

Before we begin, open any file in your system using the VI/VIM text editor. In this example, I’m opening the file /etc/maincap. You’ll notice it doesn’t display line numbers, as shown in the image below:

sudo vim /etc/maincap


Show line numbers in VIM or VI

Method 1 – Every time

Here is the method to display line numbers in the VIM or VI text editor every time you open a file. To enable this feature, press the Esc key and type the command :set nu then press Enter


You will see that the line numbers have been displayed as shown in the image below:


However, with this method, it only displays line numbers for the current file. In some cases, if you want to configure it as the default setting every time you open a file, switch to Method 2

Method 2 – By default

To configure it as the default setting, first, let’s open the configuration file /etc/vim/vimrc

sudo vim /etc/vim/vimrc

Scroll down near the end of the file and press the i key to switch to the edit mode. Then, add the line set number just below the line "set mouse=a.


After you’re done, save and exit the file by pressing Esc, entering the command :x, and then pressing Enter.


That’s it. From now on, every time you open a file using VIM/VI, you will see the line numbers displayed by default.


Leave a Reply

Your email address will not be published. Required fields are marked *