All posts
Linux

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 showsYou pressFallback when the combo fails
^OCtrl+OEsc twice, then O
M-UAlt+UEsc, 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

JobKeysHelp label
Save (with name prompt)Ctrl+O^O
Save quietlyCtrl+S^S
QuitCtrl+X^X
Full helpCtrl+G^G
Find forwardCtrl+W^W
Next / previous hitAlt+W / Alt+QM-W / M-Q
Find backwardCtrl+Q^Q
Find and replaceCtrl+\ or Alt+R^\ / M-R
Jump to lineCtrl+_ or Alt+G^_ / M-G
Top / bottomAlt+\ / Alt+/M-\ / M-/
Cut lineCtrl+K^K
PasteCtrl+U^U
Copy lineAlt+6M-6
Start selectionAlt+A or Ctrl+6M-A
Undo / redoAlt+U / Alt+EM-U / M-E
Toggle commentAlt+3M-3
Cursor positionCtrl+C^C
Word / line statsAlt+DM-D
Line numbersAlt+N or nano -lM-N
Soft wrapAlt+S or nano -SM-S
Insert another fileCtrl+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.

  1. Ctrl+O, then Ctrl+K inside the prompt to clear the name.
  2. Type the new path and press Enter.
  3. 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:

  1. Shell: export VISUAL=nano EDITOR=nano in ~/.bashrc or ~/.profile. For Git alone: git config --global core.editor nano.
  2. Debian/Ubuntu: run select-editor once; the choice lands in ~/.selected_editor.
  3. System alternative: sudo update-alternatives --config editor so /usr/bin/editor points 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

nanovim
ModelAlways typing; Ctrl/Alt chordsModes (normal vs insert)
Exit with saveCtrl+O, Enter, Ctrl+XEsc, :wq
DiscoverabilityFooter + Ctrl+G:help
ReplacePrompt-driven Ctrl+\:%s/old/new/g
Typical VPS roleQuick config editsHeavy, 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 -B for automatic ~ backups.
  • Validate before reload: sudo sshd -t, sudo nginx -t, then jump with nano +LINE file when the checker names a line.
  • Very old nano (pre-4.0) may hard-wrap long lines; use nano -w on 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.

Share

XLinkedIn