nano cheatsheet

Keyboard shortcuts and options of the nano editor: save, exit, search, replace, cut, paste, undo and more.

loading nano cheatsheet
About this tool

What is a nano cheatsheet?

nano is the editor that comes with most Linux distributions and is the easiest to start with: no modes, you type and the text appears. The catch is that every action is a key combination, and the two lines of hints at the bottom of the screen show only a few of them. In the hints, ^ means the Ctrl key (so ^X is Ctrl+X) and M- means the Alt key.

This cheatsheet lists the shortcuts by task in six sections: open and start, save and exit, move around, search and replace, cut, copy and paste, and view options. It also has the command-line options that matter, such as opening a file at a given line with nano +<line> <file>, showing line numbers with nano -l <file>, or opening read-only with nano -v <file>. At the end there is an example of a ~/.nanorc, the file where you make your settings permanent.

Placeholders appear between angle brackets, such as <file> and <line>; replace them before you run the command. Shortcuts are written the way you press them, for example Ctrl+O or Alt+6. Some shortcuts differ between versions of nano or between keyboards, and the built-in help (Ctrl+G) is the final word on yours. The page runs in your browser, with no account and nothing sent. If you prefer a modal editor, see the Vim cheatsheet.

How to use the nano cheatsheet

  1. Open the page and scroll, or use the section chips at the top to jump to a group such as Search and replace.
  2. Type in the search box to filter. It looks at the description, the section title and the shortcut, so line finds the items about going to a line, cutting a line and showing line numbers. Capital letters and accents are ignored.
  3. Press / anywhere on the page to focus the search box.
  4. Click the copy button next to an item to copy its command or shortcut. When an item has two shortcuts, such as start and end of the line, both are copied, one per line.
  5. Use the Copy section button in a section title to take all the items of that group.
  6. Click Print to print the sheet or save it as a PDF, without the buttons.
  7. Open your file with the commands of the first section and use the shortcuts as you need them.

For the full list of commands to use around the editor, there is the Linux cheatsheet.

Examples

Edit a system file and save it

Files like /etc/hosts belong to root, so open them with sudo:

sudo nano /etc/hosts

Make your change, then press Ctrl+O to write the file. nano shows the file name at the bottom; press Enter to confirm. Then Ctrl+X to leave. If you press Ctrl+X with unsaved changes, nano asks:

Save modified buffer?
 Y Yes
 N No           ^C Cancel

Answer Y to save, N to discard, or Ctrl+C to go back to editing.

Move a block of lines

Put the cursor on the first line, press Ctrl+6 to start the selection, move down with the arrow keys, and press Ctrl+K to cut. Move to the destination and press Ctrl+U to paste. Without a selection, Ctrl+K cuts the current line, and several Ctrl+K in a row cut several lines that Ctrl+U pastes back together.

Search and replace everywhere

Press Ctrl+\, type the text to find, press Enter, type the replacement and press Enter. For each match nano asks what to do:

Replace this instance?
 Y Yes
 N No           A All

Press Y for one, N to skip, A to replace all the rest. Before that, Alt+C turns case sensitivity on and Alt+R turns regular expressions on in the prompt.

Make settings permanent

Open nano ~/.nanorc and write:

set linenumbers
set tabsize 4
set tabstospaces
set autoindent

The next time you open nano it shows line numbers, uses four spaces for a tab and keeps the indentation of the previous line. The same toggles exist as shortcuts (Alt+N, Alt+I) for a single session.

Common errors and how to fix them

  • Permission denied or a message saying the file is unwritable: you opened a file owned by another user. Leave with Ctrl+X and open it again with sudo nano <file>.
  • Ctrl+S freezes the terminal: in nano, saving is Ctrl+O. In many terminals Ctrl+S pauses the screen (flow control) and nothing you type appears; press Ctrl+Q to resume.
  • Cannot exit: the shortcut is Ctrl+X, not Esc and not :q. If a prompt is open, cancel it with Ctrl+C first.
  • The Alt shortcuts do not work: some terminal apps, especially on macOS, use Alt for special characters. Enable "Use Option as Meta key" in the terminal settings, or press Esc and then the key instead of holding Alt.
  • Long lines wrap strangely, or lines were cut in two: this is soft wrapping versus real line breaks. Alt+S toggles soft wrapping, and Alt+P shows whitespace so you can see what is a real line break.
  • A file~ appeared next to your file: you started nano with -B, which keeps a backup. Delete it if you do not need it.
  • No shortcut matches your search: remove a word. All words must match at once.

Frequently asked questions

How do I save and exit in nano?

Press Ctrl+O and Enter to save, then Ctrl+X to exit. You can also press Ctrl+X directly: if there are changes, nano asks whether to save, and you answer Y.

How do I cut, copy and paste in nano?

Ctrl+K cuts the current line, Alt+6 copies it and Ctrl+U pastes. To work with part of a line, start the selection with Ctrl+6, move the cursor, and then cut or copy.

How do I show line numbers in nano?

Press Alt+N inside the editor, or open the file with nano -l <file>. To keep them on always, add set linenumbers to ~/.nanorc.

How do I search in nano?

Ctrl+W opens the search. Alt+W goes to the next match and Alt+Q to the previous one. To replace, use Ctrl+\.

What do the ^ and M- symbols mean?

^ is Ctrl and M- is the Meta key, which is Alt on most keyboards. So ^O is Ctrl+O and M-U is Alt+U.

Report a problem