How to display line numbers in Vim / Vi

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

By default, Vim doesn’t display line numbers, but it can be easily displayed. Vim supports three numbering modes that help you navigate files. Aside from the standard absolute line numbering, Vim also supports relative and hybrid line numbering modes.

In this guide, we will show you how to show or hide line numbers in the Vim / Vi text editor.

Aside from assisting in navigating the code, viewing numbers online is also useful in other situations such as collaborative programming, script debugging, code review, ease of referencing specific lines and many more.

Absolute line numbers

Absolute line numbering is standard line numbering, and it displays consecutive line numbers next to each line of text.

To enable line numbering:

  1. push the button Esc To switch to command mode.
  2. Journalism : (2 dots) and the cursor will move in the lower left corner of the screen. Type set number or set nu And press Enter.
    :set number

    Vim numbering

  3. Line numbers will be displayed on the left side of the screen:The vim number is displayed

To disable absolute line numbers, turn on :set nonumber or set nonu

:set nonumber

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

:set number!

Relative line numbers

When relative line numbering is enabled, the current line appears as 0, And the lines above and below the current line are gradually numbered (1And the 2And the 3 … etc).

Relative positioning is useful because many operations in Vim such as move up / down and delete rows on relative line numbers.

For example, to delete the next ten lines below the cursor, you can use the command d10j . With relative line numbers enabled, you will have a better visual overview of the code.

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

:set relativenumber

Relative line numbers

To turn off relative line numbers, type :set norelativenumber or set nornu

:set nonumber

To activate relative line numbers, use the command :set relativenumber! or :set rnu!

:set number!

Hybrid numbering mode

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

Hybrid line numbering is the same as relative line numbering, the only difference being that the font is current instead of width 0 Instead, it displays the absolute row number.

To enable mixed line numbering, run the command number And the relativenumber

:set number relativenumber

Hybrid line numbers

The same can be accomplished by executing the commands one by one:

:set number 
:set relativenumber

To disable mixed mode, you must turn off absolute and relative numbering.

Permanent arrangement

If you want the line number to appear every time you start Vim, add the appropriate command to .vimrc (Vim configuration file). For example, to enable absolute line numbering, you can add the following:

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *