How to show the Line Numbers in the Nano text editor

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
5 min

banner

# Table of Contents

  1. How to show the Line Numbers in the Nano text editor
  2. Show the Line Numbers in Nano using a keyboard shortcut
  3. Show the Line Numbers in Nano using your global config file
  4. Show the current line number in Nano using a keyboard shortcut
  5. Show the current line number using a command line flag
  6. Show the current line number using your global config file
  7. A note on using nano with the sudo prefix
  8. Useful nano keyboard shortcuts

# How to show the Line Numbers in the Nano text editor

The easiest way to show the line numbers in the nano text editor is to use the --linenumbers flag.

Issue the command nano --linenumbers myfile.txt to open the file with line numbers shown.

shell
nano --linenumbers example.txt

show line numbers using linenumbers flag

The command above assumes that you are trying to open a file named example.txt with the line numbers shown.

Here is what the result of issuing the command looks like.

nano showing line numbers

The --linenumbers (or -l) flag displays the line numbers to the left of the text area as shown in the following clip.

nano show line numbers

You can also use the -l shorthand flag to achieve the same result.

shell
nano -l example.txt

using l shorthand flag to show line numbers

Make sure to replace example.txt with the name of your actual file.

# Show the Line Numbers in Nano using a keyboard shortcut

You can also use a keyboard shortcut to show the line numbers in the nano editor.

Press Alt + Shift + 3 (or Alt + #) to show or hide the line numbers.

show hide line numbers using keyboard shortcut

You can also use the Alt + n keyboard shortcut to show or hide the line numbers.

The keyboard shortcut toggles the display of line numbers in front of the text.

# Show the Line Numbers in Nano using your global config file

You can also enable line numbering in nano by using your global config file located under ~/.nanorc.

Issue the following command to open the file.

shell
nano ~/.nanorc

using l shorthand flag to show line numbers

The file might be empty if you haven't previously edited it.

Add the following line to show the line numbers.

~/.nanorc
set linenumbers

set line numbers in nano config

Save the file and restart your terminal.

Once you enable the setting, restart your terminal and issue the nano myfile.txt command.

shell
nano example.txt

Now any file you open will have the line numbers displayed.

show line numbers using global configuration file

# Show the current line number in Nano using a keyboard shortcut

You can also show the current line number in nano by using the Ctrl + C keyboard shortcut.

nano show current line number

You can also use the Alt + Shift + C keyboard shortcut to toggle the display of the current line number after opening a file.

# Show the current line number using a command line flag

You can also enable showing the current line number with the -c command line flag.

shell
nano -c example.txt

show current line number using flag

The longer form of the flag is −−constantshow.

shell
nano −−constantshow example.txt

Here is a short clip that demonstrates how this works.

nano show current line number using flag

# Show the current line number using your global config file

You can also use your global ~/.nanorc configuration file to show the current line number.

Issue the following command to open the file.

shell
nano ~/.nanorc

using l shorthand flag to show line numbers

The file might be empty if you haven't previously edited it.

Add the following line to show the current line number.

shell
set constantshow

show current line number using global config file

Save the file and restart your terminal.

Issue the nano myfile.txt command.

shell
nano example.txt

current line number shown in nano

Now the current line number will always be displayed at the bottom of the terminal window.

# A note on using nano with the sudo prefix

If you use the nano command with the sudo prefix and have difficulties showing the line numbers, set the -l or --linenumbers flag when opening the file.

shell
sudo nano --linenumbers example.txt

The command above assumes that you are trying to open a file named example.txt with the line numbers shown.

When you use the sudo prefix, you are issuing the nano command as a super user.

The line numbers might not be shown if you only edited your non-root ~/.nanorc file to enable the linenumbers setting.

You can also show line numbers for the root user via the global /root/.nanorc config file.

Assuming your ~/.nanorc file has the linenumbers setting enabled.

~/.nanorc
set linenumbers

set line numbers in nano config

You can copy and paste the file to /root/.nanorc by issuing the following command.

shell
sudo cp ~/.nanorc /root/

You can use the cat command to verify the /root/.nanorc file has been copied successfully.

shell
sudo cat /root/.nanorc

Your /root/.nanorc file should contain the following line.

/root/.nanorc
set linenumbers

Once set, you should be able to issue the sudo nano myfile.txt command with line numbers enabled.

shell
sudo nano example.txt

# Useful nano keyboard shortcuts

Here is a table containing some of the most useful nano keyboard shortcuts.

Keyboard shortcutDescription
Ctrl + SSave the current file.
Ctrl + XClose the buffer and exit from nano
Ctrl + CCancel or report the current cursor position
Alt + UUndo last action.
Alt + ERedo last undone action.
Ctrl + KCut the current line into the cut buffer.
Alt + 6Copy the current line into the cut buffer.
Ctrl + UPaste the contents of the cut buffer.
Alt + DelDelete the current line.

I've also written an article on:

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.