In one minute
nanoandvimare text editors that run inside the terminal.- A server has no VS Code and no mouse. When a config needs fixing at 2 a.m., you edit it right there in the terminal.
nanoworks like a normal notepad: you just type, and the shortcuts are shown at the bottom.vimhas modes: one mode for typing text and another where keys are commands.- 👉 Any quick change to a file on a server: a config value, a hosts entry, a typo in a script. For changes on many servers, you use automation instead (see "Editing without an editor").
You do not need to master vim. You must be able to open a file, change it, save it and quit without panic.
nano: the easy one
A simple editor. What you type goes straight into the file.
👉 Your first choice for quick edits, if it is installed.
nano /etc/hostsnano shows its shortcuts at the bottom of the screen. ^ means the Ctrl key.
| Keys | What it does |
|---|---|
| Type normally | Add text |
| Ctrl+O, then Enter | Save ("write Out") |
| Ctrl+X | Exit |
| Ctrl+W | Search ("Where is") |
| Ctrl+K / Ctrl+U | Cut a line / paste it back |
| Alt+U | Undo |
Remember it: O = save Out, X = eXit.
vim: the one that is always there
vi is on almost every Linux and Unix system, even tiny containers and network devices. vim ("vi improved") is the modern version.
On a minimal server, nano may be missing, but vi is almost always there.
How it works: modes. This is the one idea that makes vim make sense. Keys do different things depending on the mode you are in.
| Mode | What keys do | How to get there |
|---|---|---|
| Normal | Keys are commands (move, delete, copy) | Press Esc |
| Insert | Keys type text | Press i |
| Command-line | Run commands like save and quit | Type : from normal mode |
Think of it like: a TV remote with a "typing mode" button. In normal mode the buttons change channels. Press
iand they type letters. Press Esc to get the remote back.
The survival sequence
Learn just these five steps first:
vim notes.txt open the filei start typing (insert mode)...type...Esc back to normal mode:wq write and quit| Command | Meaning |
|---|---|
:w | Save (write) |
:q | Quit (refuses if you have unsaved changes) |
:wq or :x | Save and quit |
:q! | Quit and throw away your changes. Your emergency exit |
Stuck in vim? Press Esc twice, type :q!, then press Enter. You are out, and nothing was changed.
Moving and editing faster (normal mode)
👉 Once the survival sequence feels easy, add these one at a time.
| Keys | What it does |
|---|---|
gg / G | Go to the top / bottom of the file |
0 / $ | Start / end of the line |
/text then n | Search for "text", then jump to the next match |
dd | Delete (cut) the current line |
yy / p | Copy (yank) the line / paste it below |
u / Ctrl+R | Undo / redo |
:set number | Show line numbers |
:%s/old/new/g | Replace every "old" with "new" in the whole file |
:42 | Jump to line 42 |
A real example: an error says the problem is on line 37 of a config. Open the file, type :37, fix it, then :wq.
Editing without an editor
Changing a file with a single command, no editor opened.
Scripts and automation cannot press keys in an editor.
👉 In scripts, CI/CD jobs, and any time the same change must happen on many servers.
echo "10.0.3.10 cache1" >> /etc/hostssed 's/web1/web-1/g' /etc/hosts- The first line adds a line to the end of the file.
sed 's/old/new/g' fileprints the file with every "old" replaced by "new".sed -ichanges the file itself. You will learnsedproperly in the Bash Scripting course.
In the practice terminal, nano and vim explain how to write files instead, because full-screen editors need a real machine. Open a live lab to practise them for real.
nano notes.txtReal-world scenario: changing an nginx setting safely
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak # 1. back upsudo vim /etc/nginx/nginx.conf # 2. editsudo nginx -t # 3. test the configsudo systemctl reload nginx # 4. applyBack up → edit → test → apply. Many services have a "test my config" command (nginx -t, sshd -t, apachectl configtest). Use it every time. A broken config plus a restart means the service goes down.
Common mistakes
- Typing text in vim's normal mode. Your letters become commands and change things. Press
uto undo, Esc, thenibefore you type. - Editing a root-owned file as a normal user, then not being able to save. Quit with
:q!and open it again withsudo. - Ignoring swap-file warnings. If vim says a
.swpfile exists, someone else may be editing the same file, or an earlier session crashed. Read the message before you choose. - Windows line endings. Files edited on Windows can carry hidden
\rcharacters. These break shell scripts with "bad interpreter" errors.
Practice
- In a live lab, open
notes.txtin nano, add a line, save and exit. - Open the same file in vim, delete the first line with
dd, undo withu, then quit without saving using:q!. - In vim, replace every "devops" with "DevOps" using
:%s/devops/DevOps/gand save. - Without any editor, add a line to a file with
>>and show it withtail -n 1.
Cheat sheet
| Task | nano | vim |
|---|---|---|
| Open | nano file | vim file |
| Start typing | Just type | i |
| Save | Ctrl+O, Enter | :w |
| Quit | Ctrl+X | :q |
| Quit without saving | Ctrl+X, then N | :q! |
| Search | Ctrl+W | /text |
| Undo | Alt+U | u |
Key takeaways
nanois the easy editor: just type, then Ctrl+O to save and Ctrl+X to exit.vimis on every server. Pressito type, Esc to stop,:wqto save and quit.:q!gets you out of vim without saving.- Back up a config, edit it, test it, and only then reload the service.