[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
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 Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM