36

Is there a way to turn on line numbering for nano?

1
24

The only thing coming close to what you want is option to display your current cursor position. You activate it by using --constantshow (manpage: Constantly show the cursor position) option or by pressing AltC on an open text file.

1
  • 5
    Good answer. The --const argument is the same as -c. Use it to open the file like this: nano -c filename . Also, to make it permanent, include the 'set const' option in your ~/.nanorc file, which may not exist until you create it. – MountainX Feb 2 '12 at 2:24
43

Adding set linenumbers in /etc/nanorc works for me in nano version 2.7.1.

It also can be activated with -l or --linenumbers on the command line.

Toggle the line numbers from within nano with: M-# in my case Alt+Shift+3.

4
  • 1
    Thanks for Alt+Shift+3 for toggling. – bob Jan 4 '19 at 2:51
  • In addition to change the background color of the line numbers, I use set numbercolor ,normal – smac89 Apr 5 '19 at 22:24
  • It may be preferable to store the set linenumbers in /home/<user>/.nanorc as a user preference instead. – topher217 Oct 22 '20 at 14:32
  • I like this but it should be noted that when you copy and paste multiple lines, the line numbers will also copy and paste. – mchid Mar 28 at 19:30
4

This was added on the 20th of October and was documented on the 22nd of October and has yet to make it into a release so compile it from source like so:

git clone git://git.savannah.gnu.org/nano.git;cd nano;./autogen.sh;./configure;sudo make install 

Then add the following to your .nanorc:

set linenumbers

You can use ^# to turn line numbers on and off from within Nano. It's probably a bit buggy since it's recent and hasn't been tested by many people.

5
  • A good answer would have pointed out that the feature was added within the past day, and has no user experience to relate to. – Thomas Dickey Oct 22 '16 at 19:03
  • What do you mean by "has no user experience to relate to"? – 0x777C Oct 22 '16 at 21:57
  • 1
    There cannot be more than 1-2 users of the feature at this point in time, there are no bug reports. By the way, "recently" demands a date (or link to the commit making the change). – Thomas Dickey Oct 22 '16 at 22:06
  • I don't see how that is in any way related to the question. – 0x777C Oct 23 '16 at 8:02
  • 1
    You got the date wrong (see commit October 20 by Faissal Bensefia). It also was undocumented until mid-day October 22. All other points raised are still issues. – Thomas Dickey Oct 23 '16 at 10:18
2

Nano is by design a very simple editor with few features. If you start wishing for anything beyond basic edition, nano isn't the right tool. Emacs is a very powerful editor; to switch line numbers on, type M-x linum-mode. If Emacs scares you and you want a text mode editor, consider Joe, where line numbers are switched on with the -linums option.

That being said, line numbering was eventually added five years after this question was asked.

5
  • So.. Emacs is your text editor of choice? – Pacerier Aug 9 '16 at 14:33
  • "how to do X in nano" --> "do it in Emacs" is not a very good answer – Purefan May 27 '20 at 6:57
  • @Purefan If you can't do it in nano, the only possible answers are “don't do it at all” and “do it in this other program”. – Gilles 'SO- stop being evil' May 27 '20 at 7:06
  • but it is possible to do it in nano (as mentioned in the accepted answer), and even if it were not possible, I would argue that a better answer is "its not possible in nano", if you want to suggest how to do it in your favorite editor I have nothing against it, but the question specifically refers to nano – Purefan May 27 '20 at 7:46
  • 1
    @Purefan It wasn't possible back when I posted my answer. It became possible a few years later. – Gilles 'SO- stop being evil' May 27 '20 at 8:19
2

It's 2021. The OP's question is still valid, but many of the answers here are for an older version of nano. I'm not presenting this answer as "the last word" - only as an update.

The default screen of nano consists of five areas. From top to bottom these are: the title bar, a blank line, the edit window, the status bar, and two help lines.

Where to display line numbers?

Line numbers may be displayed in one of two places:

  1. the edit window
  2. the status bar

The status bar display simply updates the line number (and column) of the cursor/insertion point as it's moved about in the edit window. Line numbers in the edit window are positioned in the left margin. It is possible to display the line number in either or both the edit window and the status bar.

Display line numbers in the edit window:

There are several methods (this is not necessarily a complete list):

1. before the file is opened:

Edit/create the file ~/.nanorc with the following line:

set linenumbers

2. when the file is opened:

Use the -l option in nano:

$ nano -l <myfilename>

3. after the file is opened:

Toggle line numbers "on" and "off" w/ alt-shift-#:

altshift#

Display line numbers in the status bar:

1. before the file is opened:

Edit/create the file ~/.nanorc with the following line:

set constantshow

2. when the file is opened:

Use the -c option in nano:

$ nano -c <myfilename>

3. after the file is opened:

Toggle line number display in the status bar "on" and "off" w/ alt-shift-C:

altshiftC

Summary

These all work as of today: Ubuntu 20.04, nano --version = GNU nano, version 4.8, although there are minor discrepancies in the documentation.

2
  • 1
    It should be noted that if you copy and paste multiple lines when line numbers are displayed on the edit window, the numbers for the lines will also copy and paste. – mchid Mar 28 at 19:47
  • That may be a function of a. how you do the copy, and b. which version of nano. I don't get the line numbers unless I use my macOS' copy & paste from nano to the text edit app. I don't have time to test all the permutations now, but I will say that if nano's copy & paste (i.e. all within nano) includes the line numbers, then that seems pretty weak - to my way of thinking. – Seamus Mar 29 at 2:17
0

Alternative, a quick alias in .bashrc:

echo "alias nano='nano -c -l'" >> ~/.bashrc && source ~/.bashrc && reset

Forcing the syntax seems mandatory for hashbang scripts, so to force a particular highlighting:

echo "alias nano='nano --syntax=php -c -l'" >> ~/.bashrc && source ~/.bashrc && reset

man nano

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.