Keep line numbers and syntax highlighting in GNU nano across sessions
Add set linenumbers and syntax includes to ~/.config/nano/nanorc so GNU nano shows line numbers and colors in every new session.
06 Dec 2025, 06:01 UTC

The useful answer
If GNU nano starts without line numbers or color highlighting, the setting is not saved in the file you edit. It belongs in nano's user configuration file. Create or edit ~/.config/nano/nanorc and add display options plus syntax includes. The changes apply to new nano sessions; the file on disk is not modified by highlighting.
Worked configuration
set linenumbers
set tabsize 4
set tabstospaces
include "/usr/share/nano/python.nanorc"
include "/usr/share/nano/sh.nanorc"
Run this in a normal user terminal, not as root:
$ mkdir -p ~/.config/nano
Then open ~/.config/nano/nanorc with nano or another editor, paste the lines above, save, and exit. No sudo is required because this is a user file in your home directory.
How the mechanism works
When nano starts, it reads its user nanorc if one exists. set linenumbers tells nano to draw a number column on the left of the buffer. include loads syntax definition files that contain rules for matching file names or the first lines of a file. When a match is found, nano applies color rules to the text in memory only. The colors exist inside the editor buffer; they are not stored in the file and do not travel with it when you open it elsewhere.
Syntax files are usually provided by the nano package. On many Linux distributions they live under /usr/share/nano/, but the exact path and file names depend on the distribution and nano version.
Check before you include
- Confirm the installed editor and version:
$ nano -V
This command runs in your terminal as a regular user. It prints version information; there is no expected numeric value to hard-code here because versions differ by system.
- List available syntax files so you include paths that actually exist:
$ ls /usr/share/nano/*.nanorc
If that directory is empty or missing, adjust the include paths in your nanorc to match your system. Do not include a path that does not exist; nano may warn at startup or ignore the line.
Limits and common mistakes
| Mistake | What happens | Correction |
|---|---|---|
Editing /etc/nanorc instead of the user file | Requires root and affects all users; your personal setting may still be overridden by ~/.config/nano/nanorc. | Use the user file under ~/.config/nano/ unless you administer a multi-user system and intend a system-wide default. |
| Expecting highlighting from a file extension alone | Nano only highlights when an installed syntax rule matches the name or content. | Verify that a matching .nanorc file exists and is included. |
| Placing options after includes | Later settings can reset or override earlier ones, depending on the nano version. | Put set options before include lines, or test the order on your system. |
Using ~/.nanorc on an XDG system | Newer nano and XDG setups prefer ~/.config/nano/nanorc; the older file may be ignored or read with different precedence. | Check man nanorc for your version's search order. |
Other limits:
- Highlighting is nano-only. Opening the same file in vim, less, or cat shows no colors.
- Very large files may redraw more slowly with line numbers and syntax rules active.
- Buffers already open when you save nanorc are not re-highlighted until you close and reopen them.
- Changes do not alter file contents. If you open the file with
cat, you should see plain text with no ANSI escape sequences added by nano.
Practical verification
- Open a file that matches an included syntax, for example a Python script:
$ nano example.py
Look for line numbers in the left margin and color differences between keywords, strings, and comments. If nothing changes, confirm that /usr/share/nano/python.nanorc exists and that its path is spelled correctly in your nanorc.
- Confirm the file on disk is unchanged:
$ cat example.py
The output should be plain source code without color codes.
- Reopen nano to verify persistence:
$ nano example.py
Line numbers and highlighting should return in the new session.
Rollback
Editing nanorc changes only your user configuration. To undo, open ~/.config/nano/nanorc, remove or comment out the lines you added, save, and exit. New nano sessions will start without those options. No system packages are modified.
If a syntax include causes startup warnings, remove that include line first while keeping set linenumbers. You can add syntax files again after confirming the correct path with ls /usr/share/nano/.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.