How to save and exit nano (shortcuts, nanorc, sudoedit)
Ctrl+O, Enter, Ctrl+X to leave cleanly. A practical map of search, replace, cut/paste, ^ vs M-, root edits with sudoedit, and a small ~/.nanorc for VPS work.
Need the exit hatch? Press Ctrl+O, confirm the name with Enter, then leave with Ctrl+X. If you hit Ctrl+X first, nano asks whether to keep your edits: Y saves, N discards, Ctrl+C returns to the buffer. In the bottom help strip, ^ is Ctrl and M- is Alt.
nano notes.txt
# Ctrl+O → Enter → Ctrl+X
This guide walks through everyday nano work on a Linux VPS: saving, quitting, search/replace, cut/paste, root-owned files, defaults, and a small ~/.nanorc. Behaviour described here matches current GNU nano on Debian-style systems. Run nano --version and open Ctrl+G on your host if a shortcut looks different.
Save and leave
Ctrl+O writes the buffer. The status area offers the current path; Enter keeps it. You should see a short confirmation such as [ Wrote N lines ].
Ctrl+X closes the editor. With a clean buffer it exits immediately. With pending changes you get a three-way prompt: save, discard, or cancel.
A faster “done” sequence is Ctrl+X, Y, Enter. On recent nano builds, Ctrl+S also saves without re-asking for the filename. Watch the title bar for Modified (or a trailing asterisk when stateflags is enabled) so you know whether a write is still pending.
Reading the help keys: ^ and M-
The footer lists bindings like ^O and M-U.
| Help shows | You press | Fallback when the combo fails |
|---|---|---|
^O | Ctrl+O | Esc twice, then O |
M-U | Alt+U | Esc, release, then U |
^\ | Ctrl+\ | Alt+R for replace |
Capital letters in the help are labels only. You do not hold Shift. Over SSH or on macOS, Alt often fails to reach nano; the Esc sequences above are the reliable backup.
Shortcut map
| Job | Keys | Help label |
|---|---|---|
| Save (with name prompt) | Ctrl+O | ^O |
| Save quietly | Ctrl+S | ^S |
| Quit | Ctrl+X | ^X |
| Full help | Ctrl+G | ^G |
| Find forward | Ctrl+W | ^W |
| Next / previous hit | Alt+W / Alt+Q | M-W / M-Q |
| Find backward | Ctrl+Q | ^Q |
| Find and replace | Ctrl+\ or Alt+R | ^\ / M-R |
| Jump to line | Ctrl+_ or Alt+G | ^_ / M-G |
| Top / bottom | Alt+\ / Alt+/ | M-\ / M-/ |
| Cut line | Ctrl+K | ^K |
| Paste | Ctrl+U | ^U |
| Copy line | Alt+6 | M-6 |
| Start selection | Alt+A or Ctrl+6 | M-A |
| Undo / redo | Alt+U / Alt+E | M-U / M-E |
| Toggle comment | Alt+3 | M-3 |
| Cursor position | Ctrl+C | ^C |
| Word / line stats | Alt+D | M-D |
| Line numbers | Alt+N or nano -l | M-N |
| Soft wrap | Alt+S or nano -S | M-S |
| Insert another file | Ctrl+R | ^R |
Open a file on a known line from the shell with nano +42 path/to/file when a service points at a bad line number.
Find and replace
Ctrl+W opens the search prompt. After the first match, Alt+W continues forward. Empty results print a short “not found” status.
At the search prompt you can flip behaviour: Alt+C for case sensitivity, Alt+R for regular expressions, Alt+B to search upward.
Replace with *Ctrl+* (or Alt+R from the editor). nano asks for the needle, then the replacement, then whether to change this hit, every hit, skip, or cancel. Mark a region with Alt+A first if you only want the change inside a block. Replacements stay on a single line; for bulk edits across many files, use sed or a proper script.
Cut, copy, paste
Ctrl+K cuts the current line into the cutbuffer. Ctrl+U pastes it. Repeated Ctrl+K without another keystroke stacks lines into one paste; any other key starts a fresh cutbuffer. Alt+6 copies instead of cutting. Alt+Del drops a line without updating the cutbuffer.
For a partial line or a multi-line block: Alt+A to mark, move the cursor, then cut or copy. Alt+U undoes an accidental cut.
Example: move line 3 to the top of a short file.
Ctrl+_ → 3 → Enter
Ctrl+K
Ctrl+_ → 1 → Enter
Ctrl+U
Save as a new file
Ctrl+O fills the existing name. Typing without clearing appends to that string, which is rarely what you want.
- Ctrl+O, then Ctrl+K inside the prompt to clear the name.
- Type the new path and press Enter.
- Confirm the “different name” prompt with Y.
Further saves target the new path. From the same prompt, Alt+B keeps a ~ backup, Alt+A appends into an existing file, and Alt+D writes Windows line endings.
Root-owned configs: prefer sudoedit
Editing /etc/ssh/sshd_config as a normal user usually shows that the file is unwritable. Saving then fails with a permission error. Leave with Ctrl+X, N, and reopen safely:
sudoedit /etc/ssh/sshd_config
SUDO_EDITOR=nano sudoedit /etc/hosts
# works, but elevates the whole editor:
sudo nano /etc/hosts
sudoedit copies the file to a temp location you own, runs the editor as you, then writes back. Prefer it over sudo nano so helper actions inside nano do not run as root.
Make nano the default editor
Pick one level, from personal to system-wide:
- Shell:
export VISUAL=nano EDITOR=nanoin~/.bashrcor~/.profile. For Git alone:git config --global core.editor nano. - Debian/Ubuntu: run
select-editoronce; the choice lands in~/.selected_editor. - System alternative:
sudo update-alternatives --config editorso/usr/bin/editorpoints at nano for every account.
A practical ~/.nanorc
nano loads /etc/nanorc, then your user file (~/.nanorc or ~/.config/nano/nanorc). Keep options lowercase, one per line:
set linenumbers
set tabsize 4
set tabstospaces
set autoindent
set softwrap
set constantshow
include "/usr/share/nano/*.nanorc"
Start with nano -I when a bad nanorc blocks you. Distros disagree on the packaged /etc/nanorc, so read yours before copying settings between machines.
nano compared with vim
| nano | vim | |
|---|---|---|
| Model | Always typing; Ctrl/Alt chords | Modes (normal vs insert) |
| Exit with save | Ctrl+O, Enter, Ctrl+X | Esc, :wq |
| Discoverability | Footer + Ctrl+G | :help |
| Replace | Prompt-driven Ctrl+\ | :%s/old/new/g |
| Typical VPS role | Quick config edits | Heavy, repetitive editing |
If a tool opens vim by surprise: Esc, then :q!.
Habits that keep VPS configs intact
- Snapshot first:
sudo cp file file.bak, or start nano with-Bfor automatic~backups. - Validate before reload:
sudo sshd -t,sudo nginx -t, then jump withnano +LINE filewhen the checker names a line. - Very old nano (pre-4.0) may hard-wrap long lines; use
nano -won those hosts. - Keep a panel console ready. When SSH dies after a bad
sshd_config, fix it from the VpsLayer browser console with nano.
nano: command not found
Install it when the image is minimal:
sudo apt install nano # Debian / Ubuntu
sudo dnf install nano # Rocky / AlmaLinux
Otherwise fall back to vi, which is almost always present.
FAQ
How do I save and exit?
Ctrl+O, Enter, Ctrl+X. Or Ctrl+X first and answer the save prompt.
How do I quit without saving?
Ctrl+X, then N.
What do ^ and M- mean?
^ = Ctrl. M- = Alt (Meta). Use Esc sequences when Alt is broken.
How do I replace text?
Alt+R or **Ctrl+**, enter find and replace strings, then Y, N, or A. Ctrl+W only searches; Alt+W jumps to the next match.
