Vim: Which Line Am I On? Which Column Am I In?

Vim is a powerful beast of a text editor, but you would know that from its’ spartan screen. Sometimes it’s a good thing, but sometimes you need to know where you are. Fear not the answers are here.

Which Line Am I On?

The following Vim lesson is an excerpt from “Vim and Vi Tips.” You can get in PDF in a couple of minutes!

If you ever need to know the current line number, try these commands:

  • Display the line number:
    type :#, :num, :.=, or press Ctrl+G.
  • Display the number of lines in the current file:
    type :=
  • Display the line number and the total number of lines:
    press Ctrl+g.
  • Display the number of the next line that matches a regular expression:
    type :/regex/=

The search will start on the line with the cursor and continue towards the end of the current file.

How-To:

  1. Open exercise file: vi line-number.txt
  2. Type 4G to move the cursor to line 4.
  3. Type :num
  4. Press Enter/Return.

If you would like to display line numbers without having to check them using the commands described in this section, you need to set the number option:

How-To:

  1. Press Esc to switch to command mode.
  2. Type :set number
  3. Press Enter/Return.

If you want to reclaim the space taken by the line numbers, you need to set the nonumber option:

How-To:

  1. Press Esc to switch to command mode.
  2. Type :set nonumber
  3. Press Enter/Return.
  • You must switch vim to command mode to use the commands described in this section.

Which Column Am I In?

If you would like to display a continously updated location of the text cursor, the line number and the column number, you need to set the ruler option:

How-To:

  1. Press Esc to switch to command mode.
  2. Type :set ruler
  3. Press Enter/Return.

The location of the cursor will be displayed in the right half of the bottom of the vim screen.

Did you enjoy this short Vim lesson? There are plenty more practical Vim tips in Vim and Vi Tips! You can get in PDF in a couple of minutes!

Buy Now