Skip to main content
graphwiz.aigraphwiz.ai
← Back to Cheatsheets

Nano Cheatsheet

DevOps
nanotext-editorterminalgnusysadmin

Nano Cheatsheet

GNU Nano is a simple, modeless terminal editor. Every keystroke is either text or a command — no modes to remember. Ideal for quick edits and SSH sessions.


Nano

GNU Nano is a simple, modeless terminal editor. Every keystroke is either text or a command — no modes to remember. Ideal for quick edits and SSH sessions.

Basic Keybindings

# Start Nano
nano file.txt         # open or create file
nano +10 file.txt     # open at line 10
nano -l file.txt      # show line numbers
nano -B file.txt      # create backup on save (.save)
nano -m file.txt      # enable mouse support
KeyActionDescription
Ctrl+GHelpShow built-in help (lists all shortcuts)
Ctrl+OSaveWrite file to disk (prompts for filename)
Ctrl+XExitClose Nano (prompts to save if modified)
Ctrl+KCutCut current line (or selected text)
Ctrl+UPastePaste last cut text
Ctrl+TSpellRun spell checker (if configured)
Ctrl+JJustifyReformat/justify current paragraph
KeyAction
Ctrl+ABeginning of line
Ctrl+EEnd of line
Ctrl+YPage up
Ctrl+VPage down
Ctrl+FMove forward one character
Ctrl+BMove back one character
Ctrl+SpaceNext word
Alt+SpacePrevious word
Ctrl+_Go to specific line (enter line number)
Alt+GGo to specific line (alternative)
Ctrl+CShow current cursor position (line, column)
Alt+]Go to matching bracket
Alt+( or Alt+)Go to previous/next paragraph

Search & Replace

# Search
Ctrl+W             # search forward
Alt+W              # search backward
Alt+Q              # search without regex

# Replace
Ctrl+\             # search and replace (prompted)
# Enter search text, press Enter
# Enter replacement text
# Y = replace this, N = skip, A = replace all, Ctrl+C = cancel

Cut, Copy & Paste

KeyAction
Alt+AStart/select text marking
Ctrl+6Toggle mark (alternative)
Ctrl+KCut marked text or current line
Alt+6Copy marked text (doesn't cut)
Ctrl+UPaste (uncut) last cut/copied text

File Operations

KeyAction
Ctrl+RInsert file (read a file into current buffer)
Ctrl+OWrite out (save) — Alt+D to save with different name
Ctrl+XExit

.nanorc Configuration

# ~/.nanorc — Nano configuration

# Line display
set linenumbers
set constantshow            # always show cursor position

# Editing behavior
set tabsize 4
set tabstospaces
set autoindent
set cutoff                  # hard-wrap at 80 chars (or set fill 80)
set multibuffer             # insert-to-end, not replacing buffer

# Backup & history
set backup
set historylog
set mouse

# Syntax highlighting
include /usr/share/nano/*.nanorc
# Or include specific files:
include /usr/share/nano/sh.nanorc
include /usr/share/nano/python.nanorc
include /usr/share/nano/javascript.nanorc
include /usr/share/nano/html.nanorc
include /usr/share/nano/yaml.nanorc
include /usr/share/nano/dockerfile.nanorc
include /usr/share/nano/systemd.nanorc

Custom Syntax Highlighting

# Add to ~/.nanorc

# Custom syntax for .env files
syntax "env" "\.env$"
color brightwhite "^.*="
color cyan "^[A-Z_]+="
color yellow "\"[^\"]*\""
color green "\'[^\']*\'"

# Custom syntax for log files
syntax "logs" "\.log$"
color brightred "ERROR"
color brightyellow "WARN(ING)?"
color brightgreen "INFO"
color brightmagenta "DEBUG"
color blue "^[0-9]{4}-[0-9]{2}-[0-9]{2}"

Common Flags

nano                  # basic editor
nano -B file.txt      # create backup file on save
nano -E file.txt      # tab-to-space conversion
nano -l file.txt      # show line numbers
nano -m file.txt      # enable mouse
nano -S file.txt      # smooth scrolling
nano -c file.txt      # show cursor position constantly
nano -i file.txt      # auto-indent new lines
nano -u file.txt      # undo support (default in modern Nano)
nano -r 120 file.txt  # set fill column to 120
nano -v file.txt      # view mode (readonly)
nano -w file.txt      # don't wrap long lines
nano -R file.txt      # enable regular expression search

Practical Nano Workflow

# Quick SSH config edit
ssh server
sudo nano /etc/ssh/sshd_config
# Edit, Ctrl+O to save, Ctrl+X to exit
sudo systemctl restart sshd

# Edit a Docker Compose file with syntax highlighting
nano docker-compose.yml

# Create a new script with line numbers
nano -l -i deploy.sh

# View a log file (readonly mode)
nano -vw /var/log/syslog
# Search with Ctrl+W, page with Ctrl+Y/Ctrl+V


Tips

Enable Syntax Highlighting System-Wide

```bash

Install Nano syntax files (Debian/Ubuntu)

sudo apt install nano-syntax-highlighting

Enable all syntax highlighting for all users

echo "include /usr/share/nano/*.nanorc" | sudo tee -a /etc/nanorc ```

Choosing Nano

```bash

Quick SSH config edit on a remote server

nano /etc/nginx/nginx.conf

Edit a Docker Compose file with syntax highlighting

nano docker-compose.yml

View a log file (readonly mode)

nano -vw /var/log/syslog ```


Next Steps