How to use the nano editor

nano is a command-line text editor preinstalled on many Linux distributions. It's easy to use and suitable for quickly editing server configuration files.

Installing nano

On many systems, nano is installed by default. If it's not installed, install it:

# Debian/Ubuntu sudo apt update && sudo apt install -y nano # CentOS/RHEL/AlmaLinux/Rocky sudo yum install -y nano # or sudo dnf install -y nano Code language:  PHP  ( php )

Launch and create a file

Open an existing file or create a new one:

nano /etc/nginx/nginx.conf nano myfile.txt

If the file does not exist, nano will create it when saving.

Symbols in tooltips

Hotkeys are shown at the bottom of the nano window. The ^ represents the Ctrl . For example, ^X = Ctrl+X . The M- typically stands for Alt (Meta).

Basic hotkeys

ActionCombination
Save fileCtrl+O
Exit nanoCtrl+X
Undo operation / display cursor positionCtrl+C
ReferenceCtrl+G
SearchCtrl+W
Replace (Search and Replace)Ctrl+\
Cut a lineCtrl+K
Paste (insert previously cut)Ctrl+U
Mark (start selecting)Ctrl+^
Go to row/columnCtrl+_ (then enter the number)
To the beginning of the line / to the end of the lineCtrl+A / Ctrl+E
Page up / downCtrl+Y / Ctrl+V

Save and exit

  • Save: Ctrl+O , then Enter
  • Exit: Ctrl+X
  • If there are unsaved changes, nano will ask whether to save the file: press Y (Yes) or N (No)

Search and replace

  • Search: Ctrl+W , enter the string and press Enter
  • Search further: Alt+W Ctrl+W again → Enter
  • Replace: Ctrl+\ - enter the search text, then the replacement text, confirm with Y / A (Yes / All)

Cut, copy, paste

  • Cut line: Ctrl+K
  • Paste: Ctrl+U
  • Select a block: Ctrl+^ (hold and move the cursor with the arrow keys), then Ctrl+K / Ctrl+U

Line numbering and other settings

Create (or edit) the ~/.nanorc to enable useful options. For example:

nano ~/.nanorc

Add the following lines:

set linenumbers # show line numbers set tabsize 4 # tab size set softwrap # wrap long lines set mouse # allow mouse use in terminal set historylog # save search history Code language:  JavaScript  ( javascript )

Alternatively, you can enable line numbers at startup:

nano  -l file .txt Code language:  CSS  ( css )

Editing files with root privileges

If the file is owned by root, open it with sudo :

sudo nano /etc/hosts

Or use sudo -e (uses the default editor, which can be set via the EDITOR ):

sudo EDITOR=nano -e /etc/hosts

Useful launch arguments

OptionDescription
-lShow line numbers
-cShow cursor position
-mEnable mouse support
-iAuto-indents
-EShow tab characters

Frequently asked questions

nano writes [ Read Only ]. What should I do?

You don't have write permissions to the file. Open the file with sudo, or save it under a different name, and then move the file with the correct permissions.

How to quickly jump to a specific line?

Press Ctrl+_, enter the line number and press Enter.

How to enable syntax highlighting?

In new versions of nano, it's enabled by default for popular languages. You can also enable additional rules in ~/.nanorc.