GNU nano is a small text editor that runs inside the terminal and prints its most important shortcuts along the bottom of the screen. If you have ever been stuck in Vim, nano is the editor you can open and close on the first try, because the way out is written on the screen.
What nano is
nano started in late 1999 as a free replacement for Pico, the editor built into the Pine email program. Pico's licence kept it out of distributions such as Debian, so Chris Allegretta wrote a clone. It was called TIP at first and renamed nano in January 2000. It is now part of the GNU project, and it still copies Pico's look: a title bar at the top, your text in the middle, and two help lines at the bottom.
You will meet it whether you go looking for it or not. On Ubuntu, the generic editor command is a link that points at nano, so programs that fall back to editor open nano unless you choose something else. You can see the chain for yourself with readlink -f /usr/bin/editor, which prints /usr/bin/nano. Ubuntu manages that link with the alternatives system: nano is registered with a higher priority than Vim, so it wins by default, and sudo update-alternatives --config editor lets you pick another.
No modes: you just type
The biggest difference from Vim is that nano has no modes. In Vim, a key can insert a letter or run a command depending on the mode you are in, which is why a new user can type :q and see nothing happen. In nano, every letter you type goes into the file, the arrow keys move the cursor, and commands always use Control or Alt. Run nano menu.txt and you can type sardines 4 coins straight away.
Because ordinary keys only ever insert text, nothing you type by accident can delete a line or jump somewhere else. Once you have changed the file, the title bar shows 'Modified', so you can tell at a glance when there is something unsaved.
Reading the help lines
The two lines at the bottom of the screen are nano's cheat sheet. Each entry is a key followed by what it does, written in a short notation:
^means the Control key, so^Xis Ctrl+X.M-means the Alt key (nano calls it Meta), soM-Uis Alt+U.
Here are four of the entries from nano 9.2:
The help lines show only the most common commands. ^G opens the full built-in help, which lists every key, and Alt+X hides the help lines when you want the extra two rows for text.
Saving and exiting
In nano's own words, saving is 'writing out'. Ctrl+O asks for the file name to write to, with the current name already filled in. Press Enter to keep it, and the status bar confirms how many lines were written. Ctrl+X then closes the file and takes you back to the shell.
Here is the whole round trip, ending with cat to prove the change is on disk:
If you press Ctrl+X with unsaved changes, nano asks whether to save them first. Answer Y and it shows the same file-name prompt, so you still need to press Enter. Answer N to throw the changes away, or press Ctrl+C to go back to editing. Current versions also save straight away with Ctrl+S, with no prompt.
Cut, paste and undo
nano works on whole lines by default. Ctrl+K cuts the line the cursor is on and puts it in the cutbuffer, nano's own clipboard. Ctrl+U pastes the cutbuffer wherever the cursor is. Moving a line is therefore three steps: cut it, move the cursor, paste it.
In this recording, the salmon line is cut, pasted below prawns, and then the paste is undone:
A few details make this more useful:
- Pressing Ctrl+K several times in a row collects all those lines in the cutbuffer, so one Ctrl+U pastes the whole block.
- Moving the cursor, or running any other command, ends the run: the next Ctrl+K replaces what was in the cutbuffer.
- Alt+6 copies the current line without cutting it.
- To cut part of a line, set a mark with Ctrl+6, move to the end of the text you want, and press Ctrl+K.
- Alt+U undoes the last action and Alt+E redoes it.
Why Ctrl+C and Ctrl+Z don't do what you expect
nano's keys come from Pico and from terminal programs, not from desktop apps, so the shortcuts your fingers already know mean something else here. Ctrl+C doesn't copy: it reports the cursor position on the status bar, such as line 1 of 4. Ctrl+Z doesn't undo: nano answers with a hint, 'To suspend, type ^T^Z', because in a terminal Ctrl+Z usually means 'pause this program and go back to the shell'. Neither key changes your file, so pressing one by habit costs you nothing.
Modern bindings
Since nano 8.0, the --modernbindings option (or its short form, -/) swaps in the keys most other programs use. Ctrl+S saves, Ctrl+Q quits, Ctrl+C copies, Ctrl+X cuts, Ctrl+V pastes, Ctrl+Z undoes and Ctrl+Y redoes. Ctrl+F searches, Ctrl+O opens a file and Ctrl+W writes the file out under a name you choose.
The help lines change with the keys, so the screen still lists the right ones:
Whether you can use it depends on your version. Ubuntu 24.04 ships nano 7.2, which predates the option, so there nano stops with 'unrecognized option' instead of opening the file. Run nano --version to see what you have. To use modern bindings every time on a newer nano, add an alias such as alias nano='nano --modernbindings' to your shell's startup file, since there is no setting for it in nano's own config file, nanorc.
A worked example: fixing a config file
Say a web app on a server fails to start, and the log points at line 12 of /etc/myapp/config.ini, where a port is set to 80800 instead of 8080. You are logged in over SSH with nothing but a terminal.
- Open the file at that line with
sudo nano +12 /etc/myapp/config.ini. The+12puts the cursor on line 12, andsudois there because files under/etcbelong to root. - Press Alt+N if you want line numbers down the side, to confirm you are in the right place.
- Delete the extra
0. With no modes, the letters you type only ever go into the text. - Press Ctrl+O, then Enter to keep the name. The status bar reports the lines written.
- Press Ctrl+X to get back to the shell, and restart the app.
If you don't know the line number, open the file normally and search for 80800 with Ctrl+F, which starts a forward search in nano 8.0 and later.
When to use it
nano suits short, occasional edits: a config file on a server, a crontab, a commit message, a note. It comes installed on Ubuntu and many other Linux systems, and you don't have to learn anything first, since the screen lists the keys.
It is a poor fit for long sessions of programming. It has syntax colouring, line numbers and multiple buffers, but nothing like the motions and text objects of Vim, the extensibility of Emacs, or the debugging and refactoring of an IDE. Nothing stops you using nano for the quick fix and something else for the day's work. For how editors and IDEs compare in general, see Code editor vs IDE.
Common mistakes
- Forgetting
sudoon a system file. nano opens the file, lets you edit it, then fails to save it with a permission error. Save a copy somewhere you can write with Ctrl+O and a new name, or exit and reopen the file withsudo. - Pressing Ctrl+C or Ctrl+Z out of habit. Neither copies nor undoes in the default bindings. Copy a line with Alt+6 and undo with Alt+U.
- Walking away after answering Y to Ctrl+X. That answer only opens the file-name prompt, and the file isn't written until you press Enter.
- Expecting the same keys on every machine. Ctrl+F only searches from nano 8.0, and modern bindings only exist from 8.0, so an older server can behave differently from your laptop. The help lines always show the keys for the version in front of you.
Key takeaways
- nano is a modeless terminal editor: letters go into the file, and commands use Ctrl and Alt.
- In the help lines,
^means Ctrl andM-means Alt. - Ctrl+O writes the file and Ctrl+X exits; Ctrl+K cuts a line, Ctrl+U pastes it and Alt+U undoes.
- Ctrl+C reports the cursor position and Ctrl+Z only shows a hint, so neither copies nor undoes.
--modernbindings, from nano 8.0, switches to Ctrl+S, Ctrl+Q, Ctrl+C, Ctrl+V and Ctrl+Z style keys.