GNU nano is the friendliest terminal editor on Linux: what you type goes straight into the file, and the most important shortcuts are listed at the bottom of the screen. It’s installed on nearly every Ubuntu and Debian server and is perfect for editing config files over SSH.

Open a file

nano notes.txt
sudo nano /etc/nginx/sites-available/example.com    # root-owned files
nano +37 /etc/ssh/sshd_config                       # open at line 37
nano -l file.conf                                   # show line numbers

If the file doesn’t exist, nano creates it when you save.

Reading the shortcut bar

^ means Ctrl, and M- means Alt (or Esc then the key). So ^O is Ctrl+O and M-U is Alt+U.

The essentials

Shortcut Action
Ctrl+O, Enter Save (“Write Out”)
Ctrl+X Exit (asks to save if there are changes)
Ctrl+S Save without the file-name prompt (nano 4+)
Ctrl+W Search (“Where is”)
Alt+W Find next
Ctrl+\ Search and replace
Ctrl+_ or Alt+G Go to line (and column)
Ctrl+K Cut the current line
Ctrl+U Paste (“Uncut”)
Alt+6 Copy the current line
Alt+U / Alt+E Undo / redo
Ctrl+C Show cursor position (line/column)
Ctrl+G Help

Cut, copy and paste blocks

  1. Move to the start and press Alt+A (set mark).
  2. Move the cursor to the end — the selection highlights.
  3. Ctrl+K to cut or Alt+6 to copy.
  4. Move to the destination and Ctrl+U to paste.

Pressing Ctrl+K several times in a row cuts several lines into one buffer; Ctrl+U pastes them all.

Search and replace

Ctrl+\, type the text to find, Enter, the replacement, Enter — then Y for this match, N to skip, A for all. Toggle regular expressions with Alt+R inside the search prompt.

Indent and comment

  • Alt+} / Alt+{ — indent / unindent the marked lines (or current line).
  • Alt+3 — comment or uncomment the current line or selection (nano 2.9+), using the comment style of the file type.
Shortcut Moves
Ctrl+A / Ctrl+E Start / end of line
Ctrl+Y / Ctrl+V Page up / down
Alt+\ / Alt+/ Start / end of file
Ctrl+← / Ctrl+→ Previous / next word

Useful command-line options

nano -B file        # keep a backup (file~) of the original
nano -w file        # don't wrap long lines (important for config files)
nano -i file        # auto-indent
nano -v file        # view only (read-only)
nano -E file        # convert typed tabs to spaces

Long lines in config files must not be hard-wrapped, or the config breaks. Recent versions don’t hard-wrap by default; older ones need -w.

A handy ~/.nanorc

set linenumbers
set autoindent
set tabsize 4
set tabstospaces
set nowrap
set mouse
set constantshow
include "/usr/share/nano/*.nanorc"

The include line enables syntax highlighting for many file types (nginx, sh, python, yaml…). For root’s editing sessions, put the same file in /root/.nanorc or system-wide in /etc/nanorc.

Make nano the default editor

Commands like crontab -e, visudo and git commit use the default editor:

echo 'export EDITOR=nano VISUAL=nano' >> ~/.bashrc
sudo update-alternatives --config editor       # Debian/Ubuntu system default
sudo EDITOR=nano visudo

Editing safely on servers

  • Validate after editing — sudo nginx -t, sudo sshd -t, sudo apachectl configtest, sudo visudo -c — before reloading the service.
  • Keep a backup: sudo cp file file.bak or use nano -B.
  • YAML and Python — use spaces, not tabs (set tabstospaces).
  • Windows line endings — files pasted from Windows may contain \r. nano shows [ Converted from DOS format ]; save with Alt+D toggled off to write Unix line endings.

Troubleshooting

  • “Permission denied” when saving — the file belongs to root. Save a copy to your home (Ctrl+O and change the name), then sudo cp it into place, or reopen with sudo nano.
  • Can’t use Alt shortcuts (macOS Terminal) — enable “Use Option as Meta key” in the terminal settings, or press Esc followed by the key.
  • Paste indents strangely — disable autoindent before pasting (Alt+I toggles it).

Want more power for big edits? See Vim basics. For editing many files at once from the command line, sed is faster.

  • Nano
  • Linux
  • Command Line