Guides

nano text editor: how to save and exit, shortcuts and settings

How to save and exit nano: Ctrl+O, Enter, Ctrl+X. The nano text editor's shortcuts for search, replace and cut, what ^ and M- mean, nanorc and sudoedit.

Published ~8 min read
On this page

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, any Linux
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 away

Everything 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?

  1. 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 ].

  2. 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 Cancel

    Y shows the file name prompt again (Enter saves and exits), N exits without saving, and Ctrl+C takes 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.

WrittenPressIf the key combination does not work
^OCtrl and O togetherEsc, Esc, then O (the nano manual's fallback for Ctrl)
M-UAlt and U togetherEsc, release it, then U
^\Ctrl and backslashAlt+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

TaskShortcutShown in help as
Save (asks for the name)Ctrl+O^O
Save without askingCtrl+S^S
Exit (asks to save if needed)Ctrl+X^X
Help with every shortcutCtrl+G^G
Search forwardCtrl+W^W
Next / previous matchAlt+W / Alt+QM-W / M-Q
Search backwardCtrl+Q^Q
Search and replaceCtrl+\ or Alt+R^\ / M-R
Go to line (and column)Ctrl+_ or Alt+G^/ / M-G
First / last lineAlt+\ / Alt+/M-\ / M-/
Cut the current lineCtrl+K^K
PasteCtrl+U^U
Copy the current lineAlt+6M-6
Start a selection (mark)Alt+A or Ctrl+6M-A
Undo / redoAlt+U / Alt+EM-U / M-E
Comment or uncomment the lineAlt+3M-3
Show cursor positionCtrl+C^C
Count lines, words, charactersAlt+DM-D
Line numbers on / offAlt+N (or start with nano -l)M-N
Soft wrap long lines on / offAlt+S (or nano -S)M-S
Insert another file hereCtrl+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:

nano 7.2 prompts, in order
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:

nano
Ctrl+_  3  Enter     go to line 3
Ctrl+K               cut it
Ctrl+_  1  Enter     go to line 1
Ctrl+U               paste it above

For 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:

  1. 1Ctrl+O, then Ctrl+K

    Ctrl+K inside the prompt empties the name field.

  2. 2Type the new name and press Enter

    nano asks Save file under DIFFERENT NAME?. Press Y.

  3. 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:

Debian · Ubuntu · Rocky · AlmaLinux
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 root

Per 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=nano in ~/.bashrc or ~/.profile wins 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. The sensible-editor script (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. Run select-editor again to change your answer; VISUAL and EDITOR override it.
  • The system default (Debian and Ubuntu). sudo update-alternatives --config editor changes what /usr/bin/editor points to for every user. On the Debian 12 test host, update-alternatives --display editor showed nano with priority 40 ahead of vim.basic (30) and vim.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:

~/.nanorc
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 types

nano 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

nanovim
How it worksModeless: you type text, shortcuts use Ctrl and AltModes: normal mode for commands, insert mode for text
Save and exitCtrl+O, Enter, Ctrl+XEsc, then :wq
Help on screenTwo lines of shortcuts at the bottom, Ctrl+G for all:help, nothing on screen by default
Search and replaceCtrl+\ with prompts; Alt+R in the search prompt switches to regex:%s/old/new/g, ranges, regex by default
UndoAlt+U / Alt+Eu / Ctrl-r, many levels
On a minimal serverInstalled on most Debian and Ubuntu imagesvi is nearly always there, often as a cut-down build
Best forQuick config edits, people who edit rarelyHeavy 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 -B so it keeps the previous version as sshd_config~ on every save.
  • Test before you reload. sudo sshd -t or sudo nginx -t after saving; when the test names a line, nano +LINE file opens 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_config can 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.

Related articles

All guides