Vi editor is the most used editor when editing files via command line or terminal. Vi by default does not shows the line numbers of the opened file. However, when you are editing large files it becomes difficult to edit them without line numbers. Especially when you are editing them after reading a debugging message, which shows you proper line number.

A quick way to show line number is by type the following command.

:set number

You need to type this command when you are in command mode. Now when you do not want to see the line numbers, simply type the following command.

:set nonumber

However, showing line number by typing the :set number command is temporary and Vi will forget the changes after you exit. But there are times when you want to save the settings of your favorite Vi editor, by which it shows you the line number for every file you open. This is certainly possible if you keep your settings into the .vimrc or .exrc file. Confused ? Let’s be more clear.

Open the Vi editor and in the command mode enter :version and you will be able to see the version number of the current Vi editor in the next few lines you will see the location of these two files, .vimrc and .exrc .On my system it displays the following information.

system vimrc file: “$VIM/vimrc”
user vimrc file: “$HOME/.vimrc”
user exrc file: “$HOME/.exrc”

So these are the configuration files which get loaded every time you open Vi editor. We will store our command in one of these files to successfully save the changes. If your administrator has created this file already then you can open it and edit. Otherwise, create a new file in your home directory, as follows:

vi ~/.vimrc

Now, go into the insert mode and enter the following line.

:set number

Save the file and exit Vi editor. This file is saved as the config file and now whenever the Vi editor will be opened opened the .vimrc file will be loaded. The command :set number will be executed automatically and you will be able to see the line numbers. In this way you do not have to type the same command every time you open a file.

Hope that solves you problem.

Stay Digified !!
Sachin Khosla

Share this post: