Sometimes, we might want to show line numbers when viewing a file in a text editor. For example, this task becomes essential when we want to improve navigation and references when editing a file. Since nano is a fairly ubiquitous text editor, we use it as an example.
In this tutorial, we’ll explore different methods for displaying line numbers within nano in a Ubuntu 22.04 installation.
Specifically, we’ll use shortcut keys such as ALT+C, CTRL+C, ALT+Shift+#, and ALT+N, and command line options like -c and -l. Furthermore, we’ll edit the ~./nanorc file to add the line numbers by default for all files in nano.
2. Using Shortcut Keys
In nano, key combinations or shortcut keys enable us to add line numbers when viewing a file in two different ways.
The first method displays the current line number at the bottom of the nano editor when we move up or down in the file using the Arrow keys. On the other hand, the second method shows line numbers in the column format on the left side of nano.
Now, let’s check some shortcut keys to add line numbers in nano.
The ALT+C combination displays the current line number at the bottom of the nano editor. Conversely, we can use it to hide the line numbers if already visible.
To use the ALT+C shortcut key, first, we open a sample file in nano:
$ sudo nano test.sh
Now, we press ALT+C and check the result:
Thus, we can see the current line number out of the total number of lines of the test.sh script in nano. In this case, we’re at line 1 out of 28 total lines (1/28).
Importantly, the information bar stays at the bottom until we disable it again via ALT+C or restart nano. While it’s on the screen, the information updates as we move the cursor.
Similarly, CTRL+C is another key combination used to perform the same function of displaying line numbers at the bottom of nano.
Now, in the same test.sh script, we press CTRL+C for testing:
As a result, we can see the line number in the same format that ALT+C enabled.
However, unlike ALT+C, the CTRL+C hotkey only shows a snapshot and doesn’t change when we move the cursor. Further, the bar disappears after a while.
On the other hand, we can use the ALT+N shortcut key to display all the line numbers at once in nano.
Now, let’s press ALT+N to display the line numbers in our test.sh script:
Consequently, we can see the line numbers of the test.sh in the column format on the left side of the window.
Likewise, we can use the ALT+SHIFT+# shortcut key to toggle the display of line numbers in nano.
For testing, we now press ALT+SHIFT+# in the currently opened test.sh script:
Thus, we can see the line numbers in the same format that ALT+N activated. In fact, both shortcuts are synonyms of each other.
3. Using the -c Option
Besides the shortcut keys, nano also supports different command line options to display line numbers consistently in a file.
For instance, we can use the -c option, shorthand for –-constantshow, to make sure that the line numbers remain visible throughout the session.
To do so, we add the -c option in the nano command to open the test.sh script:
$ sudo nano -c test.sh
Let’s check the result:
Consequently, we can see a constant line number displayed at the bottom of the window. This is equivalent to starting nano without flags and using the ALT+C toggle.
4. Using the -l Option
The -l option, shorthand for –linenumbers, is another command line option that enables nano to display line numbers but this time in the column format.
So, let’s open the same test.sh script in nano with the -l option:
$ sudo nano -l test.sh
The result is as expected:
Consequently, we can see the line numbers in the column format on the left side. Again, this is equivalent to the ALT+SHIFT+# and ALT+N shortcuts.
5. Editing the ~./nanorc File
In Linux, the nanorc file enables us to permanently customize the behavior of the nano editor. Moreover, we can use it to display line numbers by default when nano opens a file.
For this, we first open the ~./nanorc file in nano for editing:
$ sudo nano ~./nanorc
Then, we add the set linenumbers statement to the ~./nanorc file:
After adding the code, we press CTRL+O and Return to save the ~./nanorc file and CTRL+X to exit the nano editor:
Next, we copy the configuration to the HOME directory of the root user via cp:
$ sudo cp ~/.nanorc /root/
Finally, we open the test.sh script in nano for testing the applied changes:
$ sudo nano test.sh
Let’s check the result:
Consequently, the changes reflect the line number format in nano.
In this article, we learned different methods to show the line numbers of a file in nano.
We saw that key combinations such as ALT+C, CTRL+C, ALT+Shift+#, and ALT+N enable the line number display after opening a file. On the other hand, we use the -c or the -l nano options to activate similar options for a given file directly from the command line.
Further, we can edit the ~./nanorc for customizing nano behavior to display line numbers by default for all files.
Ultimately, we can select any of these methods as per our requirements of adding line numbers to a file in nano.