How to Edit Files on a Linux Server with Nano (Save, Search, Undo)

Almost every tutorial for a Linux server includes the phrase "edit the config file". There's no Notepad on a server, so you edit in the terminal. Nano is the friendly option: the commands are written along the bottom of the screen, and the only thing you really have to learn is how to save.
Quick reference
| Action | Keys |
|---|---|
| Open a file | nano filename (add sudo for system files) |
| Save | Ctrl + O, then Enter |
| Exit | Ctrl + X |
| Search | Ctrl + W |
| Search and replace | Ctrl + \ |
| Cut a line | Ctrl + K |
| Paste | Ctrl + U |
| Undo / redo | Alt + U / Alt + E |
| Go to a line number | Ctrl + _ |
1. Open a file
nano notes.txt
sudo nano /etc/ssh/sshd_config
If the file doesn't exist, nano creates it when you save. Files in /etc belong to root, so use sudo or you'll be told the file is unwritable when you try to save.
2. Move around and type
Arrow keys move the cursor. Type as normal. Page Up / Page Down jump a screen at a time, Ctrl + A goes to the start of the line and Ctrl + E the end.
The ^ in the bar at the bottom means Ctrl, and M- means Alt. So ^O is Ctrl + O.
3. Save and exit
- Ctrl + O (write out), then Enter to confirm the filename — saves and keeps editing.
- Ctrl + X — exits. If you've got unsaved changes it asks Save modified buffer? Press Y then Enter to save, N to throw the changes away.
4. Search
Ctrl + W, type a word, Enter. Alt + W finds the next match. Config files are long; this is how you find PasswordAuthentication without scrolling.
For search and replace: Ctrl + \, type the old text, Enter, type the new text, Enter, then A to replace all.
5. Cut, copy and paste lines
- Ctrl + K cuts the whole current line. Press it on several lines in a row to cut them all.
- Ctrl + U pastes them.
- To copy instead of cut: Alt + 6.
- To select a block: Alt + A to start selecting, move with arrows, then Ctrl + K or Alt + 6.
Pasting from your computer's clipboard is the normal terminal paste (right-click in Windows Terminal, Cmd + V on a Mac).
6. Undo
Alt + U undoes, Alt + E redoes. Or just exit with Ctrl + X and N to abandon everything since the last save.
Useful extras
- Ctrl + _ then a number jumps to that line — handy when an error message says "line 42".
- nano -l file shows line numbers. Make it permanent with echo "set linenumbers" >> ~/.nanorc.
- nano -B file keeps a backup copy (filename~) every time you save.
Troubleshooting
"File is unwritable." You opened a root-owned file without sudo. Exit, then sudo nano it.
You're stuck in Vim instead. Some commands open the default editor, which might be Vim. Press Esc, type :q! and Enter to escape. Then set nano as the default: sudo update-alternatives --config editor.
Lines wrap strangely / long lines cut off. Nano shows a $ where a line continues. Use Alt + S to toggle soft wrapping.
Wrapping up
Ctrl + O to save, Ctrl + X to leave, Ctrl + W to search, Ctrl + K and Ctrl + U to move lines. That's enough for every config file in this series.
