Short answer: to save and exit nano, press Ctrl+O, then Enter to keep the file name, then Ctrl+X to exit. If you press Ctrl+X with unsaved changes, nano asks Save modified buffer?: Y saves, N quits without saving, Ctrl+C cancels. In nano’s help, ^ means Ctrl and M- means Alt.
nano notes.txt # open (or create) a file
Ctrl+O, Enter # save under the same name
Ctrl+X # exit; asks first if something is unsaved
Ctrl+X, N # exit and throw the changes awayEverything on this page was checked in GNU nano 7.2 on Debian 12 (package 7.2-1+deb12u1) on 21 October 2026, by driving nano through a pseudo-terminal in a scratch directory and by reading its built-in help screen (Ctrl+G). The prompts quoted below are what that nano printed, except the two permission messages in the sudoedit section, which come from the message strings in nano 7.2 itself. Shortcuts have moved between nano releases, so check yours with nano --version and press Ctrl+G for the list that matches it.
How do I save and exit nano?
- 1Ctrl+O writes the file
nano shows the current name at the bottom, for example
File Name to Write: notes.txt. Press Enter to keep it. The status line then confirms with[ Wrote 1 line ]. - 2Ctrl+X exits
With nothing unsaved, nano closes at once. With unsaved changes it asks first:
nano 7.2, bottom of the screen Save modified buffer? Y Yes N No ^C CancelYshows the file name prompt again (Enter saves and exits),Nexits without saving, andCtrl+Ctakes you back to the text.
So the shortest “save and quit” is Ctrl+X, Y, Enter. nano 7.2 also has Ctrl+S, which saves without asking for the name, and the title bar tells you whether anything is unsaved: Modified at the top right, or an asterisk after the file name when the stateflags option is on, as it is in Debian’s default /etc/nanorc.
What ^ and M- mean in nano
The two help lines at the bottom of the screen list shortcuts like ^O and M-U. The caret is the Ctrl key, M- is the Meta key, which is Alt on a normal keyboard. The help shows capital letters, but you do not need Shift: Ctrl+o and Alt+u are what you press.
| Written | Press | If the key combination does not work |
|---|---|---|
^O | Ctrl and O together | Esc, Esc, then O (the nano manual's fallback for Ctrl) |
M-U | Alt and U together | Esc, release it, then U |
^\ | Ctrl and backslash | Alt+R does the same (replace) |
The Esc route matters over SSH and on a Mac, where Alt often types special characters instead of reaching nano. In the test, Esc followed by u undid the last edit exactly like Alt+U (nano answered Undid addition), and Esc, Esc, x closed nano like Ctrl+X.
nano shortcuts cheat sheet
| Task | Shortcut | Shown in help as |
|---|---|---|
| Save (asks for the name) | Ctrl+O | ^O |
| Save without asking | Ctrl+S | ^S |
| Exit (asks to save if needed) | Ctrl+X | ^X |
| Help with every shortcut | Ctrl+G | ^G |
| Search forward | Ctrl+W | ^W |
| Next / previous match | Alt+W / Alt+Q | M-W / M-Q |
| Search backward | Ctrl+Q | ^Q |
| Search and replace | Ctrl+\ or Alt+R | ^\ / M-R |
| Go to line (and column) | Ctrl+_ or Alt+G | ^/ / M-G |
| First / last line | Alt+\ / Alt+/ | M-\ / M-/ |
| Cut the current line | Ctrl+K | ^K |
| Paste | Ctrl+U | ^U |
| Copy the current line | Alt+6 | M-6 |
| Start a selection (mark) | Alt+A or Ctrl+6 | M-A |
| Undo / redo | Alt+U / Alt+E | M-U / M-E |
| Comment or uncomment the line | Alt+3 | M-3 |
| Show cursor position | Ctrl+C | ^C |
| Count lines, words, characters | Alt+D | M-D |
| Line numbers on / off | Alt+N (or start with nano -l) | M-N |
| Soft wrap long lines on / off | Alt+S (or nano -S) | M-S |
| Insert another file here | Ctrl+R | ^R |
Go to line is the one that looks different by version. nano 7.2 lists it as ^/; most terminals send the same code for Ctrl+/ and Ctrl+_, and in the test Ctrl+_ opened Enter line number, column number:. Alt+G works everywhere. To land on a line straight from the shell, use nano +42 file, handy when an error message names the line.
Search and replace in nano
Ctrl+W opens Search: at the bottom. Type the text and press Enter; Alt+W jumps to the next match, and a string that is not in the file ends with "zzz" not found. Per the nano manual, the default search is forward, case-insensitive and for a literal string. While the search prompt is open, Alt+C switches to case-sensitive, Alt+R to regular expressions and Alt+B to searching backwards.
Replacing is Ctrl+\ (or Alt+R in the editor). In the test, a three-line nginx snippet with old in two lines went like this:
Search (to replace): old
Replace with: new
Replace this instance?
Y Yes A All
N No ^C Cancel
[ Replaced 2 occurrences ]A replaced both in one go; Y and N step through them one at a time. To limit a replacement to part of the file, select that part with Alt+A first: per the manual, a marked region limits the scope of the replace. A pattern never spans lines, and the replacement cannot contain a line break. For the same edit across many files, or in a script, use sed instead.
Cut, copy and paste lines
To delete a line in nano, cut it: Ctrl+K moves the line the cursor is on into the cutbuffer, and Ctrl+U pastes it at the cursor. (Alt+Del throws a line away without touching the cutbuffer, per nano 7.2’s help screen.) Several Ctrl+K presses in a row collect all those lines; any other key in between starts a new cutbuffer. Alt+6 copies instead of cutting. Moving the last line of the test file to the top took four steps:
Ctrl+_ 3 Enter go to line 3
Ctrl+K cut it
Ctrl+_ 1 Enter go to line 1
Ctrl+U paste it aboveFor a part of a line or several lines, set the mark with Alt+A (or Ctrl+6), move the cursor to the other end, then Ctrl+K or Alt+6. Alt+U undoes a cut you did not mean.
Save under a new name
Ctrl+O pre-fills the current name, and typing right away adds to it: in the test, typing s.conf.bak into the prompt produced a file called s.confs.conf.bak. Clear the field first:
- 1Ctrl+O, then Ctrl+K
Ctrl+Kinside the prompt empties the name field. - 2Type the new name and press Enter
nano asks
Save file under DIFFERENT NAME?. PressY. - 3Carry on in the copy
The original file stays as it was on disk, and the title bar now shows the new name, so further saves go to the copy.
The same prompt offers Alt+B to keep a backup of the previous version (the name with a ~ appended), Alt+A to append to an existing file and Alt+D to save with Windows line endings.
Editing files that belong to root: sudoedit or sudo nano
Open /etc/hosts or /etc/ssh/sshd_config as a normal user and nano warns File '...' is unwritable; try to save anyway and it fails with Error writing ...: Permission denied. Quit with Ctrl+X, N, and open the file with root rights:
sudoedit /etc/ssh/sshd_config # same as: sudo -e /etc/ssh/sshd_config
SUDO_EDITOR=nano sudoedit /etc/hosts # pick the editor for this one call
sudo nano /etc/hosts # works too, but runs all of nano as rootPer the sudo manual (1.9.13p3, Debian 12), sudoedit copies the file to a temporary file owned by you, runs your editor as you, and copies the result back when you have changed it. It picks the editor from SUDO_EDITOR, then VISUAL, then EDITOR. That is the safer habit: with sudo nano, everything nano can do runs as root, including Ctrl+T, which can execute external commands.
Make nano the default editor
Programs like crontab -e, git commit and visudo open whatever editor the system names. Three places decide that, from narrow to broad:
- Your shell variables.
export VISUAL=nano EDITOR=nanoin~/.bashrcor~/.profilewins for you in every shell; the guide to environment variables explains which file to pick. Git has its own setting:git config --global core.editor nano. - select-editor (Debian and Ubuntu). It lists the installed editors and stores your pick in
~/.selected_editor. Thesensible-editorscript (sensible-utils 0.0.17 on Debian 12) runs it by itself the first time nothing is set, which is the numbered list crontab -e shows. Runselect-editoragain to change your answer;VISUALandEDITORoverride it. - The system default (Debian and Ubuntu).
sudo update-alternatives --config editorchanges what/usr/bin/editorpoints to for every user. On the Debian 12 test host,update-alternatives --display editorshowed nano with priority 40 ahead ofvim.basic(30) andvim.tiny(15), so nano is the automatic choice there.
~/.nanorc: line numbers, tabs and syntax colours
nano reads /etc/nanorc first and then the first of ~/.nanorc or ~/.config/nano/nanorc it finds (per man nanorc). One setting per line, lowercase:
set linenumbers # same as nano -l
set tabsize 4
set tabstospaces # typed Tab becomes spaces (YAML, Python)
set autoindent
set softwrap # show long lines on several rows, file unchanged
set constantshow # cursor position always in the status bar
include "/usr/share/nano/*.nanorc" # syntax colouring for many file typesnano 7.2 loaded this file without complaint and showed line numbers. A typo is reported at startup with the file and line, for example Error in /home/you/.nanorc on line 2: Requested tab size "four" is invalid. Start with nano -I to ignore every nanorc when you need a clean nano.
nano vs vim
| nano | vim | |
|---|---|---|
| How it works | Modeless: you type text, shortcuts use Ctrl and Alt | Modes: normal mode for commands, insert mode for text |
| Save and exit | Ctrl+O, Enter, Ctrl+X | Esc, then :wq |
| Help on screen | Two lines of shortcuts at the bottom, Ctrl+G for all | :help, nothing on screen by default |
| Search and replace | Ctrl+\ with prompts; Alt+R in the search prompt switches to regex | :%s/old/new/g, ranges, regex by default |
| Undo | Alt+U / Alt+E | u / Ctrl-r, many levels |
| On a minimal server | Installed on most Debian and Ubuntu images | vi is nearly always there, often as a cut-down build |
| Best for | Quick config edits, people who edit rarely | Heavy editing, repeated changes, large files |
Both are fine for a server. If a tool drops you into vim by surprise, the way out is Esc then :q!, and the how to exit vim guide has every other variant. The Linux commands cheat sheet covers the commands you run around the editor.
nano on a VPS: habits that save a config
- Copy before you edit.
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak, or start nano with-Bso it keeps the previous version assshd_config~on every save. - Test before you reload.
sudo sshd -torsudo nginx -tafter saving; when the test names a line,nano +LINE fileopens right there. - Watch out for old nano. Per nano’s release notes, versions before 4.0 (March 2019) hard-wrapped overlong lines by default, which can split a long config line in two. On such a system, start it as
nano -w. - Have a way back in. On a VMHeaven VPS, the browser VNC console in the panel stays reachable when SSH is not, so a broken
sshd_configcan be fixed with nano from there.
nano: command not found
Minimal images sometimes leave it out. Install it with sudo apt install nano on Debian and Ubuntu or sudo dnf install nano on Rocky Linux and AlmaLinux, or edit with vi, which is almost always present.
Frequently asked
How do I save and exit nano?
Press Ctrl+O, then Enter to keep the file name, then Ctrl+X. Or press Ctrl+X straight away: nano asks Save modified buffer?, where Y saves (confirm the name with Enter), N exits without saving and Ctrl+C goes back to the text.
How do I quit nano without saving?
Press Ctrl+X and answer N when nano asks Save modified buffer?. nano closes, and the file on disk stays as it was at the last save.
What do ^ and M- mean in nano?
^ is the Ctrl key, so ^O means Ctrl+O. M- is the Meta key, which is Alt on most keyboards, so M-U means Alt+U. If Alt does not reach nano, for example in a Mac terminal, press Esc, release it, then press the letter.
How do I find and replace in nano?
Press Alt+R (Ctrl and backslash does the same), type the text to find and press Enter, type the replacement and press Enter, then answer Y, N or A for all matches. Ctrl+W on its own only searches, and Alt+W jumps to the next match.