Vim is a popular text editor in Linux used for coding as well as text editing. Often you may need to use line numbers in your document, while using Vim editor. It is useful for debugging issues as well as improving overall readability, for developers, system administrators and general users. In this article, we will learn how to enable & disable line numbers in vi/vim on Linux systems.
How to Enable & Disable Line Numbers in Vim
Here are the steps to show or hide line numbers in Vim.
Enable Line Numbers in Vim
First press Esc key. Then press colon(:) key followed by any of the following commands, to enable line numbers.
set number OR set nu
The above commands will display absolute line numbers with the topmost line having line number as 1. You may also display relative line numbers where the first line is the line where your cursor is presently located, numbered as 0. The line above as well as below are numbered as 1. The lines below and above these two lines are numbered as 2 and so on. In relative numbering, sequential numbers are assigned both upwards as well as downwards, starting from your present line.
Here is the command to enable relative numbering. Please note, relative numbering is available only in vim editor and not vi editor.
set relativenumber OR set rnu
Disable Line Numbers in Vim
If you want to disable line numbers in vim editor, press Esc key, followed by colon (:) key. Enter any of the following commands to disable line numbers in Vim.
set nu! OR set nonumber
If you want to turn off relative numbering, enter the following commands.
set norelativenumber OR set nornu
Permanently Enable or Disable Line Numbers in Vim
The above commands and approach will enable or disable line numbering only as long as you are within vim editor. Once you exit the editor, all these changes will be reverted back to their default settings. If you need to use line numbers every time in vi/vim, you need to add these settings in its configuration file.
Open its configuration file in text editor.
$ vi ~/.vimrc
Add the following line to enable line numbers.
set number
On the other hand, add the following line to disable line numbers.
set nonumber
Save and close the file to apply changes. Sometimes you may need to start a new session to apply changes.
You can also set the minimum number of columns to be used for displaying numbers, using numberwidth variable.
set numberwidth=1 set numberwidth=4 set numberwidth=2
The minimum value of above variable is 1 and its maximum value is 10.
If you want to directly open a file in vi/vim editor at a specific line number then use the following syntax.
$ vi +linenumber filename
Here is an example to open file data.txt at line 100 in vi/vim editor.
$ vi +100 data.txt
In this article, we have learnt how to display & hide line numbers in vi/vim editor.
Also read:
How to Read Audit Logs in Linux
How to Check User Login Details in Linux
How to Delete Multiple Directories in Linux
How to Force CP Command to Overwrite Without Confirmation
How to Force Delete Directory in Linux