Software & OS

How to Use Nano: Open, Edit, Save and Exit Files

To use nano, open a file with nano notes.txt, type to edit it, press Ctrl+O then Enter to save, and press Ctrl+X to exit. The two lines at the bottom of the screen list the most important shortcuts, where ^ means Ctrl and M- means Alt.

Nano is a small text editor that runs inside the terminal. Unlike Vim, there are no modes: whatever you type goes straight into the file. Shortcuts below are for GNU nano 8.x unless noted; the differences in older versions are covered further down.

Open a file

nano notes.txt

If notes.txt exists, nano opens it; if not, nano starts an empty buffer and creates the file the first time you save. A few useful variants, all from the nano manual:

CommandWhat it does
nano +25 app.confOpen with the cursor on line 25
nano -l app.confShow line numbers on the left (--linenumbers)
nano -v app.confOpen read-only, so you can't change it by accident (--view)
nano -B app.confWhen saving, keep the previous version as app.conf~ (--backup)
sudoedit /etc/hostsEdit a system file safely (uses nano if it is your configured editor)

Read the shortcut bar

The manual explains the notation: "Commands are given by using the Control key (Ctrl, shown as ^) or the Meta key (Alt or Cmd, shown as M-)." So ^O Write Out means Ctrl+O, and M-U Undo means Alt+U. If your terminal swallows Alt combinations, press Esc once and then the letter; the manual documents Esc as a stand-in for Meta.

Press Ctrl+G at any time for the built-in help, which lists every shortcut for your exact version.

Save and exit

  1. Press Ctrl+O (Write Out). The bottom line shows File Name to Write: with the current name filled in.
  2. Press Enter to keep that name, or type a new one to save a copy elsewhere. Nano reports how many lines it wrote.
  3. Press Ctrl+X to exit.

If you press Ctrl+X with unsaved changes, nano asks Save modified buffer?. Press Y to save (then Enter to confirm the file name), N to quit and discard the changes, or Ctrl+C to cancel and keep editing. In nano 8 and later, Ctrl+S saves immediately without asking for a name; the manual describes it as writing "the current file to disk without prompting".

The shortcuts you'll actually use

TaskKeys
Save (asks for file name)Ctrl+O, then Enter
Save now (nano 8+)Ctrl+S
ExitCtrl+X
Search forwardCtrl+W, or Ctrl+F in nano 8+
Next match forward / backward (nano 8+)Alt+F / Alt+B
Search and replaceCtrl+\ or Alt+R
Go to a line numberAlt+G
Cut the current lineCtrl+K
Copy the current lineAlt+6
PasteCtrl+U
Start selecting (set the mark)Ctrl+6 or Alt+A
Undo / redoAlt+U / Alt+E
Show cursor positionCtrl+C
Turn line numbers on or offAlt+N
Comment or uncomment linesAlt+3
Jump to start / end of fileAlt+\ / Alt+/
HelpCtrl+G

Note that Ctrl+C in nano does not copy and does not quit; it reports the cursor position (or cancels a prompt). Ctrl+K and Ctrl+U work with nano's own cutbuffer, not your system clipboard.

Cut, copy and paste a block of text

Nano cuts whole lines by default. To move a block:

  1. Put the cursor at the start of the text and press Ctrl+6 (or Alt+A). The status bar shows Mark Set.
  2. Move the cursor to the end of the text with the arrow keys. The selection is highlighted.
  3. Press Ctrl+K to cut it, or Alt+6 to copy it.
  4. Move to where it should go and press Ctrl+U to paste.

Pressing Ctrl+K several times in a row on consecutive lines collects them all in the cutbuffer, so a quick way to move five lines is Ctrl+K five times, move, then Ctrl+U once. The manual also notes that on some terminals you can select by holding Shift with the arrow keys.

To paste text from outside nano (a command from a web page, say), use your terminal's own paste shortcut. That goes through the terminal, not nano's cutbuffer.

Search and replace

