[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to Show Line Numbers in Vim / Vi.

How To Show Line Numbers In Vim / Vi.

This article will show you how to display or hide line numbers in Vim / Vi text editor.

Vim/Vi is the text editor of choice for many software developers and Linux system administrators.

By default, Vim doesn’t show line numbers, but they can be easily turned on. Vim supports three modes of line numbering that helps you navigate through the files. In addition to the standard absolute line numbering, Vim also supports relative and hybrid line numbering modes.

Besides helping navigate the code, line numbering is also useful in other situations such as pair programming, debugging scripts, code reviews, referring to a specific line, and more.

 

Absolute Line Numbers

The absolute line numbering is the standard line numbering, which displays the appropriate line number next to each line of text.

To activate the line numbering, set the number flag:

  1. Press the Esc key to switch to command mode.

  2. Press : (colon) and the cursor will move at the bottom left corner of the screen. Type set number or set nu and hit Enter.

    :set number
  3. Line numbers will be displayed at the left side of the screen:

To disable the absolute line numbers off, run the :set nonumber or set nonu commands:

:set nonumber

You can also toggle the line numbers with :set number! or :set nu!:

:set number!

Relative Line Numbers

When the relative line numbering is enabled, the current line is shown as 0; The lines above and below from the current line are incrementally numbered (1, 2, 3, etc.).

Relative line mode is handy because many Vim operations, such as moving up/down and deleting lines work on relative line numbers.

For example, to delete the next ten lines below the cursor, you would use the d10j command. With relative line numbers enabled you’ll have a better visual overview on the code.

To enable the relative line numbering, switch to the command mode and enter :set relativenumber or :set rnu:

:set relativenumber

To disable the relative line numbering, type :set norelativenumber or set nornu:

:set nonumber

To toggle the relative line numbering, use the :set relativenumber! or :set rnu! command:

:set number!

Hybrid Line Numbers

In Vim 7.4 and later, enabling both the absolute and relative line numbers at the same time sets up the hybrid line number mode.

Hybrid line numbering is the same as the relative line numbering with the only difference being that the current line instead of showing 0 shows its absolute line number.

To turn on the hybrid line numbering, run both the number and relativenumber commands:

:set number relativenumber

The same can be achieved by running the commands one by one:

:set number 
:set relativenumber

To disable the hybrid mode, you’ll need to turn off both the absolute and relative numbering.

 

Permanent Settings

If you want line numbers to appear each time you launch Vim, add the appropriate command to your .vimrc (Vim configuration file). For example, to enable absolute line numbering, you would add the following:

vim ~/.vimrc
:set number

Conclusion

To show line numbers in Vim, use the :set number command for absolute line numbers, :set relativenumber for relative line numbers. If both absolute and relative line numbers are enabled Vim switches to the hybrid line numbering mode.

Keep reading
How to use Uptime Command.
May 5, 2023

How to use Uptime Command.

How To Use Uptime Command. In this tutorial, we will cover the uptime command. As its name suggests, the uptime command shows how long the system has been running. It also displays the current time, the number of logged-in users, and the system load averages for the past 1, 5, and 15 minutes. How…

Read article
How to Read a File Line By Line in Bash
May 5, 2023

How to Read a File Line By Line in Bash

How To Read A File Line By Line In Bash In this tutorial, we will discuss how to read a file line by line in Bash. When writing Bash scripts, you will sometimes find yourself in situations where you need to read a file line by line. For example, you may have a text file…

Read article
How to use Wc Command (Count Number of Lines, Words, and Characters)
May 5, 2023

How to use Wc Command (Count Number of Lines, Words, and Characters)

How To Use Wc Command (Count Number Of Lines, Words, And Characters) In this tutorial, we will show you how to use the wc command through simple and practical examples. How to Use the wc Command The syntax for the wc command is as follows: wc OPTION… [FILE]… The wc command can accept zero…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00