Where to start First steps track · 15 stages
Linux Labs

nano — a simple console editor

3 min GNU nano

A friendly console editor — save, quit, search, for editing config files.

Cheat sheet

7 commands Printable version
  • nano file open/create a file
  • sudo nano /etc/hosts edit a system file
  • nano +25 file open positioned at line 25
  • nano -w file don't wrap long lines
  • Ctrl+O save Ctrl+X quit
  • Ctrl+W search Ctrl+\ replace
  • Ctrl+K cut a line Ctrl+U paste Ctrl+6 select

Overview

nano is a simple editor: you type right away, with no modes (unlike vim). The bottom of the screen shows keyboard shortcuts, where ^ means Ctrl and M- means Alt. A good choice for quick config edits.

bash
nano file                         # open/create a file
sudo nano /etc/hosts              # edit a system file
nano +25 file                     # open positioned at line 25
nano -w /etc/nginx/nginx.conf     # no soft-wrapping long lines (important for configs!)

Options

7
set linenumbers          # line numbers
set tabsize 4
set tabstospaces         # tabs become spaces
set constantshow         # always show the cursor position
include "/usr/share/nano/*.nanorc"   # syntax highlighting

Globally — /etc/nanorc. Highlighting for many formats works out of the box.

Option What it does Example
-w --nowrap Do not soft-wrap long lines — important when editing config files. nano -w /etc/nginx/nginx.conf
-l --linenumbers Show line numbers. nano -l file
+N Open the file positioned at line N. nano +25 file
-c --constantshow Always display the current cursor position. nano -c file
-i --autoindent Auto-indent — a new line inherits the previous line's indent. nano -i file
-m --mouse Enable mouse support. nano -m file
-B --backup Keep a backup copy of the file (suffixed ~) when saving. nano -B file

Nothing found. Try a shorter query.

Recipes

1

Keyboard shortcuts

Ctrl+O   save (Write Out), then Enter to confirm the name
Ctrl+X   quit (asks about saving)
Ctrl+W   search (Where Is); Alt+W — next match
Ctrl+\   search and replace
Ctrl+K   cut a line          Ctrl+U — paste
Ctrl+6   start a selection, then arrows → Ctrl+K cuts the selection
Alt+U / Alt+E   undo / redo
Ctrl+_   go to line:column
Ctrl+G   help (the full list)

^O = Ctrl+O, M-W = Alt+W. This is printed at the bottom of the screen — check there.

Troubleshooting

5

You saved, but the change didn't take effect for the service

The editor only writes the file. The service needs to reread its config (systemctl reload/restart).

No permission to save

You didn't open it with sudo. Quit (Ctrl+X, without saving) and sudo nano file.

Long-line wrapping is confusing (configs)

nano -w file (or set nowrap) turns off soft wrapping, so long config lines don't look visually "broken".

Mouse paste breaks / duplicates

Terminal paste can be slow with large blocks; for configs this is usually fine. For indentation, use set tabstospaces.

More used to vim

For servers, know at least the minimum of vim — it's always there; nano sometimes isn't installed (sudo apt install nano).

See also

  • vim — The minimum for editing configs in vim/vi — modes, saving, quitting, search-replace.
  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
Loading…