Press Ctrl+W, type the text and press Enter. The cursor jumps to the next match; in nano 8 and later, Alt+F finds the following one. If the search passes the end of the file and starts again from the top, the status bar says Search Wrapped.

To replace, press Ctrl+\ (or Alt+R), type the text to find, Enter, then the replacement, Enter. Nano stops at each match and asks Replace this instance?; press Y to replace it, N to skip, or A to replace all remaining matches without asking again.

Nano 8 changed some keys

Nano 8.0 rebound two keys. The release notes say: "By default ^F is bound to starting a forward search, and ^B to starting a backward search", with Alt+F and Alt+B finding the next match in either direction. In versions before 8.0, Ctrl+F and Ctrl+B are not search keys. Ctrl+W remains bound to search in current versions, so it is the safest key to learn. Check your version with nano --version; this article was checked against GNU nano 8.0 and the current online manual (the latest release is 9.3, October 2026).

If you would rather have the shortcuts most desktop programs use, nano 8 and later accept --modernbindings (short form -/). The manual lists what it changes: "^X cuts, ^C copies, ^V pastes, ^Z undoes, ^Y redoes, ^F searches forward, ^G searches next, ^S saves, ^O opens a file, ^Q quits". With that option, Ctrl+X no longer exits; Ctrl+Q does.

Make the settings permanent with .nanorc

Options you would otherwise type every time go into ~/.nanorc (or ~/.config/nano/nanorc). A small, practical example:

set linenumbers
set autoindent
set tabsize 4
set tabstospaces
set softwrap
set constantshow

Per nanorc(5): linenumbers shows line numbers, autoindent indents a new line like the previous one, tabstospaces converts typed tabs to spaces, softwrap displays long lines over several screen lines instead of scrolling sideways, and constantshow keeps the cursor position on the status bar. The system-wide defaults are in /etc/nanorc.

Editing files you don't own

Opening /etc/hosts or a file in /etc/ssh as a normal user works, but saving fails with a permission error. Don't lose the edit: press Ctrl+O, type a path you can write such as /tmp/hosts.copy, press Enter, exit, then run sudo cp /tmp/hosts.copy /etc/hosts. Next time start with sudoedit /etc/hosts or sudo nano /etc/hosts. How to edit the hosts file walks through that particular file on every OS.

Make nano the default editor

Programs that open an editor for you read the VISUAL and EDITOR environment variables. Git, for example, checks GIT_EDITOR, then its core.editor setting, then VISUAL, then EDITOR, and otherwise falls back to a compile-time default that the Git docs say is "usually vi". To use nano instead, add this to ~/.bashrc or ~/.zshrc:

export EDITOR=nano
export VISUAL=nano

For Git alone, git config --global core.editor nano does the same. If you have already landed in Vim, how to exit Vim gets you out. For the commands you'll use around nano, such as cp, ls and sudo, see basic Linux commands.

Frequently asked questions

What does the ^ symbol mean in nano's shortcut bar?
^ means the Ctrl key, so ^X is Ctrl+X. M- means the Meta key, which is Alt on most keyboards (Cmd or Esc on some Mac terminals), so M-U is Alt+U. If an Alt shortcut doesn't work in your terminal, press Esc once and then the letter.
How do I save a file in nano without exiting?
Press Ctrl+O, check the file name shown at the bottom, and press Enter. In nano 8 and later, Ctrl+S also saves the current file straight away without asking for a name. Then keep editing.
How do I exit nano without saving?
Press Ctrl+X. If the file has unsaved changes, nano asks 'Save modified buffer?'. Press N and nano closes without writing anything. Press Ctrl+C at that prompt to cancel and go back to editing instead.
Why does nano say Permission denied when I save?
Your user cannot write to that file, which is common for system files under /etc. Save a copy somewhere you can write with Ctrl+O and a new name such as /tmp/hosts.copy, exit, and copy it into place with sudo. Next time open the file with sudoedit or sudo nano.

Sources