Save and Exit
- Press
Ctrl+O. The bottom line asks for the file name to write, with the current name filled in. - Press
Enterto keep the name. nano reports how many lines it wrote. - Press
Ctrl+Xto exit.
The shortcut for both steps is Ctrl+X, then Y, then Enter. Current nano versions also accept Ctrl+S, which saves under the current name without asking.
The Prompt When You Exit
When the file has unsaved changes, Ctrl+X asks whether to save the modified buffer:
Y: saves. nano shows the file name; pressEnter.N: exits and throws away every change since the last save.Ctrl+C: cancels and returns to the file.
Exit Without Saving
Press Ctrl+X and answer N. The file on disk stays as it was when you last saved. If you have not saved at all, it is unchanged.
Read the Help Bar
The two lines at the bottom list the main commands. A caret means the Ctrl key, so ^O is Ctrl+O. M- means the Alt key, so M-U is Alt+U. If Alt does nothing in your terminal, press Esc and then the letter. Ctrl+G opens the full help; Ctrl+X closes it.
Permission Denied When Saving
If nano reports an error writing the file with "Permission denied", your user may not write it, which is normal for files under /etc. Your text is still in nano. Two ways out:
- Press
Ctrl+O, change the name to a file in your home folder, such as~/nginx.conf, and pressEnter. Exit, then copy it into place withsudo cp ~/nginx.conf /etc/nginx/nginx.conf. - Or exit without saving and open the file again with sudo:
sudo nano /etc/nginx/nginx.conf.
After you change a service's configuration, test it with the service's own check, such as sudo nginx -t, before you reload the service.
The Most Useful Keys
| Keys | What it does |
|---|---|
Ctrl+W | Search |
Alt+W | Next match |
Ctrl+\ | Search and replace |
Ctrl+K | Cut the line |
Ctrl+U | Paste |
Alt+6 | Copy the line |
Ctrl+_ | Go to a line |
Alt+U | Undo |
Alt+E | Redo |
Ctrl+K several times in a row cuts several lines, and one Ctrl+U pastes them all. To cut a block, start a selection with Alt+A, move the cursor, then press Ctrl+K. Ctrl+C shows the line and column of the cursor.
Open a File at a Line
nano +42 /etc/ssh/sshd_config
nano -l /etc/ssh/sshd_config
+42 puts the cursor on line 42, handy when an error message names a line. -l shows line numbers along the left side.
When the Connection Drops
If your SSH session ends while nano has unsaved changes, nano tries to write them to an emergency file next to the original, named after it with .save added. Compare it with the original before you use it. To keep the editor running through a dropped connection, work inside tmux; see the tmux cheat sheet.
Make nano the Default Editor
Programs such as crontab -e and Git open the editor named in EDITOR. Add this line to ~/.bashrc:
export EDITOR=nano
For Git alone, run git config --global core.editor nano. More on these files in what is .bashrc. If a program opened Vim instead, how to save and quit in Vim gets you out.