On this page
- Is nano already installed?
- How do you open, save and quit?
- How do you move around a long file quickly?
- How do you search and replace text?
- How do you cut, copy and paste lines?
- How do you edit root-owned files safely?
- How do you make nano the default editor?
- Which nanorc settings help on a server?
- What if nano behaves oddly?
- Questions
Is nano already installed?
On Ubuntu 24.04, Ubuntu 22.04, Debian 12 and Debian 13 nano is part of the standard image and is usually the default editor. Rocky Linux 9 and AlmaLinux 9 minimal installs often include only vi, so add nano yourself.
# Ubuntu / Debian
sudo apt update && sudo apt install -y nano
# Rocky Linux / AlmaLinux
sudo dnf install -y nano
nano --version | head -n 1Versions vary by release (Ubuntu 24.04 ships the 7.x series, Rocky 9 the 5.x series). Check what your repository offers with apt policy nano or dnf info nano. Every shortcut on this page works in both.
How do you open, save and quit?
Start nano with a path; if the file does not exist yet, nano creates it on the first save. The two bottom lines list shortcuts, where ^ means Ctrl and M- means Alt.
nano ~/notes.txt
nano +25 /etc/ssh/sshd_config # open at line 25
nano -l ~/app/config.yml # show line numbers- Ctrl+O, then Enter: write the file (you can change the name before pressing Enter).
- Ctrl+X: exit. If there are unsaved changes nano asks Y or N first.
- Ctrl+G: built-in help screen.
- Alt+U and Alt+E: undo and redo.
Current releases also accept Ctrl+S to save in place without the file name prompt, which is quicker during repeated small edits.
How do you move around a long file quickly?
Use Ctrl+_ (or Alt+G) to jump to a line number, which is the fastest way to reach the line an error message mentions. Other movement keys worth learning:
- Ctrl+A and Ctrl+E: start and end of the line.
- Ctrl+Y and Ctrl+V: page up and page down.
- Alt+\ and Alt+/: first and last line of the file.
- Ctrl+C: show the current line and column at the bottom.
On a laptop keyboard where Page keys are awkward, these combinations save a lot of scrolling over a slow SSH link.
How do you search and replace text?
Press Ctrl+W, type the search term and press Enter; press Alt+W to jump to the next match. For replacement press Ctrl+\, enter the old text, then the new text, and answer Y, N or A (all) at each hit.
Search is case-insensitive by default. While the search prompt is open, Alt+C toggles case sensitivity and Alt+R switches to regular expressions, so ^#?PermitRootLogin finds the setting whether it is commented or not.
How do you cut, copy and paste lines?
Press Ctrl+K to cut the current line and Ctrl+U to paste it where the cursor is. Repeating Ctrl+K collects several lines into one buffer, so you can move a whole block in two steps.
- Alt+6: copy the current line without removing it.
- Alt+A: set a mark, move the cursor to select a region, then Ctrl+K or Alt+6 acts on the selection.
- Ctrl+R: insert the contents of another file at the cursor.
Pasting from your local clipboard goes through your terminal program, not nano. If pasted YAML or Python ends up with extra indentation, toggle auto-indent off with Alt+I before pasting and back on afterwards.
How do you edit root-owned files safely?
Prefer sudoedit for anything under /etc. It copies the file to a temporary location, runs nano as your own user, and writes it back with root rights only when you save, so your nano settings apply and the editor never runs as root.
export SUDO_EDITOR=nano
sudoedit /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl reload ssh # Ubuntu/Debian
sudo sshd -t && sudo systemctl reload sshd # Rocky/AlmaAlways test a service config before reloading. A typo in sshd_config can lock you out, so keep your current SSH session open until a second login works. Because our console has no browser terminal, a broken SSH setup is fixed by reinstalling the image, which erases the disk; the SSH hardening tutorial shows a safe order of steps. Add export SUDO_EDITOR=nano to ~/.bashrc to make it permanent.
How do you make nano the default editor?
Set the EDITOR and VISUAL variables, which tools like crontab, git and visudo read. On Debian-based systems the alternatives system also chooses the system-wide default.
echo 'export EDITOR=nano VISUAL=nano' >> ~/.bashrc
source ~/.bashrc
# Ubuntu / Debian system-wide
sudo update-alternatives --config editor
crontab -e # should now open in nanoSee environment variables for how these settings load at login.
Which nanorc settings help on a server?
Put options in ~/.nanorc for your user (or /etc/nanorc for everyone). These lines make config editing easier:
set linenumbers
set tabsize 4
set tabstospaces
set autoindent
set backup
set backupdir "~/.nano-backups"
set historylog
include "/usr/share/nano/*.nanorc"Create the backup folder with mkdir -p ~/.nano-backups; nano then keeps the previous version of each file you save. The include line loads syntax colouring for shell, YAML, nginx, Python and more. Remove tabstospaces when you edit Makefiles, which need real tabs. Options that a release does not know cause a warning at startup, so if you see one on Rocky 9, delete that line.
What if nano behaves oddly?
- Shortcuts do nothing: your terminal may capture Alt keys. Press Esc then the letter instead of Alt plus the letter.
- Long lines run off the right edge: press Alt+S to toggle soft wrapping so the whole line stays visible without changing the file.
- Saved file has Windows line endings: when asked for the file name, press Alt+D to toggle DOS format off.
- Screen garbled after resizing: press Ctrl+L to redraw.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| S1-2 | enough for config editing and small services | 1 vCPU, 2 GB RAM, 55 GB SSD | $6.00/mo | $72.00 for 12 months |
Questions about Edit files with nano
Does nano keep a backup when I save?
Only if you enable it with set backup in nanorc or start it with nano -B. Otherwise the old content is overwritten, so copy important configs first with sudo cp file file.bak.
Can I open two files at once?
Yes. Run nano a.conf b.conf, or press Ctrl+R then Alt+F inside nano to load another file into a new buffer. Switch between them with Alt+, and Alt+.
How do I see line numbers only temporarily?
Press Alt+N to toggle line numbers on and off without changing your nanorc.
Is nano enough for real server work?
For editing configs, unit files and small scripts, yes. Large refactors or many files at once are easier in Vim, which our Vim guide covers.
Why is my change not active after saving?
Most services read their config only at start. Reload or restart the service with systemctl after saving, and test the syntax first where the program offers a check.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Read files with cat, less and tail
- Find files with find and locate
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with Vim
- Read manual pages
- Users and groups
- Ownership with chown and chgrp
- Symbolic and hard links
- Pipes, redirection and xargs