Vim/Vi Show Line Numbers

Show line number in VIM editor

Vim/Vi is a popular text editor for many Linux software developers and especially for system administrators. You feel need of Vim/Vi show line numbers feature enabled, if you are debugging some script.

Today, we will teach you, how to display line numbers when you use vim/vi editor.

Show or Hide Vim/Vi Line Numbers

By default, Vim/Vi number lines aren’t displayed when you use the text editor. But there is a way for you to hide or display line numbers in vim whenever you want to. You don’t need root privileges, only thing you’ll need to equip yourself with is Vim/Vi  Editor.

You will run a Vim/Vi show line on your text editor in less than a minute.

Absolute VIM Line Numbers

The standard for line numbering in Vim is the absolute line number. It will show the line number beside the following line.

Display VIM Absolute Numbers

If you want to activate and show the Vim line numbering, set the number flag, and follow these easy steps.

Step 1: Press “ESC” to go to the command mode.

Step 2: Press “:” or colon, so that the cursor goes to the bottom left of your screen.

Step 3: Type either two of the following scripts:

:set number
OR
:set nu

Step 4: Press “ENTER.” From there, the line numbers should be displayed on the left side of your screen.

Turn-off VIM Absolute Numbers

If you want to turn off vim absolute line number, type the following command on your script:

:set number
OR
set nonu

Toggle VIM Line Numbers

You can even toggle line numbers using the following command:

:set number! 
OR
:set nu!

Relative Line Numbers

When you enable relative line numbers, the current line numbering will be displayed as 0.

The lines above and below the previous lines will then display an incremental number.

Enabling the relative line number is pretty helpful! There’s also a ton of operations in Vim where you’ll be working with relative numbers.

Let’s set an example. If you want to remove the next ten lines under your cursor, what you’d usually do is type the “d10j” command.

When you enable relative line numbers, you’ll have a better visual and overview of the next ten lines you want to remove.

Show Relative Line Numbers

Enabling relative line numbers is easy. Open your command mode and type the following command:

:set relativenumber
OR
:set rnu:

Disable Relative Line Numbering

Disabling relative line numbers is also easy. Type any of the following commands, “SET NONUMBER,” “SET NORNU,” or “SET NORELATIVENUMBER”:

:set norelativenumber
or
set nornu:
or
:set nonumber

Toggle Relative Line Numbering

If you want to toggle relative line numbers, you can run either of the following commands:

:set relativenumber!
or
:set rnu!
or
:set number!

Hybrid Line Numbers

You can also set up the hybrid line mode to enable both relative and absolute line numbers.

But, this is only possible with Vim 7.4 or a later version of it.

Hybrid line numbering is nearly similar to relative numbering. The only difference is that the current line number shows an absolute line number in vim instead of 0.

Enable Hybrid Line Numbers

You can try this out yourself and enable hybrid line numbers in vim. Make sure to run both “NUMBER” and “RELATIVENUMBER” commands:

:set number
or
:set relative number

Disable Hybrid Line Numbers

The only way for you to disable Hybrid mode is by disabling both relative and absolute line numbers in vim.

Permanent Settings

You can even make the line numbers in vim appear every time you launch the software. The steps are easy.

Make sure to type the command “.vimrc” or Vim configuration file:

vim ~/.vimrc

Next, don’t forget to append the following line:

set number

Opening a file in a particular line number or file location

Using the right command lets you jump to open a particular file, either in a line or specific location.

Enter the following command on your script:

vi +linenumber file. c
vi +300 initlib.c

You can also jump to a specific line that has () main function. On the script, type in the following:

vi +/searchTermHere file.c
vi +/main initlib.c
###Note shell escape done with  ###
vi +/addUser( initlib.c
vi +/addUser(arg1) initlib.c

Conclusion

Use the “:set number” command if you want to enable absolute line numbers; or “:set relativenumber” for relative Vi numbers.

If you want to turn off the line numbering, remember the command “set nonumber.”

When you have both absolute and, hybrid numbers enabled, you’ll have access to the hybrid mode!

Ref: The Electric Toolbox Blog.

Related Posts

Exit mobile version