how to display line numbers when viewing a file in linux

While viewing files in Linux, especially text files it is often informative and useful to see the line numbers. Text based editors and utilities such as vi, nano, cat or less can all display line numbers with a command line argument or with an command with in the editor. Many text editors with a graphical interface will display line numbers by default, but you can always toggle it from the options to enable or disable it.

If you just want to find the total number of lines in a file, then you can do that without even opening and viewing the file in an editor. This post deals only the use case where you need to see the line content alongside the line number.

In Text Editors

Almost all text editors does have an option to display line numbers, most of them are turned on by default. We will just look at some of most the popular ones, couple of text based interfaces and a couple of graphical interfaces.

Vi Editor

Vi (or the Vim variation) is probably the most popular editor in Linux with a text user interface. To display the line numbers, first you need to enter the escape mode. You can do that by hitting the escape key at any time. In escape mode, enter following command

:set number


:se nu

The se nu is the shortcut for the original command, set number. This will display the line numbers in the left hand side margin of the editor.

Once you have enabled the display of the line numbers, you might want to disable the them later. You can remove the line numbers from being displayed by using the following command.

:se nonu


:se nu!

The above :se nu! can be used to both enable and disable the line numbers. It toggles the line number display depending on the current state.

Nano Editor

Nano is another basic text editor in Linux, with a text based user interface. It is probably not as feature rich as Vi, but is widely supported over many distributions. Being a very simple editor, Nano does not have the ability to display line numbers as Vi or other editors do. The best you can do is to display the line number of the current cursor position.

bash$ nano -c <filename>

The above command will start the nano editor with the option to constantly display the cursor position at the bottom of the editor. You can make this option permanent by modifying the nano config file, named .nanorc. Add the option set const to the file to persist.

If you already have the file open in an editor then you can use the keyboard shortcut Alt + C to toggle the option to display the cursor position.

Kate Editor

Kate is a text editor with a graphical user interface. It is the default text editor for KDE, and is quite feature rich.

You can toggle the line number display from the menu bar by going to View -> Show Line Numbers. Selecting that option will display the line numbers on the left hand side margin of the editor window. You can disable it by deselecting the same option.

You can also use the keyboard shortcut F11 to toggle this setting.

GEdit Editor

GEdit is another popular text editor, especially in Gnome desktop environment. Just as the Kate editor above, you can turn on line numbers from the menu bar. Click on the Edit option in the menu bar and select Preferences. This will open the Preferences dialog box.

In the dialog box, select the View  tab and then select the checkbox next to Display line numbers. This will enable the line numbers in the editor window.

Other Command Line Tools

Aside from using a text editor to view your text files, there are also some command line utilities that can be used to print the file to the console. It might sometimes be helpful to display the line numbers along side the printed file.


Even though it is mostly used for merging and concatenating files, it can also be used to display the file contents in the console. There are two command line options available in the cat command that allows you to display line numbers, –number (-n) and –number-nonblank (-b), according to your requirement.

bash$ cat -n file.txt

The option -n or –number  will print out the line numbers of all lines, including the blank or empty lines in the file. If you like to count and display line numbers only for the non-empty lines in the file, then use the -b or –number-nonblank option as shown below.

bash$ cat -b file.txt


Another useful utility is less which allows files to be displayed by screenful and makes it easier to scroll the file contents as well, unlike the cat command. The option to enable line numbering in less is -N or –LINE-NUMBERS.

bash$ less -N file.txt

The option -N can be used while viewing the file as well. Typing -N will toggle the line numbering feature of the displayed file which can be quite useful.

nl and more

The more command does not have a line number feature. You can however use the nl command to display the line numbers in the file and then pipe the output through more to achieve a similar functionality.

bash$ nl file.txt | more