0.3 — Survival Guide — Basic Modes and Exiting
0.3 — Survival Guide — Basic Modes and Exiting#
This lesson covers the absolute minimum you need to survive in Vim. By the end, you’ll be able to open a file, make changes, save, and exit—without panic.
The Most Important Thing: Modes#
Vim is a modal editor. This is the key concept that confuses beginners:
- In most editors, pressing
jtypes the letter “j” - In Vim’s Normal mode, pressing
jmoves the cursor down one line - To type the letter “j”, you must be in Insert mode
The Three Modes You Need to Know Now#
| Mode | Purpose | How to Enter | How to Exit |
|---|---|---|---|
| Normal | Navigate, delete, copy, paste | Press Esc | Enter another mode |
| Insert | Type text | Press i | Press Esc |
| Command-line | Save, quit, search | Press : | Press Esc or Enter |
Rule #1: When in doubt, press
Esc. This returns you to Normal mode, which is the “safe” mode.
Entering and Exiting Insert Mode#
Entering Insert Mode#
From Normal mode, press i to enter Insert mode. You’ll see -- INSERT -- at the bottom of the screen.
Now you can type text normally. Press Esc to return to Normal mode.
There are several ways to enter Insert mode:
| Key | Action |
|---|---|
i | Insert before cursor |
I | Insert at beginning of line |
a | Append after cursor |
A | Append at end of line |
o | Open new line below |
O | Open new line above |
For now, just remember i to insert and Esc to exit.
The Escape Key#
The Esc key is your friend. It:
- Returns you to Normal mode from any other mode
- Cancels partially-typed commands
- Dismisses error messages
Some people remap Caps Lock to Esc for easier access, or use Ctrl-[ which is equivalent to Esc.
Saving and Exiting#
This is what everyone wants to know: how do I exit Vim?
Basic Save and Exit Commands#
From Normal mode, type these commands (they start with :):
| Command | Action |
|---|---|
:w | Write (save) the file |
:q | Quit Vim |
:wq | Write and quit (save and exit) |
:q! | Quit without saving (discard changes) |
:wq! | Write and quit, forcing if needed |
ZZ | Save and quit (Normal mode shortcut) |
ZQ | Quit without saving (Normal mode shortcut) |
Step-by-Step: Edit, Save, and Exit#
- Open a file:
vim test.txt - Press
ito enter Insert mode - Type some text: “Hello, Vim!”
- Press
Escto return to Normal mode - Type
:wqand press Enter to save and exit
When Vim Won’t Let You Quit#
If you see an error like:
E37: No write since last change (add ! to override)
This means you have unsaved changes. You can:
:wto save, then:qto quit:wqto save and quit:q!to quit without saving (discarding changes)
Basic Navigation#
In Normal mode, use these keys to move around:
| Key | Movement |
|---|---|
h | Left |
j | Down |
k | Up |
l | Right |
Why hjkl? On the original keyboard Bill Joy used to create vi, these keys had arrow symbols on them. More importantly, they keep your fingers on the home row.
Tip: Think of
jas having a downward hook, pointing down.
Alternative: Arrow Keys#
Arrow keys also work in Vim, but experienced users avoid them because they require moving your hand from the home row. As you get comfortable, try to use hjkl instead.
Basic Editing in Normal Mode#
You don’t need to be in Insert mode to delete text:
| Key | Action |
|---|---|
x | Delete character under cursor |
dd | Delete entire line |
u | Undo last change |
Ctrl-r | Redo (undo the undo) |
Undo is Your Safety Net#
Made a mistake? Press u to undo. Vim has unlimited undo levels by default, so you can keep pressing u to go further back.
Undid too much? Press Ctrl-r to redo.
Putting It All Together: Survival Workflow#
Here’s the minimal workflow for editing a file:
1. vim filename.txt # Open file
2. i # Enter Insert mode
3. [type your changes] # Edit text
4. Esc # Return to Normal mode
5. :wq # Save and quit
If something goes wrong:
Esc # Return to Normal mode
u # Undo if needed
:q! # Quit without saving if you want to start over
Practice Exercise#
Create and edit a test file:
- Open terminal and run:
vim practice.txt - Press
ito enter Insert mode - Type: “This is my first Vim edit.”
- Press
Esc - Press
oto open a new line below and enter Insert mode - Type: “Learning Vim is fun!”
- Press
Esc - Type
:wqand press Enter
Now reopen the file and make changes:
- Run:
vim practice.txt - Use
jandkto move between lines - Press
ddto delete a line - Press
uto undo - Type
:q!to quit without saving
Quick Reference Card#
Print this or keep it handy:
MODES
i Enter Insert mode
Esc Return to Normal mode
: Enter Command-line mode
SAVE & QUIT
:w Save
:q Quit
:wq Save and quit
:q! Quit without saving
ZZ Save and quit (shortcut)
NAVIGATION
h j k l Left, Down, Up, Right
BASIC EDITING
x Delete character
dd Delete line
u Undo
Ctrl-r Redo
EMERGENCY
Esc Return to Normal mode (always safe)
:q! Abandon everything and exit
Summary#
You now know enough to survive in Vim:
- Modes matter: Normal for commands, Insert for typing, Command-line for saving/quitting
- Esc is your friend: Returns you to Normal mode from anywhere
:wqsaves and quits: The most important command to rememberuundoes mistakes: Your safety net
In the next lesson, we’ll review what we’ve learned and test your knowledge with a quiz. Then we’ll dive deeper into Vim’s modes and commands!