Table of Contents
Starting nano
To open a file with nano:
- Edit an existing file:
nano filename.txt- Create a new file (if it doesn’t exist, it will be created when you save):
nano newfile.txt- Open a file at a specific line number:
nano +25 filename.txtCommon optional flags:
-l— show line numbers-c— show cursor position (line, column)-m— enable mouse support (if your terminal supports it)-i— auto-indent new lines
Example:
nano -l -m script.shnano interface basics
When nano opens, you’ll see:
- The text area in the middle
- A status line near the bottom (shows file name, warnings, search results)
- A help bar at the very bottom with commands like:
^XExit^OWrite Out^WWhere Is
^ means “Ctrl”. So:
^Xmeans pressCtrl + X^Omeans pressCtrl + O
M- means “Meta” (usually the Alt key):
M-UmeansAlt + U
Basic editing
Moving the cursor
- Left/right:
←/→arrow keys - Up/down:
↑/↓arrow keys - Previous/next word:
Ctrl + ←/Ctrl + →(depends on terminal) - Start of line:
Ctrl + A - End of line:
Ctrl + E - Start of file:
Ctrl + HomeorAlt + \ - End of file:
Ctrl + EndorAlt + / - Go to specific line (and column):
Ctrl + _then type line (and optionally,column)
Example: Ctrl + _, then 42 jumps to line 42. 42,10 jumps to line 42, column 10.
Inserting and deleting text
- Just type to insert text at the cursor.
- Backspace: delete character before cursor.
Delete: delete character under cursor.Ctrl + D: also delete character under cursor.Ctrl + K: cut the entire current line.Ctrl + J: re-justify (re-wrap) a paragraph of text (handy in plain text files).
Saving and exiting
Saving (Write Out)
- Save:
Ctrl + O nanowill show the file name at the bottom.- Press
Enterto confirm, or change the name to save as a different file.
Exiting
- Exit:
Ctrl + X - If there are unsaved changes, nano will ask:
- “Save modified buffer?” — press:
Yto save, thenEnterto confirm filenameNto exit without savingCtrl + Cto cancel and return to editing
Searching and replacing
Searching text
- Search:
Ctrl + W - Type search term, press
Enter. - Next match:
Ctrl + W, thenEnteragain (repeats last search).
Useful search options (toggled after pressing Ctrl + W, then Alt + key):
- Case-sensitive:
Alt + C - Search backwards:
Alt + B - Use regular expressions:
Alt + R(basic regex support)
Find and replace
- Replace:
Ctrl + \(sometimes shown as^\\) - Enter the text to search for, press
Enter. - Enter the replacement text, press
Enter. - For each match, nano will ask:
Y— replace this oneN— skipA— replace all remaining without askingCtrl + C— stop replacing
Cutting, copying, and pasting
nano uses a “mark and cut” style selection.
Cutting and copying lines
- Cut current line:
Ctrl + K - Repeatedly pressing
Ctrl + Kcuts subsequent lines and appends them to the same cut buffer. - Paste:
Ctrl + U(inserts last cut text at cursor) - Copy current line (without deleting it):
Ctrl + K(cut the line)- Immediately
Ctrl + Utwice (paste twice: one stays, one is the copy) - Move to where you want the copy and
Ctrl + Uagain
Cutting/copying a selection
- Move cursor to start of the text.
- Set the mark:
Ctrl + ^(Ctrl + Shift + 6 on many keyboards) - Use arrows to extend selection.
- Cut selection:
Ctrl + K - Paste selection:
Ctrl + U
To “copy” instead of cut:
- After step 4 (cut), immediately hit
Ctrl + Uto restore text where it was, then move and paste again withCtrl + U.
Undo and redo
- Undo:
Alt + U - Redo:
Alt + E
Undo/redo covers most editing actions (insert, delete, cut, paste, search-replace, etc.), but history depth can be limited depending on version.
Working with multiple files and buffers
nano can work with multiple open files in one session.
Opening additional files
- Insert file into current cursor position:
Ctrl + R - Type file name, press
Enter. - Open a new buffer/file:
Ctrl + R, thenCtrl + Xat the prompt - Type a filename and press
Enter.
Switching between open buffers
- Next buffer:
Alt + . - Previous buffer:
Alt + ,
nano shows the current buffer’s name in the status line.
Indentation and code editing helpers
nano is basic but has a few coding-friendly features, often enabled via options or config.
Common shortcuts:
- Auto-indent toggle:
Alt + I - Comment/uncomment selected lines (in many nano builds):
Alt + 3 - Indent selection (with mark set):
Alt + } - Unindent selection:
Alt + {
These depend on your nano version and config; if they don’t work, your distro’s defaults may differ.
Enabling line numbers and syntax highlighting
Some distros enable these by default; others don’t. You can control them from within nano and via config.
Temporary (per session)
While in nano:
- Toggle line number display (if supported):
Alt + N - Toggle soft wrapping:
Alt + L
Or start nano with options:
- With line numbers:
nano -l file - With soft wrap:
nano -$ file
Permanent (via config file)
User-level config: ~/.nanorc
System-wide config: /etc/nanorc (requires root)
Example ~/.nanorc:
set linenumbers
set softwrap
set tabsize 4
set autoindent
include "/usr/share/nano/*.nanorc"Common options:
set linenumbers— show line numbersset softwrap— wrap long lines visuallyset tabsize 4— set tab widthset autoindent— auto-indent new linesset mouse— enable mouse clicks
The include line loads syntax highlighting rules provided by your distro (language-specific .nanorc files).
Using nano as default editor
Some commands (like crontab -e, git commit, or editing system configs via tools) use the EDITOR or VISUAL environment variables.
To make nano your default editor for your user:
Add to ~/.bashrc or ~/.profile:
export EDITOR=nano
export VISUAL=nanoApply immediately in the current shell:
export EDITOR=nano
export VISUAL=nanoNow tools that respect these variables will open nano instead of another editor.
Practical workflows and tips
- Quick note taking:
nano note.txt- Type your note
Ctrl + O,Enter,Ctrl + X- Editing a config file as root:
sudo nano /etc/filename.conf- Prevent accidental overwrites:
- Use a new filename when saving:
Ctrl + O, then typefilename.conf.bakfor a backup. - Checking your location in the file:
Ctrl + C— shows current line, column, and file info in the status line.
Use Ctrl + G at any time to see nano’s built-in help screen, which lists many of these commands with their key combinations.