Open In App

How to Display Line Number in vim editor in Linux

Last Updated : 02 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to make the Vim editor display or hide line numbers. First, we look at what Linux and VIM editors and why we use them, are its features and how can we use them Follow the basic VIM editor guide to get familiar with the editor followed by various options to make the vim editor display or hide line numbers and how we can change the default settings of vim editor to display or hide line numbers.

Linux

Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, Cent OS, Red Hat, Debian, and Kali Linux.

VIM Editor

VIM stands for VI Improved (VIM) which is an improved version of the vi editor released in 1991, vi editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility, vi editor was developed in 1976 by Bill Joy. There are two modes in Vim Editor:

  • Insert Mode
  • Command Mode

Basic VIM editor guide

1. Create/Edit a file using Vi editor

vi filename

2. Insert Mode

To make any changes in the file first you need to enter in the insert mode to modify the file. To get into insert mode press button ‘i‘ to enter in insert mode.

3. Command Mode

To run any command in the vi editor you have to first enter in the command mode if you are currently in the insert mode then press Esc and then ‘:’ colon followed by your command to run your command in the editor.

4. Basic Navigation

To navigate easily within the file vi editor provides a large number of commands which are as follows:

Key

Description

k

Moves the cursor up one line.

j

Moves the cursor down one line.

h

Moves the cursor to the left one-character position.

l

Moves the cursor to the right one-character position.

Display line numbers in vim editor

Follow the below steps to display line numbers in the vi editor:

  • Enter in command mode, Press Esc if you are in insert mode
  • Use the following command to display line numbers

:set number

alternatively,

:set nu

Example:

Screenshot-from-2023-09-25-17-50-24

Display Relative line numbers in vim editor

To display relative line number in vi editor follow the below steps, in relative line number the current line is shown as 0 and the lines above and below are incremented by 1,

  • Enter in command mode, Press Esc if you are in insert mode
  • Use the following command to display relative line numbers

:set relativenumber

Alternatively,

:set rnu

Example:

Screenshot-from-2023-09-25-17-58-54

Hide line numbers in vim editor

Follow the below steps to hide line numbers in the vim editor:

  • Enter in command mode, Press Esc if you are in insert mode
  • If you are using the relative line numbers use the following command to hide relative line numbers,

:set norelativenumber

Alternatively,

:set nornu

Use the following command to hide line numbers,

:set nonumber

Alternatively,

:set nonu

Example:

Screenshot-from-2023-09-25-18-06-09

Toggle Display between Hide line numbers

To toggle the relative line numbers use the following commands,

:set relativenumber!

Alternatively,

:set rnu!

Example:

Screenshot-from-2023-09-25-18-11-57

To toggle the line numbers use the following commands:

:set number!

Alternatively,

:set nu!

Example:

Screenshot-from-2023-09-25-18-12-12

Configure Default Settings of vim editor

To change the default settings of vim editor, we have to configure the vim editor configuration file (.vimrc), to configure the default settings follow the below steps,

  • Open the file in vim editor to configure it

vi ~/.vimrc

  • Configure the default setting as per your requirements,
  • For example, if you want to display the line numbers by default when you open the vi editor add the command :set number

Screenshot-from-2023-09-25-18-22-38

  • When you are done with configuration Press Esc to exit the insert mode
  • Use :wq command to save the changes and exit the editor

Screenshot-from-2023-09-25-18-24-17

After saving the changes whenever you open the vim editor it will open with the default settings you set in the configuration file.

Frequently Asked Questions (FAQs)

What is VIM ?

VIM stands for VI Improved (VIM) which is a improved version of vi editor released in 1991, vi editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility.

I’m not able to write in the editor ?

To modify the text you have to enter in the insert/editing mode first, Press i or a to enter in the insert/editing mode.

What is command mode ?

To run any command in the vi editor you have to first enter in the command mode. To enter in the command mode press Esc and then ‘:’ colon followed by your command to run your command in the editor.

How to display line numbers in vim editor ?

To display line number in vim editor first enter in command mode and use :number or :nu command to display line number.

What is relative line numbers ?

In relative line numbering the current line is shown as 0 and the lines above and below the current line are incremented by 1

How to display relative line numbers in vim editor ?

To display line number in vim editor first enter in command mode and use :relativenumber or :rnu command to display line number.

Conclusion

In this article we had covered a basic vim editor guide followed by the commands for displaying line numbers, relative line number in vim editor and various commands to do the same , then how to toggle between display and hide line numbers , hide the line numbers and its command syntax along with the examples. At last we discussed some of the Frequently Asked Questions (FAQs) followed by how can we configure the default settings of the vim editor so whenever we opens the editor if loads with the default settings.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads