Logo

Display Line Numbers in Vim and Vi Text Editors In 3 Simple Ways

Try this guide with our instant dedicated server for as low as 40 Euros

display line numbers in vim

Vim, a highly configurable text editor built for efficient text editing, has various features that cater to programmers and writers alike. 

One such feature is the ability to display line numbers, which is essential for navigating and editing large files. Whether you are a beginner seeking to enhance your editing efficiency or a seasoned programmer looking to streamline your workflow, understanding how to display line numbers in Vim is invaluable. 

In this comprehensive guide, we will explore three popular methods you can use to enable, customize, and disable/hide line numbers in Vim. 

Table Of Contents

  1. How to Enable and Hide Line Numbers in Vim
    1. The Prerequisites
    2. Method #1: Absolute Line Numbers
    3. Method #2: Relative Line Numbers
    4. Method #3: Hybrid Line Numbers
  2. Permanently Display Line Numbers in Vim
  3. Additional Configurations to Display Line Numbers in Vim
    1. Adjust the Number Column Width
    2. Adjust Text Wrap in the Number Column
    3. Adjust Line Number Color
  4. Conclusion
  5. FAQs

How to Enable and Hide Line Numbers in Vim

There are three distinct methods to configure line numbers in Vim / Vi when using the feature vim show line numbers. These settings adjust how line numbers appear within the text editor.

Absolute Line Numbers: This traditional mode displays a sequential number beside each line, starting from the top of the document.

Relative Line Numbers: Line numbers are displayed based on their distance from the cursor’s current position in this mode.

Hybrid Line Numbers: This option combines absolute and relative line numbers, showing the actual line number for the current line where the cursor is located, while other lines display their relative distance from the cursor.

The Prerequisites

Before understanding how to display line numbers using Vim, ensure you have the following:

  • A system running a mainstream Linux distro
  • A use account with sudo or administrative privileges

Method #1: Absolute Line Numbers

The first method deals with Absolute line numbers that act like the traditional line numbers. Here are the steps you need to follow to enable these line numbers. 

Step #1: Enter Command Mode

Press the Esc key to exit the Insert mode. This also enables Vim’s Command mode. 

Step #2: Set Line Numbers

Next, press : to prompt the command line. Now run the following command to view line numbers:

: set number

Alternatively, you can also use the shorthand version:

: set nu!

set nu!

Hide Absolute Line Numbers

You can also hide the absolute line numbers you enabled by following these steps:

Step #1: Enter Command Mode

Press the Esc key to exit Insert mode. This will enable the command mode. 

Step #2: Disable Line Numbers

Next, press : to prompt the command line. Next, run this command to disable line numbers:

: set nonumber

Alternatively, you can also use the shorthand version:

: set nonunu!

set nonunu

Method #2: Relative Line Numbers

This method enables relative line numbers, which allows you to see how far a line is from your current cursor location. 

Step #1: Enter Command Mode

Press the Esc key to exit Insert mode and enter the Command mode. 

Step #2: Set Line Numbers

Next, press : to prompt the command line. Run the following command to enable relative line numbers:

: set relativenumber

Alternatively, you can also use the shorthand version:

: set rnu

set rnu

The lines in the output are numbered based on their distance from the cursor.

In our example, the cursor’s position is marked as 0. The lines immediately above and below the cursor are numbered 1, and the numbers increase the further they are from the cursor.

Disable Relative Line Numbers

You can disable relative line numbers by these steps:

Step #1: Enter Command Mode

Press the Esc key to exit the Insert mode and enable the Command mode. 

Step #2: Disable Line Numbers

Next, press : to prompt the command line. To disable line numbers, run this command:

:set nonumber norelativenumber

set number relativenumber

Alternatively, you can also use the shorthand version:

:set nonu nornu

Method #3: Hybrid Line Numbers

In addition to absolute and relative line numbers, you can also enable hybrid line numbers by following these steps.

Step #1: Enter Command Mode

Press the Esc key to exit Insert mode. This action will also enable the Command mode.

Step #2: Set Line Numbers

Next, press : to prompt the command line. Now, enter the following command to enable relative line numbers:

:set number relativenumber

Hybrid line numbering in Vim combines absolute and relative numbering. It applies relative numbers to all lines except the cursor line, which displays its absolute number.

Disable Hybrid Line Numbers

Hybrid line numbers can be confusing for some users. If that’s the case with your, we recommend the following steps to disable them.

Step #1: Enter Command Mode

Press the Esc key to exit Insert mode and enter the Command mode. 

Step #2: Disable Line Numbers

Next, press : to prompt the command line. To disable line numbers, run:

:set nonumber norelativenumber

Permanently Display Line Numbers in Vim

Vim, by default, does not display line numbers. So, any of the above methods simply enable line numbers for the duration of the current session. Once you close the file, Vim reverts to its default setting. 

You can enable line numbers permanently in Vim so that you always see them when you edit files. This can be accomplished by editing the standard Vim config file. 

