Text Editors: Nano & Vim
Sooner or later you will need to edit a file on a Linux server. There is no point-and-click app like Notepad — instead you edit files right inside the terminal (the text-based window where you type commands). The two editors you will meet everywhere are nano (a text editor that is simple and shows you the shortcuts on screen) and vim (a powerful text editor that is installed on practically every Linux machine on earth). This page teaches enough of both to be productive on Ubuntu 22.04 / 24.04 LTS today.
Why you need a terminal editor
When you log into a remote server over SSH (Secure Shell — a way to control another computer over the network from your own terminal), you usually have no graphical desktop. The only way to change a config file like /etc/nginx/sites-available/default or /etc/ssh/sshd_config is to open it in a terminal editor, make your change, and save. Knowing at least one editor is not optional for DevOps work — it is a daily task.
There are two main choices, and they suit different moments:
| Editor | Best for | Learning curve | Where it lives |
|---|---|---|---|
| nano | Quick edits, beginners, copy-paste a config | Almost none | /usr/bin/nano (preinstalled on Ubuntu) |
| vim | Power editing, scripting, any server | Steep at first | /usr/bin/vim (on every Unix box) |
Recommendation: Start with nano so you are never stuck. But learn the vim basics below, because some minimal servers and containers only ship with
vi/vimand no nano at all. Knowing how to save and quit vim has rescued countless engineers.
Nano: the friendly editor
Nano is the easiest editor to learn because it prints its shortcuts along the bottom of the screen. The ^ symbol you see there means the Ctrl key. So ^O means “press Ctrl and O together”.
Opening and editing a file
To open (or create) a file, just type nano followed by the filename. Editing a system file needs sudo (run a command as the administrator/root user):
sudo nano /etc/hosts
Once open, you simply type. Arrow keys move the cursor, Backspace deletes — it behaves like a basic text box.
Saving and exiting
These are the two shortcuts you must remember:
- Ctrl+O — “write Out” (save). Press it, then press Enter to confirm the filename.
- Ctrl+X — exit nano. If you have unsaved changes it asks “Save modified buffer?”; press Y then Enter to save, or N to discard.
A few more handy nano shortcuts:
| Shortcut | What it does |
|---|---|
Ctrl+O | Save the file |
Ctrl+X | Quit |
Ctrl+W | Search (“Where is”) |
Ctrl+K | Cut the current line |
Ctrl+U | Paste (uncut) the line |
Ctrl+_ | Go to a line number |
Ctrl+G | Open the help screen |
When to use nano: quick one-off edits, pasting a config block someone gave you, or any time you do not want to think. When not to: large refactors or repetitive edits where vim’s commands are far faster.
Vim: powerful and everywhere
Vim feels strange at first because it has modes. A mode is a state that changes what your keystrokes do. The big surprise for beginners is that you cannot just start typing — you have to enter the right mode first.
The three modes you need:
- Normal mode — where you land when vim opens. Keys are commands (move, delete, copy), not text. Press Esc to return here from any other mode.
- Insert mode — where you actually type text. Enter it by pressing i.
- Command-line mode — for saving, quitting, and searching. Enter it by pressing : (colon) from normal mode.
Opening a file
sudo vim /etc/ssh/sshd_config
When it opens you are in normal mode. To type, press i (insert). When done typing, press Esc to go back to normal mode.
Saving and quitting
From normal mode, press : then type one of these and hit Enter:
| Command | What it does |
|---|---|
:w | Write (save) the file |
:q | Quit |
:wq | Save and quit (the one you will use most) |
:x | Save and quit (same as :wq) |
:q! | Quit and throw away all changes |
:w !sudo tee % | Save a file you opened without sudo (handy escape hatch) |
The classic gotcha: if you ever feel trapped in vim, press Esc first (to guarantee you are in normal mode), then type
:q!and Enter to quit without saving. That sequence always works.
Essential editing keys
These all run in normal mode. They are fast once they become muscle memory:
| Key | Action |
|---|---|
i | Insert before the cursor |
a | Insert after the cursor |
o | Open a new line below and insert |
dd | Delete (cut) the whole current line |
yy | Yank (copy) the current line |
p | Paste below the cursor |
u | Undo |
Ctrl+r | Redo |
gg | Jump to the top of the file |
G | Jump to the bottom of the file |
/word | Search forward for “word” — press n for next match |
:set number | Show line numbers |
A typical edit looks like this: press i, type your change, press Esc, then :wq and Enter.
Searching text
In normal mode, type / followed by what you are looking for and press Enter:
/PermitRootLogin
Vim jumps to the first match. Press n for the next match, N for the previous one. This is great for finding a setting buried deep in a long config file.
A quick comparison
| Question | nano | vim |
|---|---|---|
| Easy to learn? | Yes, shortcuts shown on screen | No, needs practice |
| Has modes? | No | Yes (normal / insert / command) |
| Save and quit | Ctrl+O, then Ctrl+X | :wq |
| On every server? | Usually, but not guaranteed | Yes (or vi) |
| Fast for big edits? | No | Yes |
Best Practices
- Always edit system files with
sudo— without it you cannot save and you waste your edits. If you forget, use:w !sudo tee %in vim to recover. - Back up before risky edits:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakso you can roll back. - Validate config after editing rather than restarting blindly — e.g.
sudo nginx -torsudo sshd -tto catch syntax errors before they break a service. - Learn vim’s
:q!escape so you are never stuck in an editor on an unfamiliar box. - Pick one editor and get fluent — nano is plenty for most DevOps tasks; add vim once you find yourself making the same edit over and over.
- Avoid editing files while a deploy is running to prevent half-written configs being picked up by a restart.