Tuesday, April 16, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  104] [ 0]  / answers: 1 / hits: 753855  / 2 Years ago, wed, april 13, 2022, 1:11:59

Is there a way to tell nano to automatically show line numbering each time I open a file?


More From » nano

 Answers
1

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.


INITIALIZATION FILE
nano 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.

       set linenumbers
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:


REBINDING KEYS
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.



  1. The simplest is just to use the -l flag and it will work as is.

  2. 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".


[#42467] Thursday, April 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
singerrupt

Total Points: 435
Total Questions: 111
Total Answers: 109

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
singerrupt questions
;