Signup/Sign In

How To Show Line Numbers In Vim / Vi

Vim does not show line numbers by default. However, the numbers that indicate the beginning of each line may be beneficial to change the text using the Vim command.

Many of the commands we use in Vim need you to supply line numbers. It is wise to enable line number in Vim editor to increase productivity.

In this lesson, learn how to display or conceal line numbers in Vim / VI in Linux and macOS.

3 Ways To Show Line Numbers In Vim

There are three distinct methods to show line numbers in Vim / Vi. You may configure the text editor to show:

  1. Absolute line numbers: Represents the well-known chronological numbering.
  2. Relative line numbers: Each line number is determined by its distance from the point of the cursor.
  3. Hybrid line numbers: A mixture of the two above-mentioned alternatives.

1. Display Absolute Line Numbers

This is a conventional and useful method for number lines as usual, starting from 1. To simply display line numbers:

1. First, go to normal mode by hitting Esc key.

2. Then, hit :(colon) to prompt a command line at the bottom of the file.

3. Finally, to display line numbers, type:

set number

Alternatively, you may use the shortened version of the above command:

set nu

Finally, press Enter to execute the command, which will display line numbers.

Set absolute line number in Vim editor

To hide/disable Absolute Line Numbers, follow the steps in Vi editor:

1. Press Esc to change to command/normal mode.

2. Use : (colon) to start the command bar.

3. Hide line numbers by entering the following command:

set nonumber

If you like, you may also use its shortened form:

set nu!

Last, press Enter. This will hide line numbers.

2. Display Relative Line Numbers

This mode shows line numbers relative to the current position of the cursor, with the current line being 0. It is useful for using other commands quickly.

1. Ensure you are in command mode by hitting Esc.

2. Next, hit : (colon) and prompt a command line.

3. Set Vim to show relative line numbers using the command:

set relativenumber

Or, use its shorter form:

set rnu

When you show relative numbers in Vim, each line is numbered by its distance from the cursor.

Set Relative line numbering in VIm

Take a look at the location of the pointer in the picture above. This position is denoted as 0. Accordingly, lines above and beneath the cursor are classified as 1, the next ones 2 and so on.

We can turn off Relative Line Numbers following these steps:

1. Switch to regular mode using Esc.

2. Press : (colon).

3. Now, you may switch off relative line numbers by entering one of the following commands:

set nonumber norelativenumber
# or use the shorter version
set nonu nornu

Display Hybrid Line Numbers

Combines the benefits of both modes, with the current line showing the absolute number and all other lines showing the relative number. It is what most of experienced users choose. To enable it:

1. Use Esc to make sure you are in command/normal mode.

2. Open the command line in Vim by pressing : (colon).

3. Activate hybrid line numbering by typing:

set number relativenumber

Alternatively, use a short form set nu rnu of this command.

4. Then, press Enter.

Hybrid line number in Vim and Vi editors

Hybrid line numbering is a blend of absolute and relative numbering. It employs relative line numbering for everything except the cursor line, which is denoted by its absolute number instead of zero.

To, turn off Hybrid Line Numbers:

1. Move to command mode(Esc).

2. Start by typing : (colon).

3. Then, disable hybrid line numbering using the below command:

set nonumber norelativenumber

Permanently Enable Line Numbers in Vim

Vim doesn’t contain line numbering by default. Activating line numbering only applies to the file you are working on.

Which means, once the file is closed, Vim returns to (the default) concealing line numbers.

To activate line numbers permanently:

1. Open the Vim configuration file with the name .vimrc.

vim ~/.vimrc

2. Enter “Insert” mode by pressing i.

3. Append the following line to the file:

set [numbering mode]

Replace [numbering mode] with your preferred line numbering method. Example: nu rnu.

4. Jump to normal mode with Esc key.

5. Press : (colon), and type the following command to write and exit vim editor.

wq

This will change the default line numbering mode for the current user.

Vim: change default line numbering mode with vimrc

FAQs

Although, vim has so many features and use cases that a book can be written on it. But we have tried to cover some relevant queries you might have.

Q. How do I jump to a specific line number in Vim?

Get to normal mode with Esc key, then type : (colon) followed by line number and press Enter. For example, to jump to line number 15, type :15 and press Enter. It will move the cursor to the 15th line.

Another way to move the cursor to a specified line is by typing the line number and G (capital) just after it, in normal mode. For example, type 15D to jump to line number 15.

Q. How to toggle line numbers in Vim editor?

To show/hide line numbers in vim, type the command :set number! in normal mode.

Use :set relativenumber! to toggle relative line numbers, and :set number! relativenumber! to toggle both types of line numbers.

Q: How to copy a specific line number in Vi?

You can use the command :[line]y to yank (copy) a line. For example, :10y will copy 10th line.

You can also use ranges to copy (yank) multiple lines, such as :5,10y to copy lines number 5 to 10.

Q: How to delete a specific line in Vim?

To delete a specific line number in Vim, type :[line]d in normal mode. For example, :20d will delete 20th line.

We hope you learned how to show or conceal line numbers in the Linux Vim editor. This will help you increase command line productivity tremendously.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.