How to Show Line Numbers in Nano Text Editor in Linux

Nano is a lightweight, user-friendly text editor available on most Unix-based systems. While it provides essential features like syntax highlighting and search functionality, it does not show line numbers by default. Enabling line numbers in Nano can be particularly helpful when referencing specific lines in a file or debugging code.

Follow How to Install Nano text editor in Linux if it’s not installed.

There are three methods to show line numbers in Nano-


Table of Contents

  1. Method 1: Use Command-Line Option
  2. Method 2: Edit Nano Configuration File
  3. Method 3: Use Keyboard Shortcut

Method 1: Use Command-Line Option

The quickest way to enable line numbers in Nano is to use the -l or --linenumbers option when opening a file.

1. Open your terminal and view the content of the file using the cat command

2. To open the file in Nano with line numbers, use the -l option. Alternatively, you can use the long-form option:

or

3. Nano will display the file with line numbers on the left margin.

Example:

This method is ideal if you want to enable line numbers for a specific editing session temporarily.


Method 2: Edit Nano Configuration File

To enable line numbers permanently, you can modify the Nano configuration file.

1. Open the Nano configuration file (nanorc) in Nano:

2. Add the following line to the configuration file:

set linenumbers

3. Save and exit the file by pressing Ctrl + O, then Enter, and Ctrl + X.

4. pen any file in Nano, and you will see line numbers displayed by default:

Example Output:

This method ensures line numbers are always enabled, saving you time and effort.


Method 3: Use Keyboard Shortcut

Nano also allows you to toggle line numbers on and off during an editing session using a simple keyboard shortcut.

1. Open any file in Nano:

2. Press Alt + Shift + 3 to toggle line numbers on or off.

3. Line numbers will immediately appear on the left margin. Press the shortcut again to hide them.

Tip: This method is useful if you only occasionally need line numbers or prefer to keep the editor uncluttered most of the time.