learn / vim / v1 · 1 of 1 · 6 min
The editor that's waiting on every server you'll ever log into — and the four keystrokes that get you out of it.
## The editor that's already there
You log into a server at 2 a.m. because something is on fire. You need to change one line in a config file. There's no mouse, no Visual Studio Code, no graphical anything. There's a blinking cursor and whatever editor came installed.
Some version of vi comes with almost every Linux and Unix system ever built. It's even required by the Portable Operating System Interface (POSIX), the standard that defines what a Unix system must include. Vim ("Vi IMproved", released by Bram Moolenaar in 1991) is the version you'll usually find.
You'll also land in it without asking:
git commitwith no message opens an editor — often Vimcrontab -eandvisudoopen whatever$EDITORis set to, usually vi- the recovery shell on a broken box may have nothing else
In security work this shows up constantly: editing sshd_config during a hardening sprint, fixing a firewall rule over a secure shell (SSH) session, cleaning a crontab an attacker left behind. In defense competitions the scoring boxes are often minimal installs. You can't count on nano being there, but vi is.
## Why your keys seem broken
The first time you open Vim and type hello, nothing appears. Or worse, things start disappearing. Vim isn't broken. It's in a different mode.
i a o (start typing)
┌────────────┐ ───────▶ ┌────────────┐
│ NORMAL │ │ INSERT │
│ (commands) │ ◀─────── │ (typing) │
└────────────┘ Esc └────────────┘
│ v │ :
▼ ▼
VISUAL COMMAND-LINE
(select) (:w :q …)Vim opens in Normal mode, where every key is a command. That's the point: editing is mostly moving and changing text, not typing new text, so Vim gives the whole keyboard to commands.
ienters Insert mode before the cursor, andEscleaves itventers Visual mode to select text:opens the command-line at the bottom for things like saving and quitting
That hello you typed? In Normal mode, h moves left, e jumps to the end of a word, l moves right, and o opens a new line and drops you into Insert mode. Nothing was broken. Vim did exactly what you asked.
You open a file in Vim and type "hello". The cursor jumps around and a new line appears. What happened?
## How to quit (finally)
Every "how do I exit Vim" joke comes down to this: the quit commands live on the command-line, and you can only open it from Normal mode.
- Press
Escto make sure you're in Normal mode - Type
:and the cursor jumps to the bottom of the screen - Type the command, then press
Enter
The commands you need:
:w— write (save):q— quit (refuses if you have unsaved changes):wqor:x— save and quit:q!— quit and throw away your changes. The!means "I'm sure."
You're stuck in Vim, half-typed into a file, and want out without saving. Put the steps in order.
- 1.Type q! — quit, and I'm sure
- 2.Press Esc (maybe twice) to get back to Normal mode
- 3.Press Enter
- 4.Type a colon to open the command-line
You're in Normal mode. Type exactly what you'd enter to quit and throw away your changes.
Now the opposite. You fixed the config line and want to save and quit. What do you type?
## Practice without fear
Vim comes with a built-in, 30-minute interactive lesson. In any terminal, run:
$ vimtutor
It opens a practice copy of a file, so you can't break anything. Do it once this week. Then come back for Modes & Moving Around, the next module in this track.
Real talk: nobody learns Vim in a weekend. Learn to get in, make one change and get out. Speed comes from using it a little every day, not from memorizing a cheat sheet.