GNU nano is a lightweight, terminal-based text editor built into almost every Linux distribution and macOS system. Unlike modal editors like Vim, nano is modeless—you type directly into the file and use Ctrl or Alt key combinations for commands.
1. Opening and Creating Files
Launch nano from your terminal by passing a filename. If the file doesn’t exist, nano creates a new buffer in memory and writes it to disk when you save.
Open or create a file:
nano filename.txtOpen a file at a specific line and column: (Useful for jumping to syntax errors in config files)
nano +42,10 /etc/nginx/nginx.confOpen with line numbers displayed:
nano -l script.shOpen in read-only mode: (Prevents accidental edits to critical system files)
nano -v /etc/fstab
Reading the Bottom Bar: The two rows at the bottom of the screen display your primary shortcuts. The caret symbol (
^) represents the Ctrl key, andM-represents the Meta key (Alt on Linux/Windows, or Esc / Option on macOS).
2. Essential Keyboard Shortcuts
| Action | Shortcut | Notes |
|---|---|---|
| Save (Write Out) | Ctrl + O | Prompts for filename; press Enter to confirm. |
| Exit | Ctrl + X | Prompts to save (Y or N) if there are unsaved changes. |
| Cut Line | Ctrl + K | Cuts the entire current line (or highlighted selection). |
| Paste (Uncut) | Ctrl + U | Pastes cut/copied text at the cursor. |
| Copy Line | Alt + 6 | Copies the current line or highlighted selection. |
| Undo / Redo | Alt + U / Alt + E | Reverts or reapplies recent edits. |
| Search | Ctrl + W | Type search term and press Enter. |
| Find Next | Alt + W | Jumps to the next occurrence of your last search. |
| Search & Replace | Ctrl + \ | Prompts for search term, replacement, and confirmation (Y, N, or A for All). |
| Help Menu | Ctrl + G | Opens the full built-in shortcut reference (Ctrl + X to close). |
3. Navigation and Text Selection
Moving around large config files is much faster with jump shortcuts than holding down the arrow keys.
- Beginning / End of line:
Ctrl + A/Ctrl + E(orHome/End) - Page Up / Page Down:
Ctrl + Y/Ctrl + V - Top / Bottom of file:
Alt + \/Alt + / - Jump to a specific line:
Ctrl + _(orCtrl + Shift + -), then type the line number and pressEnter.
Selecting Blocks of Text
- Move the cursor to the start of the text you want to select and press
Alt + A(orCtrl + 6) to set a mark. - Move the cursor with the arrow keys to highlight the block.
- Press
Ctrl + Kto cut orAlt + 6to copy, then move to the destination and pressCtrl + Uto paste.
4. Customizing Your Workflow (~/.nanorc)
You can configure nano’s default behavior permanently by creating or editing a .nanorc file in your home directory (nano ~/.nanorc).
# Show line numbers in the left margin
set linenumbers
# Enable mouse support (click to move cursor, scroll wheel)
set mouse
# Convert tabs to spaces and set tab width to 4
set tabsize 4
set tabstospaces
# Automatically indent new lines to match the previous line
set autoindent
# Enable syntax highlighting for common languages and configs
include "/usr/share/nano/*.nanorc"
5. Common Troubleshooting
Permission Denied on Save: If you opened a system file without
sudoand get a permission error when pressingCtrl + O, you don’t need to lose your work. Save it to/tmp/temp.conf, exit nano, and move it over withsudo mv /tmp/temp.conf /path/to/original.Weird Pasting Indentation: Pasting multi-line code from your clipboard can sometimes cause cascading indentation (“stair-stepping”). Press
Alt + Ito temporarily toggleautoindentoff before pasting, then pressAlt + Iagain afterward.Accidental Suspend (
Ctrl + Z): If you pressCtrl + Zand drop back to your shell, nano is still running in the background. Typefgand pressEnterto bring it right back.