Having difficulties in displaying Linux vi / vim text editor line numbers while modifying or writing a shell script? This guide is for you.
Showing line numbers in Linux vi / vim text editor makes it very easy for users to debug errors or locate a particular line that needs to be modified.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to debug tasks on their Linux Server.
In this context, we shall look into how to display or hide line numbers in Vim / Vi text editor running on Linux/Unix based systems.
Vi stands for Visual. It is a text editor that is an early attempt to a visual text editor. Vim stands for Vi Improved. It is an implementation of the Vi standard with many additions.
They are very popular among software developers, system administrators and Linux/Unix/macOS users but lines are not displayed by default.
Just like in Window Notepad ++ html editor, Vim can also be made to display line numbers in the left margin. To make this happen, simply follow the process presented below;
i. Log into your Server via an SSH tool such as putty and once logged in as the root user, press the "ESC" key.
ii. Next, type in the following command to run on line numbers at the prompt;
set number
iii. To turn off line numbering, simply run the following command;
set nonumber
Below, we shall look into these activities briefly.
In order to display line numbers in the text editor which will be displayed on the left side of the prompt window, execute the following command. But first press the "Esc" key followed by ":". Now enter any one of the following command at a : prompt;
set number
or
set nu
To display relative line numbers in vim or vi, use any of the following;
:set relativenumber
OR
:set rnu
To hide line number , simply execute any of the following command;
set nu!
OR
set nonumber
Similarly, to turn off relative line numbers, use:
:set norelativenumber
OR
:set nornu
In cases where you need number every time you start vi/vim, append the following line to your "~/.vimrc" file. Execute the command;
vi ~/.vimrc
Then, Append the following line;
set number
Save and close the file in vim.
To go to particular line number from a shell prompt, simply enter;
vi +linenumber file.c
vi +300 initlib.c
To go to particular line that contains main() function from a shell prompt, enter:
vi +/searchTermHere file.c
vi +/main initlib.c
### note shell escape done with \ ###
vi +/addUser\( initlib.c
vi +/addUser\(arg1\) initlib.c
Set following option:
:set numberwidth=N
:set numberwidth=1
:set numberwidth=4
:set numberwidth=10
The minimum value of N is 1, the maximum value N is 10. See this page for more info.
To list all available settings, do the following;
At the : prompt, enter:
set all
To see a list of everything that you have set so far in your vim config file or runtime, type the following command at the : prompt:
set
This article will guide you on how to show or hide line numbers in vim / vi text editors using commands.