Table of Contents
Vim Editor was written by Bram Moolenaar in 1991, based on the Vi Text editor. At first his name meant Vi Imitation. Renamed "Improved VI" as a result of many improvements found in version 2.0 of Vim.
This text editor, which can be run on many operating systems, has many features that cause it to be preferred. One of them is that it displays the line number in the opened file.
How to Show Line Numbers in Vim?
By default, Vim doesn't show line numbers, so you need to enable/turn this setting on. The feature to show line numbers is done in 3 ways:
- Absolute Line Numbers
- Relative Line Numbers
- Hybrid Line Numbers
Now let's explain them in order.
Absolute Line Numbers
For this feature to be active, after opening the file with vim, first ESC and then :set number or :set nu must be written.
To disable this feature, just type :set nonumber or :set nonu after pressing ESC.
After pressing ESC, typing :set number! or :set nu! also removes the number line.
It can be useful for finding the wrong line in the log result. When you open the file, the problem is solved quickly by going to the wrong line. Open the file, type the line number with the following command:
- absolute line numbers active
- absolute line numbers inactive
- absolute line numbers inactive(Other option)
:set the number!
Relative Line Numbers
In this feature, the line number changes according to the line where the cursor is located. Each line in the file is numbered according to the current position of the cursor to show the distance to that line. The current row is marked as 0, above and below as 1.
For this feature, the following commands are used after pressing ESC:
- relative line numbers active
- relative line numbers inactive
- relative line numbers inactive(Other option)
Hybrid Line Numbers
In Vim 7.4 and later it was possible to enable both absolute and relative line numbers at the same time. It's called hybrid line number mode. While either one is active, the other can also be activated. In other words, when Absolute is active and Relative is activated, or vice versa, hybrid mode occurs.
If none is active, hybrid mode is activated with the following commands after ESC:
- hybrid line numbers active
:set number relativenumber
:set nu rnu
For inactive mode:
- hybrid line numbers inactive
:set nonumber norelativenumber
:set nonu nornu
- hybrid line numbers inactive(Other option)
:set number! relativenumber!
:set nu! rnu!
If one of the modes is deactivated, the other mode remains active. If both are disabled, line numbers will not appear.
We talked about a feature of the Vim editor. Vim is a very powerful and advanced editor. Some users develop apps with this editor.
For more information about Vim, you can get help from the manual page:
foc@fedora:/tmp$ man vim NAME vim - Vi IMproved, a programmer's text editor SYNOPSIS vim [options] [file ..] vim [options] - vim [options] -t tag vim [options] -q [errorfile] ...
foc@fedora:/tmp$ vim --help VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Oct 11 2022 00:00:00) Usage: vim [arguments] [file ..] edit specified file(s) or: vim [arguments] - read text from stdin or: vim [arguments] -t tag edit file where tag is defined or: vim [arguments] -q [errorfile] edit file with first error Arguments: -- Only file names after this -v Vi mode (like "vi") -e Ex mode (like "ex") -E Improved Ex mode ...
stackoverflow.com - Command for toggling line numbers in vi