Start by opening the Vim configuration file.vimrc in your preferred text editor. Since this tutorial is about Vim, we will use the following command:

# vim ~/.vimrc

Add the following line to the file:

set number

Save the file.

Now, launch Vim and press Esc to enter the Command mode. Next, type the following command:

:wq

This action sets up absolute line numbering across all Vim sessions.

Additional Configurations to Display Line Numbers in Vim

Line numbers in Vim are an overlooked aspect of the user experience. You can optimize the display of line numbers in Vim sessions. For instance, you can tweak the following three options. 

Adjust the Number Column Width

By default, Vim allocates a certain screen width to line numbers in the gutter column on the left. In Vim versions 7 and later, you can customize this width using the numberwidth option.

You can apply these steps for this purpose.

Step #1: Enter Command Mode

Press Esc to enter the Command mode.

Step #2: Set the Number Display Width

At the command line, type:

:set numberwidth=[number]

Replace [number] with your desired width.

Step #3: Enable Permanent Display

You can make this change permanent by adding the above command to the .vimrc file. Open the file in a text editor and enter the following line to set the gutter width to 10:

:set numberwidth=10

set numberwidth

Adjust Text Wrap in the Number Column

The following steps allow you to adjust text wrapping in the number column in Vim. 

Step #1: Enter Command Mode

Press Esc to enter the Command mode.

Step #2: Adjust Text Wrap

Run the following command to configure text wrapping:

:set cpoptions+=n

Step #3: Make Text Wrapping Permanent

Add the following line in the ~/.vimrc file to make text wrapping permanent.

set cpoptions+=n

Adjust Line Number Color

You can modify the color of line numbers in Vim by following these steps: 

Step #1: Enter Command Mode

Press Esc to enable the Command mode.

Step #2: Adjust Line Number Color

Enter the following command syntax to set the line number color:

: highlight LineNr term=bold cterm=NONE ctermfg=[color] ctermbg=NONE gui=NONE guifg=[color] guibg=NONE

For instance, the following command to set the line number color to red:

: highlight LineNr term=bold cterm=NONE ctermfg=Red ctermbg=NONE gui=NONE guifg=Red guib=NONE

highlight LineNr term

Conclusion 

Mastering the display of line numbers in Vim can significantly improve your text editing experience. The Vim Show Line Numbers feature is a tool for better navigation and improves the accuracy of code editing and reviewing. 

By mastering the different line number modes—absolute, relative, and hybrid—you can customize Vim to suit your specific needs, making it a more powerful ally in your coding and writing endeavors. Whether debugging code or editing a manuscript, enabling line numbers in Vim is a simple yet effective step toward a more controlled and efficient editing environment.

FAQs 

Q. What is Vim, and how is it used as a powerful text editor?

Vim is an open-source text editor known for its command-line productivity and extensive customization options. It is widely used to write and edit text files on Unix/Linux systems, operating systems, and, even cloud environments.

Q. How do I display line numbers in the Vim editor?

To show line numbers in a file using the Vim editor, run the command syntax: set number while in command mode. This command activates the primary editor display of line numbers on the Vim text editor screen.

Q. What is insert mode in Vim?

Insert mode in Vim allows you to insert and edit text directly in the document instead of navigating and running commands. You can enter insert mode by pressing i while in command mode.

Q. Can I display relative line numbers in Vim?

You can use the : set relativenumber command in Vim to display relative line numbers, where each line number indicates the line’s distance from the cursor’s current position.

Q. What is the hybrid mode in displaying line numbers in Vim?

Hybrid mode combines absolute and relative line numbering, showing the actual line number for the current line where the cursor is located, while other lines display their relative distance from the cursor. You can enable it using set number relativenumber.

Q. How do I change the color schemes of line numbers in the Vim editor?

To change the color scheme for line numbers in Vim, use the command :highlight LineNr ctermfg=color, where color can be replaced with your preferred option. This enhances the vim editor display, making it easier to navigate through terminal sessions.

Q. What is the significance of the Vim editor configuration file?

The Vim editor configuration file, typically named .vimrc, allows users to customize settings permanently. The Vim text editor retains specified settings across sessions by adding commands like set number to files.

Q. How can Vim improve command line program efficiency?

Vim excels at command-line productivity through powerful command shortcuts, source command capabilities, and rapid navigation features, making it a standout choice for advanced text editing directly from the terminal.

Q. Is Vim suitable for use in professional development environments?

Vim’s robust editing features, compatibility with various scripting languages, and ability to work seamlessly in different environments make it ideal for professional software development, especially within Unix/Linux systems and open-source projects.

Q. How do I execute a single command in Vim?

To execute a single command in Vim, switch to command mode by pressing Esc, type : followed by your command, and press Enter. This could range from configuration changes to file operations within the vim text editor.

Try this guide with our instant dedicated server for as low as 40 Euros