According to the man page for GNU nano 2.9.3 (also verified on version 4.8) , you have two options for automatically opening with line-numbers shown and one for toggling them on/off once nano is open:
Command Line Flags
You can use the -l or --linenumbers flags.
-l, --linenumbers
Display line numbers to the left of the text area.
e.g. nano -l foo.txt nano --linenumbers foo.txt
Via the config file(s)
Alternatively, according to the man page for nanorc, you can add set linenumbers in ~/.nanorc, $XDG_CONFIG_HOME/nano/nanorc or ~/.config/nano/nanorc.
INITIALIZATIONFILEnano will read two configuration files: first the system's nanorc (if
it exists), and then the user's nanorc (if it exists), either ~/.nanorc
or $XDG_CONFIG_HOME/nano/nanorc or ~/.config/nano/nanorc, whichever is
encountered first. See nanorc(5) for more information on the possible
contents of those files.
setlinenumbers
Display line numbers to the left of the text area.
So the contents of my ~/.nanorc file are simply:
1 set linenumbers
2
Via a key bindings
Not exactly what the OP was asking for, but for completeness you can also toggle line number on/off via the default keybinding (as of v4.8) of Alt+N.
Alternatively you can rebind this key via the man file mentioned method:
REBINDINGKEYS
Key bindings can be changed via the following three commands:
bind key function menu
Rebinds the given key to the given function in the given menu (or in all menus where the function exists when all is used).
The format of key should be one of:
^X where X is a Latin letter, or one of several ASCII characters (@, ], \, ^, _), or the word "Space". Example: ^C.
M-X where X is any ASCII character except [, or the word "Space". Example: M-8.
Sh-M-X where X is a Latin letter. Example: Sh-M-U. By default, each Meta+letter keystroke does the same as the corresponding Shift+Meta+letter. But when any Shift+Meta bind is made, that will
no longer be the case, for all letters.
FN where N is a numeric value from 1 to 24. Example: F10. (Often, F13 to F24 can be typed as F1 to F12 with Shift.)
Ins or Del.
Rebinding ^M (Enter) or ^I (Tab) is probably not a good idea. On some terminals it's not possible to rebind ^H (unless --raw is used) because its keycode is identical to that of the Backspace key.
Valid function names to be bound are:
...
linenumbers
Toggles the display of line numbers in front of the text.
Regarding alternate config files:
In response to comments about alternate config files, from the description of man nanorc:
During startup, nano will first
read the system-wide settings, from /etc/nanorc (the exact path might be different on your
system), and then the user-specific settings, either from ~/.nanorc or from $XDG_CON‐
FIG_HOME/nano/nanorc or from ~/.config/nano/nanorc, whichever is encountered first.
I would not typically advise others to change the system-wide config file, unless you have a good reason to do so, as it will likely be overwritten during any updates, and is not likely to be included in system backups, which typically only include the home directory.
Using with sudo
As noted by a commenter, this may not work as expected if you have to use sudo with nano, e.g. sudo nano myFile. This is because when you use sudo you are doing whatever comes after sudo as the "super user", thus the name (super user do ..). If you edited your non-super user config file (e.g. ~/.nanorc or /home/myUserName/.nanorc) this will not be run when you use sudo as you are not running nano as myUserName any longer. Thus, you have a few options.
The simplest is just to use the -l flag and it will work as is.
Copy the contents of your ~/.nanorc to /root/.nanorc as this represents the home directory for the super user. Assuming you have your ./nanorc file setup as you like, and you don't already have a /root/.nanorc file then you can just run sudo cp /home/myUserName/.nanorc /root/. Note this will overwrite any existing nanorc file in /root and you must replace myUserName with your actual user name. I didn't test, but I'd guess you can't use the tilde operator, i.e ``sudo cp ~/.nanorc /root/as it would likely interperet this to be/root` rather than your username as again, you're telling it "super user do cp from home directory".
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:
the edit window
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):
Before the file is opened:
Edit/create the file ~/.nanorc with the following line:
set linenumbers
When the file is opened:
Use the -l option in nano:
$ nano -l <myfilename>
After the file is opened:
Toggle line numbers "on" and "off" w/ alt-shift-#:
altshift#
Display line numbers in the status bar
Before the file is opened:
Edit/create the file ~/.nanorc with the following line:
set constantshow
When the file is opened:
Use the -c option in nano:
$ nano -c <myfilename>
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.
The keyboard combination to display the current line number whilst you are using nano is CTRL+C.
Alternatively, to display the line & column number position you could just use the
-c
parameter when launching nano i.e:To make this permanent,
nano
uses a configuration in your home folder~/.nanorc
Thus to display line numbers always when using nano...
(don't worry if its empty - this file doesn't exist by default)
type
set constantshow
N.B. the deprecated syntax
set const
is shown in the animationSave
Since you are using line numbers remember you can use ALT+G to jump to a specific line number.
Accidentally found nice shortcut: Alt+#, which in some keyboard layouts can be done with Alt+Shift+3.
According to the man page for GNU nano 2.9.3 (also verified on version 4.8) , you have two options for automatically opening with line-numbers shown and one for toggling them on/off once nano is open:
Command Line Flags
You can use the
-l
or--linenumbers
flags.e.g.
nano -l foo.txt
nano --linenumbers foo.txt
Via the config file(s)
Alternatively, according to the man page for nanorc, you can add
set linenumbers
in~/.nanorc
,$XDG_CONFIG_HOME/nano/nanorc
or~/.config/nano/nanorc
.So the contents of my ~/.nanorc file are simply:
Via a key bindings
Not exactly what the OP was asking for, but for completeness you can also toggle line number on/off via the default keybinding (as of v4.8) of
Alt+N
.Alternatively you can rebind this key via the man file mentioned method:
Regarding alternate config files:
In response to comments about alternate config files, from the description of
man nanorc
:I would not typically advise others to change the system-wide config file, unless you have a good reason to do so, as it will likely be overwritten during any updates, and is not likely to be included in system backups, which typically only include the home directory.
Using with sudo
As noted by a commenter, this may not work as expected if you have to use sudo with nano, e.g.
sudo nano myFile
. This is because when you use sudo you are doing whatever comes aftersudo
as the "super user", thus the name (super user do ..). If you edited your non-super user config file (e.g.~/.nanorc
or/home/myUserName/.nanorc
) this will not be run when you use sudo as you are not running nano asmyUserName
any longer. Thus, you have a few options.-l
flag and it will work as is.~/.nanorc
to/root/.nanorc
as this represents the home directory for the super user. Assuming you have your./nanorc
file setup as you like, and you don't already have a/root/.nanorc
file then you can just runsudo cp /home/myUserName/.nanorc /root/
. Note this will overwrite any existing nanorc file in/root
and you must replace myUserName with your actual user name. I didn't test, but I'd guess you can't use the tilde operator, i.e ``sudo cp ~/.nanorc /root/as it would likely interperet this to be
/root` rather than your username as again, you're telling it "super user do cp from home directory".Compile Nano from source:
Then add the following to your
.nanorc
file:You can use Meta+# to turn line numbers on and off from within Nano.
If
nano -c filename
does not work, usenano filename
then Ctrl +_. It will ask you for the line number to go to.If you have already opened the file with nano you press
(not simultaneusly, press control and w, then without letting go control and letting go w, press t)
This command will require column and line, you enter them this way:
and you will end up in line 10 character 23.
If you are at the top of the file and you only want to find the line:
If you are at a acertain line and you want to find the column:
If you haven't opened the file yet, you can do this:
and the file will open with cursor in the line 10, chanracter 23, so you can try also:
nano +,23 file (enter)
(Note that this will send you to the character 23 of the first line only)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.Where to display line numbers?
Line numbers may be displayed in one of two places:
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):
Before the file is opened:
Edit/create the file
~/.nanorc
with the following line:When the file is opened:
Use the
-l
option innano
:After the file is opened:
Toggle line numbers "on" and "off" w/
alt-shift-#
:altshift#
Display line numbers in the status bar
Before the file is opened:
Edit/create the file
~/.nanorc
with the following line:When the file is opened:
Use the
-c
option innano
: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.