
Linux command: nano
Something is broken on a server, you are connected over SSH, and one line in a configuration file needs to change. This is the moment where a lot of people freeze, because the editor that opens is either an editor they cannot leave or an editor they have never seen. nano is the answer to both problems: it is already installed on nearly every Linux system, it prints its own instructions along the bottom of the screen, and you can walk away from it without searching the internet for how to quit.
1. The Basics
The job of nano is to let you edit a text file in a terminal without learning anything first. You give it a filename, you type, and what you type appears. That sounds too obvious to mention until you remember that the other classic Unix editor, vi, throws away your keystrokes as commands until you tell it otherwise.
$ nano notes.txt
Here is what that actually looks like. This is a real screen, not a drawing:
GNU nano 7.2 notes.txt
Dear reader,
This is a small file opened in nano.
The third line is here.
[ Read 4 lines ]
^G Help ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line
The screen has four parts, and knowing their names makes every later instruction easier to follow:
- The title bar at the top shows the nano version and the filename.
- The editing window in the middle holds your file, or as nano calls it internally, the buffer.
- The status line third from the bottom shows messages such as
[ Read 4 lines ]. - The two shortcut rows at the bottom are nano's most important feature. The
^means the Control key, so^Xis Ctrl+X.
nano is modeless. There is no command mode to enter or escape from. The arrow keys move the cursor, Backspace deletes, and typing inserts. Everything else is a Control or Alt combination, and the six most common ones are printed on the screen at all times.
The editor that tells you how to use it. It looks like the simplest program on the system, and underneath sits a configuration file, syntax highlighting, undo, and a set of defaults that changed in a way most tutorials never caught up with.
This article starts by opening a file and getting back out again, then works through searching, undo, the config file, syntax colouring, and the handful of behaviours that surprise even people who have used nano for years.
Back to topThe right mental model:
nanoputs its own manual on the screen. Those two bottom rows are not decoration and not a beginner's crutch, they are the interface. If you can read them, you already know how to use the editor.
2. Where the Name Comes From
Ask nano what its name means and it tells you, in the first line of its own manual page:
$ man nano
NAME
nano - Nano's ANOther editor, inspired by Pico
That is a recursive joke in the GNU tradition, the same style as GNU standing for "GNU's Not Unix". The name expands into a phrase that contains the name.
It was not called nano at first. Chris Allegretta wrote the original in late 1999 and called it TIP, short for Tip Isn't Pico, because it was a clone of an editor called Pico. That name lasted only a few months. There was already a Unix program called tip, an old tool that "establishes a full duplex terminal connection to a remote host", which shipped with many older Unix systems and with Solaris. Two programs cannot own the same command name, so in January 2000, at version 0.7.4, TIP became nano. The project's own release notes record the moment:
2000.01.09 - As of this version (0.7.4), TIP has officially
been renamed to nano.
The new name carries a second joke that is easy to miss. Both pico and nano are SI prefixes for very small numbers, but they are not the same size: pico means a trillionth and nano means a billionth. A nano is a thousand times larger than a pico. For a clone that set out to keep Pico's interface while adding the features it lacked, that is exactly the right name.
The GNU in front came later, when nano was adopted as an official GNU package. That is why the title bar says GNU nano and why the program is licensed under the GPL, which turns out to be the whole reason it exists at all.
3. A Short History
nano exists because of a licence, not because of a missing feature.
In the 1990s, the way most people read email on a Unix system was a program from the University of Washington called Pine. The part of Pine you used to write your message was a small, friendly editor called Pico, and it was many people's first text editor. It was pleasant, it showed its shortcuts at the bottom of the screen, and beginners loved it.
The problem was how it was licensed. The University of Washington placed restrictions on distributing modified versions, which meant Pine and Pico did not meet the Free Software Foundation's or Debian's definition of free software. Distributions would not ship them. People who liked the editor could not get it through their package manager. nano's own FAQ, which ships with the program, describes what happened next:
It was in late 1999 when Chris Allegretta (our hero) was yet again
complaining to himself about the less-than-perfect license Pico was
distributed under, the 1000 makefiles that came with it and how just a
few small improvements could make it the Best Editor in the World (TM).
...
Finally something snapped inside and Chris coded and hacked like a
madman for many hours straight one weekend to make a (barely usable)
Pico clone.
That weekend produced something that could not even save files. Twenty-five years later it is the default editor on most Linux distributions.
| Era | Milestone |
|---|---|
| 1990s | Pico, the editor inside the Pine email client, teaches a generation its first shortcuts |
| late 1999 | Chris Allegretta writes a GPL clone in a weekend and calls it TIP (Tip Isn't Pico) |
| January 2000 | Version 0.7.4 renames TIP to nano, to avoid the existing tip command |
| 2001 onward | nano becomes an official GNU package, licensed under the GPL |
| 2006 | nano 2.0 brings UTF-8 support, go-to-line-and-column, and better syntax colouring |
| 2019 | nano 4.0 changes the defaults: no more automatic line breaking, smooth scrolling becomes standard |
| Today | Maintained by Benno Schulenberg; nano 7.x ships nearly everywhere and is Debian and Ubuntu's default editor |
You can check which version you have, and the answer matters more than usual because of that 2019 row:
$ nano --version
GNU nano, version 7.2
(C) 2023 the Free Software Foundation and various contributors
Compiled options: --disable-libmagic --enable-utf8
Section 7 explains why nano 4.0 is the dividing line, and why a great deal of advice you will find online is now describing an editor that no longer behaves that way.
Back to top4. Simple Use Cases
4.1 Opening a File
Give nano a filename. If the file exists it opens; if it does not, nano starts an empty buffer and creates the file when you save.
$ nano /etc/hosts # open an existing file
$ nano newfile.txt # start a new one
Where it is missing, it is a small package and every distribution has it:
$ nano --version # is it here at all?
$ sudo apt install nano # Debian, Ubuntu
$ sudo dnf install nano # Fedora, RHEL
$ sudo pacman -S nano # Arch
$ apk add nano # Alpine
That last line is there because of a machine on my own desk. I put Alpine Linux on an old thin client with an 8 GB SSD, because a normal Ubuntu install simply would not fit, and then found there was no nano to edit anything with. Alpine ships BusyBox instead, where vi is a symlink to /bin/busybox and there is no second editor at all. One command fixed it:
$ apk update && apk add nano
This is worth knowing even if you never touch a thin client, because Alpine is the usual base image for Docker containers. Open a shell inside one and you meet the same empty toolbox. The install is not heavy either: on Alpine nano is about 300 KB, or roughly 0.7 MB once its ncurses dependencies come with it, which is a reasonable trade even on a very small disk.
Run nano with no filename at all and you get an empty buffer with a friendly greeting:
GNU nano 7.2 New Buffer
[ Welcome to nano. For basic help, type Ctrl+G. ]
^G Help ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line
You can also jump straight to a line, which is useful when a program has just told you that something is wrong on line 47:
$ nano +47 /etc/apache2/apache2.conf # open with the cursor on line 47
$ nano +47,10 file.txt # line 47, column 10
$ nano +/ServerName /etc/apache2/apache2.conf # jump to the first match instead
4.2 Saving Your Work
The shortcut row calls saving Write Out, which is ^O. Press it and nano asks where to write, offering the current name:
File Name to Write: notes.txt
^G Help M-D DOS Format M-A Append M-B Backup File
^C Cancel M-M Mac Format M-P Prepend ^T Browse
Press Enter to accept the name and the file is written. Type a different name first and you have saved a copy instead, which is the quickest way to keep a backup before you change something risky.
Modern nano also has a plain save on ^S, the same key every graphical program uses. It writes the file immediately with no prompt at all:
[ Wrote 1 line ]
Once a buffer has unsaved changes, the title bar marks it with an asterisk, so you can always tell at a glance:
GNU nano 7.2 notes.txt *
4.3 Getting Out Again
This is the question that sends people to a search engine, and in nano the answer is written on the screen: ^X for Exit. If you have not changed anything, nano simply closes. If you have, it asks first:
Save modified buffer?
Y Yes
N No ^C Cancel
That prompt is safe to answer honestly, because of how nano works. Opening a file does not lock or change it. nano reads a copy into memory, the buffer, and every keystroke edits the copy. The file on disk is untouched until you save, which is why answering N genuinely throws away only your changes and leaves the original exactly as it was.
Three keys, three outcomes, and none of them can catch you out:
Ysaves and then asks for the filename, exactly like^O.Nthrows your changes away and quits.^Ccancels and puts you back in the file.
That prompt is the single biggest reason to reach for nano when you are tired or under pressure at two in the morning. It is very hard to lose work by accident.
4.4 Finding Things
Searching is ^W, labelled Where Is. Type your text and press Enter, and nano jumps to the next match. Press ^W then Enter again to repeat the same search.
Search:
^G Help M-C Case Sens M-B Backwards ^P Older ^T Go To Line
^C Cancel M-R Reg.exp. ^R Replace ^N Newer
Look at that row of options, because it answers questions people usually assume nano cannot handle. M-C makes the search case sensitive, M-B searches backwards, and M-R switches to regular expressions. Search and replace is ^\, or ^R from inside the search prompt.
4.5 Cut and Paste
The one shortcut worth memorising immediately is ^K, which cuts the whole current line. Press it several times in a row and the lines pile up together, so ^K^K^K cuts three lines as one block. ^U pastes them back.
^K cut this line (repeat to cut several lines as one block)
^U paste
This pair is how most real config editing gets done: cut a line, paste it somewhere else, or cut it, retype it correctly, and move on. Note that these are nano's own clipboard, not the system clipboard, so pasting into another window still uses your terminal's normal paste.
Back to top5. Moderate Use Cases
5.1 Reading the Shortcut Notation
Two prefixes appear everywhere in nano, and the built-in help (^G) explains them precisely:
Shortcuts are written as follows: Control-key sequences are notated with
a '^' and can be entered either by using the Ctrl key or pressing the Esc
key twice. Meta-key sequences are notated with 'M-' and can be entered
using either the Alt, Cmd, or Esc key, depending on your keyboard setup.
So ^K is Ctrl+K and M-U is Alt+U. The detail worth keeping is the alternative: pressing Esc twice works as Ctrl, and pressing Esc once works as Alt. That matters when you are on a Mac, inside a terminal that eats Alt combinations, or on a remote console where modifier keys arrive mangled. If M-U does nothing, press Esc then U.
5.2 Undo, Which nano Does Have
A surprising number of people believe nano cannot undo. It has had undo since 2008 and it works for every operation, including cuts, pastes and justifications:
M-U undo the last operation
M-E redo the last undone operation
These are not on the two shortcut rows, which is exactly why the myth survives. The bottom of the screen shows the twelve most common commands, not all of them. ^G shows the rest.
5.3 Selecting Text Without a Mouse
To work on a block rather than whole lines, set a mark, move the cursor, and the text between them is selected:
M-A (or ^6) set the mark where the cursor is
then move the cursor; the region highlights
^K cut the marked region
M-6 copy the marked region
^O save just the marked region to a separate file
On most modern terminals you can also just hold Shift and use the arrow keys, which selects text the way every other program does. Any cursor movement without Shift cancels the selection.
5.4 Moving Around Quickly
| Keys | Move to |
|---|---|
^/ or M-G |
A specific line (and optionally column) number |
M-\ / M-/ |
The first line / the last line of the file |
^Y / ^V |
Previous page / next page |
^A / ^E |
Start / end of the current line |
^C |
Not a command: it reports where the cursor is |
M-] |
The matching bracket |
That ^C deserves a warning. In almost every other terminal program ^C means "stop". In nano it shows your position in the file, and does not interrupt anything. To leave nano you need ^X.
5.5 Flags Worth Knowing
Line numbers make error messages much easier to act on, and -l (short for linenumbers) turns them on:
$ nano -l notes.txt
GNU nano 7.2 notes.txt
1 Dear reader,
2
3 This is a small file opened in nano.
4 The third line is here.
5
A few more that earn their place in daily work:
| Flag | Long form | What it does |
|---|---|---|
-l |
--linenumbers |
Show line numbers down the left |
-v |
--view |
Open read-only, so you cannot change the file by accident |
-S |
--softwrap |
Display long lines wrapped on screen, without changing the file |
-m |
--mouse |
Let you click to position the cursor |
-B |
--backup |
Keep a backup of the previous version when saving |
-E |
--tabstospaces |
Insert spaces when you press Tab |
-i |
--autoindent |
Keep the indentation of the previous line |
View mode is worth a special mention for reading logs and other people's configs. The title bar says View, and any key that would change something is refused:
GNU nano 7.2 notes.txt View
[ Key is invalid in view mode ]
5.6 Several Files at Once
nano can hold more than one file open. ^R reads another file into the current buffer at the cursor, and ^R followed by M-F opens it in a separate buffer instead. With multiple buffers open, M-> and M-< switch between them.
$ nano file1.conf file2.conf # open both at once
Two things on screen change to tell you that you are in this mode. The title bar replaces the version with a counter, and the bottom row now says Close rather than Exit, because ^X closes the current file and leaves the other one open:
[1/2] file1.conf
[ file1.conf -- 1 line ]
^G Help ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location
^X Close ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line
Back to top6. Advanced Use Cases
6.1 The Config File
nano reads settings from /etc/nanorc for the whole system and ~/.nanorc for you personally. The system file on Debian and Ubuntu is nearly all comments; only a few lines are actually active:
$ grep -vE '^\s*#|^\s*$' /etc/nanorc
set historylog
set locking
set stateflags
include "/usr/share/nano/*.nanorc"
Everything else is switched off and waiting. Your own ~/.nanorc overrides it, and this is a reasonable starting point:
set linenumbers # show line numbers
set softwrap # wrap long lines on screen only
set autoindent # keep the indent of the previous line
set tabsize 4 # a tab looks four columns wide
set tabstospaces # pressing Tab inserts spaces
set constantshow # always show the cursor position
set backup # keep a ~ backup of the previous version
set mouse # allow clicking to move the cursor
Every option in that file has a command-line twin, so anything you can set permanently you can also try once with a flag before committing to it.
6.2 Syntax Highlighting
That include line in /etc/nanorc is doing more than it looks. It loads a directory of syntax definitions, and on a standard Ubuntu install there are 42 of them:
$ ls /usr/share/nano/*.nanorc | wc -l
42
$ ls /usr/share/nano/ | head
asm.nanorc c.nanorc css.nanorc go.nanorc
autoconf.nanorc changelog.nanorc default.nanorc groff.nanorc
awk.nanorc cmake.nanorc elisp.nanorc guile.nanorc
nano picks one by matching the filename, so opening script.sh, site.conf, or playbook.yml colours the file automatically with no setup. If a file has no extension, force it with -Y:
$ nano -Y sh /usr/local/bin/deploy # treat it as a shell script
6.3 Tabs Versus Spaces
This matters more on a server than it does in a text document, because several file formats care deeply about the difference. Python and YAML want spaces. Makefiles require real tab characters and break in confusing ways without them.
$ nano -E playbook.yml # Tab inserts spaces
$ nano Makefile # leave it alone: this file needs real tabs
Section 7.2 covers a related behaviour that overrides both of these and surprises almost everyone.
6.4 Line Endings: The Other Invisible Character
Tabs are not the only thing you cannot see. Unix ends a line with a single line feed (\n); Windows ends it with a carriage return and a line feed (\r\n). A shell script written on Windows and uploaded to a server fails with errors that make no sense, because the stray \r becomes part of the last word on every line.
nano handles this well, and tells you when it happens. Open a file with Windows line endings and the status line says so:
$ nano dos.txt
[ Read 2 lines (Converted from DOS format) ]
The important part is what happens next. nano remembers the format and writes it back the same way. Editing a Windows file on a Linux server does not silently convert it:
$ file dos.txt
dos.txt: ASCII text, with CRLF line terminators # before
$ nano dos.txt # make an edit, save, exit
$ file dos.txt
dos.txt: ASCII text, with CRLF line terminators # after: unchanged
That is the right default, but it means nano will not fix a broken script for you either. When you actually want Unix endings, ask for them with -u (short for unix):
$ nano -u script.sh # edit, save, exit
$ file script.sh
script.sh: ASCII text # the CRs are gone
You can also toggle the format for a single save: press ^O and the prompt bar offers M-D for DOS format and M-M for Mac format, which switch the file over as you write it.
The third flag is the diagnostic one. -N (short for noconvert) tells nano not to convert on reading, so the carriage returns appear in the text as ^M and you can see exactly what you are dealing with:
$ nano -N dos.txt
GNU nano 7.2 dos.txt
first line^M
second line^M
[ Read 2 lines ]
If a script is failing for no visible reason, that is a fast way to prove the line endings are the cause before you start editing anything.
6.5 Rebinding Keys
If a shortcut fights your muscle memory, change it in ~/.nanorc. This is how people who came from other editors make nano feel normal:
bind ^Z undo main # Ctrl+Z undoes, as everywhere else
bind ^Y redo main
bind ^F whereis main # Ctrl+F searches
bind ^Q exit all
The last word is the context the binding applies in: main for the editing window, all for everywhere including prompts.
That first line is safer than it used to be. Older nano suspended on ^Z, dropping you to the shell until you typed fg, which startled people who expected an undo. Since nano 6.0 the plain keystroke does not suspend; it tells you what to press instead:
[ To suspend, type ^T^Z ]
So rebinding ^Z to undo now costs you nothing, and if you actually want single-key suspension back you ask for it explicitly with bind ^Z suspend main.
6.6 Restricted Mode
The -R flag (short for restricted) stops nano from reading or writing any file other than the one it was given. No saving elsewhere, no reading other files in, no shell commands, no suspending.
$ nano -R /etc/motd
This exists for situations where nano is launched on someone's behalf and should not become a way to roam the filesystem. If you ever set up a restricted account that can edit one file, this is the flag that makes that safe.
6.7 Running Commands Without Leaving
^T is labelled Execute. Press it and the shortcut rows change into a menu that is easy to miss, because it is where several separate features live:
Command to execute:
^G Help M-F New Buffer ^S Spell Check ^J Full Justify ^V Cut Till End
^C Cancel M-\ Pipe Text ^Y Linter ^O Formatter ^Z Suspend
Type a command and its output is inserted into your buffer, which is genuinely useful when you are writing a config and need a value from the system:
^T then type: ip -4 addr show
the command output lands in the file
M-F first sends the output to a new buffer instead, leaving your file untouched. M-\ pipes the buffer through a command and replaces it with the result, so you can sort a list or reformat a block using any tool on the system. The spell checker, linter and formatter hang off the same menu where a syntax defines them.
6.8 nano as the System Editor
A lot of programs open "the editor" without you choosing one: crontab -e, git commit, visudo, sudoedit. On Debian and Ubuntu that editor is nano, wired up through the alternatives system:
$ update-alternatives --display editor
editor - auto mode
link best version is /bin/nano
link currently points to /bin/nano
link editor is /usr/bin/editor
Two environment variables override it for your account. EDITOR is the general one and VISUAL takes priority in programs that check both:
export EDITOR=nano
export VISUAL=nano # put these in ~/.bashrc to make them stick
Git keeps its own setting, which wins over both variables, so set that one separately if you write commit messages on the command line:
$ git config --global core.editor "nano"
$ git config --global core.editor # check what it is now
nano
To change it system-wide on Debian or Ubuntu, use select-editor for your own account or update-alternatives --config editor as root. This is worth setting deliberately: the worst moment to discover your server opens vi by default is halfway through a visudo session.
7. Something Most Users Do Not Know
7.1 The -w Flag Everyone Recommends Is Now the Default
Search for advice about editing configuration files with nano and you will be told, over and over, to use nano -w. The reasoning was real: older nano automatically broke long lines to fit the screen, which could split a configuration directive across two lines and stop a service from starting. -w turned that off.
Look at what nano --help says about that flag today:
$ nano --help | grep -- '-w '
-w --nowrap Don't hard-wrap long lines [default]
Those last two words are the point. The behaviour flipped in nano 4.0, released in March 2019, and the release notes are explicit about it:
2019.03.24 - GNU nano 4.0 "Thy Rope of Sands"
- An overlong line is no longer automatically hard-wrapped.
- Option --breaklonglines (-b) turns automatic hard-wrapping back on.
So -w still works, but it asks for something you already have. Typing it is harmless habit rather than protection.
The more interesting part is what the danger actually was, because the common description of it is wrong. Wrapping never triggered simply from opening a file and saving it. Take a long configuration line, open it with wrapping deliberately switched on, save it untouched, and nothing happens:
$ nano -b httpd.conf # open, save, exit without typing
$ diff httpd.conf httpd.conf.orig
# no output: the file is unchanged
The line only breaks when you type into it and push it past the wrap column. Here is the same file edited twice, once with wrapping on and once with today's default, adding one more host to the end of a long line:
# with -b (--breaklonglines), 2 lines became 3:
# comment
AllowedHosts host-one.example.com host-two.example.com
host-three.example.com host-four.example.com host-five.example.com host-six.example.com
# with the modern default, still 2 lines:
# comment
AllowedHosts host-one.example.com host-two.example.com host-three.example.com host-four.example.com host-five.example.com host-six.example.com
That first result is a broken configuration file. There is one more condition, though: nano can only break a line at whitespace. A long unbroken value, such as a TLS cipher list joined by colons, survives even with wrapping switched on, because there is nowhere to break it.
Since 2019 the safe behaviour is the default, and the real rule was never "nano mangles files you open". It was "nano used to reflow lines you typed into". Both are worth knowing, because plenty of servers still run older versions.
7.2 Tab Does Not Always Insert a Tab
Press Tab in nano and you expect a tab character. Open a YAML file and you get something else entirely. Here is the same keystroke in two files, with the bytes shown in hexadecimal:
$ nano plain.txt # press Tab, save
$ od -An -tx1 plain.txt
09 6c 69 6e 65 0a # 09 = a real tab
$ nano conf.yml # press Tab, save
$ od -An -tx1 conf.yml
20 20 6c 69 6e 65 0a # 20 20 = two spaces
Nothing in your configuration caused this. The reason is one line in the YAML syntax file that ships with nano:
$ grep tabgives /usr/share/nano/yaml.nanorc
tabgives " "
The tabgives directive makes the Tab key produce a given string for files matching that syntax, and the manual notes that it overrides the setting of the tabstospaces option. So a syntax file quietly wins over your own ~/.nanorc.
In this case it is doing you a favour, because YAML forbids tabs for indentation and an accidental tab in an Ansible playbook or a docker-compose file is a parse error. But it is worth knowing that the rule exists, because it explains behaviour that otherwise looks like a bug, and because you can add the same line to a syntax of your own with extendsyntax.
7.3 Your Work Survives a Dropped Connection
This one has rescued real work. You are editing a file over SSH, the connection dies, and the editor disappears with everything you typed. Except it does not.
When nano is killed by a hangup, which is exactly what a dropped SSH session sends, it writes the unsaved buffer to a file named after the original with .save on the end, and leaves the original untouched:
$ ls
crash.txt crash.txt.save
$ cat crash.txt # the original, exactly as it was
original line
$ cat crash.txt.save # everything you had typed
UNSAVED WORK original line
If you have ever lost a connection mid-edit and assumed the work was gone, check the directory. It is probably still there.
There is a companion feature on Debian and Ubuntu, switched on by that set locking line in /etc/nanorc. While nano has a file open it creates a lock file beside it, and the system can tell you exactly what it is:
$ file .notes.txt.swp
.notes.txt.swp: Nano swap file, pid 109282, user pe7er, host xps, file notes.txt
Open the same file in a second window and nano warns you that someone else is already editing it, which prevents two administrators quietly overwriting each other on a shared server. The lock disappears when nano exits cleanly. A leftover .swp file usually means a session died, and it is safe to delete once you are sure nobody is editing.
7.4 Knowing Where nano Stops
Part of expertise is knowing when to reach for something else. nano is the right tool for changing a few lines in a file on a server. It is not the right tool for everything.
| Need | Use | Why |
|---|---|---|
| Edit on a system where nano is missing | vi |
nano is not guaranteed. Alpine, most containers and many rescue images give you BusyBox, where vi is a symlink to /bin/busybox |
| Change the same thing in 200 files | sed |
Scripted editing needs no interactive editor at all |
| Read a large log file | less |
Opens instantly without loading the whole file, and cannot change it |
| Heavy editing, macros, many files | vim or emacs |
Modal editing and macros repay the learning curve when you edit all day |
| Edit a server file from your desktop | An editor over SSH or sshfs |
Your normal tools, with the file still living on the server |
| Edit a system file safely | sudoedit |
Edits a copy as you, then installs it as root, so the editor never runs with root privileges |
That last row is worth adopting as a habit. Running sudo nano /etc/something gives a full editor root privileges, and from inside it ^R and ^T can reach any file on the system. sudoedit /etc/something gives you the same edit with far less exposed.
8. Best Practices
- Learn four keys and you are done.
^Osave,^Xexit,^Wsearch,^Kcut. Everything else can be read off the screen when you need it. - Back up before you edit anything important. Either
cp file file.bakfirst, or use^Owith a different filename, or turn onset backuppermanently. - Use
sudoeditinstead ofsudo nano. Same result, and the editor never runs as root. - Open logs and other people's configs with
-v. Read-only mode removes the chance of a stray keystroke changing a file you only meant to look at. - Turn on line numbers.
set linenumbersin~/.nanorcpays for itself the first time a service reports an error on line 214. - Check the file after you save it. A quick
diffagainst your backup, or the service's own config test (nginx -t,apachectl configtest,sshd -t), catches mistakes before a restart does. - Know your fallback. Learn just enough
vito open a file, change a line and save (i, Esc,:wq). One day nano will not be installed. - Do not fight the editor. If nano is slowing you down on real editing work, that is a signal to learn
vim, not to add fifty rebindings. - Reach for the documentation.
^Ginside nano is a complete key reference, and it is always there.
$ man nano # the program
$ man nanorc # every configuration option
$ nano --help # the flags, with their long forms
^G # the built-in key reference, from inside nano
Back to top9. Common Mistakes
9.1 Common Myths
| Myth | Reality |
|---|---|
"Always use nano -w or it will wreck your config." |
Since nano 4.0 (2019) that is the default. --help literally marks -w as [default]. |
| "nano has no undo." | M-U undoes and M-E redoes. They are just not on the two shortcut rows. |
"^C quits, like everywhere else." |
In nano ^C reports the cursor position. Exit is ^X. |
| "nano is on every Linux system." | It is on most, but not on Alpine, not in most containers, and not in many rescue images. vi is the safer assumption. |
| "nano cannot do regular expressions." | M-R at the search prompt switches them on, for both search and replace. |
| "The Tab key inserts a tab character." | Not in YAML files, where the shipped syntax file sets tabgives " " and overrides your own settings. |
9.2 Traps to Avoid
- Editing a file you do not have permission to write. nano opens it happily and only complains when you try to save, after you have done the work. Check first, or open it with
sudoedit. - Assuming a saved file means a working service. Editing is not applying. Reload or restart the service, and test its config first where the service offers that.
- Pasting a large block into a terminal. Auto-indent and the terminal's own handling can mangle it. Turn off
set autoindentfor the paste, or transfer the file withscpinstead. - Editing a Makefile with
set tabstospaceson. Makefiles need real tab characters, and the error you get is famously unhelpful. - Expecting nano to fix Windows line endings. It preserves the format it found, so a CRLF script stays a CRLF script. Use
nano -u, ordos2unix, when you want it converted. - Leaving
.saveand.swpfiles behind. They are useful for recovery and clutter afterwards. Clean them up once you have what you need. - Running
sudo nanoout of habit. A root editor can read and write anything on the system from inside itself. - Trusting muscle memory from other editors.
^Sdoes save in modern nano, but^Creports your position instead of quitting, and^Zundoes nothing unless you bind it yourself. - Forgetting the file is still open elsewhere. On a shared server, two people editing the same config will silently overwrite each other unless locking is on.
10. Summary
nano is the editor you use when the point is the file, not the editing. It asks nothing of you up front and it is very hard to lose work in.
- nano is a modeless terminal editor that prints its own shortcuts along the bottom.
^means Ctrl,M-means Alt, and pressing Esc twice works as Ctrl when your keyboard fights you. - The name is a recursive joke, "Nano's ANOther editor". It started as TIP (Tip Isn't Pico) and was renamed in January 2000 because the
tipcommand already existed. - It exists because Pico, the editor inside the Pine mail client, had a licence that kept it out of Linux distributions. Chris Allegretta wrote a GPL clone in a weekend in late 1999.
- The four keys that matter:
^Owrite out,^Xexit,^Wsearch,^Kcut with^Uto paste.^Ssaves silently. - It does have undo (
M-U), redo (M-E), regular expression search (M-R), and text selection (M-A, or Shift with the arrow keys). ~/.nanorcconfigures it permanently; every setting has a command-line flag twin. Syntax highlighting is automatic, from 42 definitions in/usr/share/nano.nano -wis advice from before 2019. nano 4.0 made it the default, and the wrapping danger only ever applied to lines you typed into, at whitespace.- Tab does not always insert a tab: the shipped YAML syntax sets
tabgives " ", which overridestabstospaces. - nano detects Windows line endings, says so, and writes them back unchanged.
-uconverts to Unix,-Nshows the carriage returns as^Mso you can diagnose a failing script. - A dropped connection leaves your unsaved work in
filename.save, and an open file is guarded by a.swplock file. - Prefer
sudoeditoversudo nano, and learn just enoughvifor the systems where nano is not installed.
This is the quick reference worth keeping:
nano file open a file
nano +47 file open with the cursor on line 47
nano -l file show line numbers
nano -v file read-only, cannot change the file
nano -E file Tab inserts spaces
nano -u file save with Unix (LF) line endings
nano -N file show Windows carriage returns as ^M
sudoedit file edit a root-owned file without a root editor
^O write out (save) ^X exit
^S save, no prompt ^G the full key reference
^W search ^\ search and replace
^K cut a line ^U paste
M-U undo M-E redo
M-A set the mark M-6 copy the marked region
^/ go to line number ^C show cursor position
^T run an external command ^R read another file in
M-] jump to matching bracket ^Y/^V page up / page down
Small tools like nano are where a lot of server work actually happens: the one line in a virtual host, the setting in a cron file, the typo that took a site offline at four in the afternoon.
Back to top

Peter is a Joomla specialist and a Linux admin for fast, secure and scalable websites.












