How to See and Use Line Numbering in Vim

How to See Line Numbering in Vim

by | Apr 4, 2024 | Useful Tech

Quick! How many lines from the current one, 143, is the beginning of the paragraph, 116? Trying to see these numbers in the first place? Find out here how to see and use line numbering (both absolute and relative to the current line) in Vim and Neovim.

First, Count Like an Egyptian

Around the year 46 BCE, among the things out of whack in Rome was the very year itself. The Roman calendars predominantly followed the moon’s cycles, and Romans still preferred the months to line up with the sun’s seasons. This, they achieved with manual corrections and apparently decreasing success.

So, Julius Caesar, equipped with the powers of a tribune and a dictator (as well as that can be established in a Rome at war with itself), consulted with Sosigenes of Alexandia. The renowned astronomer suggested Romans to follow the Egyptian ways of counting days and months in a solar calendar.

This, pretty much, is what many a part of the world still does.

With that in mind, how about counting lines in addition to days, weeks, months and years?

How to See Line Numbering in Vim

Enable Line Numbering in Vim for the Current Session

Time needed: 2 minutes

To turn on line numbering in Vim or Neovim:

  1. Press Esc to enter normal mode in Vim.

    Here’s why: Esc exists insert mode and makes sure Vim is ready to take commands instead of text input.

  2. Now press : (colon) for command mode.

  3. Type set number to turn on line number in Vim.

    Relative numbering: For numbers relative to the current line, type set relativenumber instead.

  4. Press Enter.

    Line numbering in Vim on macOS

Enable Vim Line Numbering Permanently

To set up Vim for line numbering by default, add the following to your .vimrc file

set number " turn on line numbering
"set nonumber " turn off line numbering

"set relativenumber " turn on relative line numbers
set norelativenumber " turn off relative line numbers

Enable the desired options; the configuration above turns on absolute line numbers and disables relative line numbering, for example.

How to Use Line Numbering in Vim

You can make use of line numbers in Vim and Neovim in a number of ways:

  • Going to a line
    Type :<line number> and press Enter in normal mode to go to a line.
    Example: :8 will go to line number 8 or the last line if there are fewer than 8 lines.
  • Specifying a range
    Use :<starting line number>,<ending line number> to act on the specified lines (inclusively) in command mode.
    Example: :1,5d will delete the first five lines.
  • Giving a relative line or range
    You can use + and - to make a range’s line number relative to the current line.
    Examples
    :+2,+2d will delete the line after the next counting from the current line.
    :1,-1d will delete from the top until the current line, but leave the latter in place.
  • Repeating commands that act on lines
    Use relative line numbering to know how often to repeat a command that acts on lines. (Pattern matching might be easier to achieve the same result and faster to boot in some cases.)
    Example: 3 J moves the cursor down 3 lines.

Special Line Numbers

Special Line NumberFunction
0Equal to 1, refers to the first line
$The last line, equal to the absolute number of the last line
%All lines, equal to the range 1,$
. (period)The current line, equal to the line’s absolute number
(You can also use +0 or -0 to achieve the same in most cases)

How to See and Use Line Numbering in Vim: FAQ

Can I see both absolute and relative line numbers?

Yes.

You can turn on both number and relativenumber. Vim will then show the absolute number for the line hat holds the cursor and all other lines with numbers relative to it.

(How to see and use line numbering tested with Vim 9.1 and Neovim 0.9.5; first published April 2024)

Home » Useful Tech » How to See and Use Line Numbering in Vim