vim Essentials: Editing Without a GUI
- Explain vim's modal model and recover to Normal mode from anywhere
- Exit vim with or without saving — never feel trapped again
- Edit efficiently with insert commands, motions, and operator+motion combos
- Search and replace across a whole file with :%s/old/new/g
- Know when registers, macros, and buffers are worth reaching for — and when nano is fine
The one idea that makes vim make sense: modes
Understand why your keys sometimes type text and sometimes do... something else.
On the RHCSA and LFCS exams there is no graphical editor — you edit
/etc/fstab, unit files, and SSH configs invi/vimor not at all. The same is true on most real servers you'll SSH into.Most editors are always in "insert" mode — every key you press types a character. vim is modal: the same keys do different things depending on the mode you're in. This is why people get stuck: they start typing and vim interprets their letters as commands.
The three modes you need:
- Normal mode — the default when vim opens. Keys are commands: move, delete, copy, paste. You are here whenever you're not sure.
- Insert mode — keys type text, like a normal editor. Enter with
i(and friends), leave withEsc. - Command-line mode — "ex" commands typed at the bottom of the screen, started with
:— how you save, quit, and search-and-replace.
Check yourself
You open vim and start typing your name, but the screen does strange things instead of showing your text. Which mode are you in?
Escaping vim, defanged
Get out of vim on purpose — with or without your changes.
To quit, get to Normal mode (
Esc), then type one of::qEnter— quit (only works if you haven't made changes):q!Enter— quit and throw away changes. Your panic button.:wqEnter— write (save) and quitZZ(Normal mode, no colon) — save and quit;:xis the same idea
If you opened a file by accident and want out with no harm done:
Esc:q!Enter. That's it. You are never trapped.Example — not runnable on your lab box
vim /root/motd-draft.txt(vim opens the file full-screen; the bottom line shows the filename)
Opening a file is the shell-side half. Everything after this happens inside vim's modes. Try it: open any file, press i, type a word, press Esc, then :q! to leave without saving — proof the panic button works. Check yourself
You've mangled a config file inside vim and want to leave WITHOUT saving any of it. What do you type from Normal mode?
Getting in: insert commands and motions
Enter Insert mode exactly where you want, and move fast in Normal mode.
From Normal mode, pick the insert command that puts the cursor where you want:
i— insert before the cursor ·a— append aftero— open a new line below ·O— open a new line aboveA— jump to end of line and insert ·I— first non-blank of the line
Moving around (Normal mode):
h j k l— left, down, up, right (arrows work too)w/b— forward/back one word ·e— end of word0— start of line ·^— first non-blank ·$— end of linegg— top of file ·G— bottom ·42Gor:42— line 42/patternEnter— jump to next match;nnext,Nprevious
Counts multiply motions:
5jmoves down 5 lines,3wjumps 3 words. This count-plus-motion idea is the heart of vim's speed.Check yourself
You need to add a new configuration line BELOW the line your cursor is on. Which single key gets you a new line and puts you in Insert mode there?
Editing grammar: operator + motion
Combine operators with motions instead of memorizing hundreds of commands.
vim's power is combining an operator with a motion — the grammar reads like English:
d— delete (cut):dwdeletes a word,dda whole line,d$to end of line,5ddfive linesc— change (delete, then Insert mode):cwchanges a wordy— yank (copy):yycopies a line ·p/Ppaste after/beforex— delete the character under the cursor ·r— replace one characteru— undo ·Ctrl-r— redo ·.— repeat your last change (hugely useful)
To fix a typo'd word: cursor on it,
cw, type the correct word,Esc. To duplicate a line:yythenp.Check yourself
Your cursor is on the word 'true' and you want to replace just that word with 'false'. Which operator+motion drops the word and leaves you typing its replacement?
Search & replace: the config-editing superpower
Change every occurrence in a file with one command — with regex when you need it.
Substitute (command-line mode) follows
:[range]s/old/new/[flags]::s/foo/bar/— firstfooon the current line:s/foo/bar/g— allfooon the current line (g= every match on the line):%s/foo/bar/g— allfooin the whole file (%= every line):%s/foo/bar/gc— same, but confirm each change:10,20s/foo/bar/g— only lines 10–20
The pattern is a regex, so
:%s/ *$//strips trailing spaces from every line. After a substitute, vim reports how many changes on how many lines — read it, it's your sanity check.Check yourself
What command replaces EVERY occurrence of 'projectx' with 'signal9' in the whole file?
Once you're comfortable: visual mode, registers, macros, buffers
Recognize the power tools so you know they exist when a task begs for them.
- Visual mode:
vselects characters,Vwhole lines,Ctrl-va block/column (great for columns of config). Then apply an operator (d,y,c). - Registers:
"ayyyanks a line into registera;"appastes it — multiple clipboards. - Macros:
qarecords into registera, do the edits,qstops;@areplays,14@areplays 14 times,@@repeats the last replay. Perfect for repetitive line edits. - Buffers (multi-file):
vim *.envopens several files in one session.:lslists buffers,:n/:prevwalk the argument list,:wawrites all,:wqawrites all and quits. One catch worth knowing before it bites you::nwill not abandon a buffer you have edited but not written — it answersE37: No write since last change. Use:wn(write and next), or:set autowriteonce so:nsaves for you.
And a minimal
~/.vimrcmakes daily editing pleasant (the defaults are exam-sufficient):The vim Power Tools lesson (
vim-power-guide) takes each of these further: marks, filtering through Unix tools,:sortand:g, a vimrc you can verify, windows and tabs, and:argdoacross a whole directory.cat > ~/.vimrc <<'EOF' syntax on set number set expandtab tabstop=4 shiftwidth=4 set ignorecase smartcase hlsearch EOFLine numbers, sane search (case-insensitive unless you type a capital), spaces for tabs, and syntax colors. Six lines, most of the comfort. Check yourself
You recorded a one-line cleanup into register a with qa...q and there are 14 more identical lines below. What replays the macro 14 times?
- Visual mode:
nano: the quick-edit alternative
Use nano for a fast edit when modal editing isn't worth the ceremony.
nano fileopens a modeless editor: type to edit, no modes at all. The shortcut bar at the bottom is the whole manual —^means Ctrl:Ctrl-oEnter— save ("write out") ·Ctrl-x— exitCtrl-w— search ("where is") ·Ctrl-\— search & replaceCtrl-k/Ctrl-u— cut / paste a line
nano is fine for one-line tweaks. The reasons vim still wins the deep skill investment: it's on every system (
viis guaranteed even on minimal installs; nano isn't), and nothing in nano approaches:%s, macros, or multi-file buffers.Check yourself
In nano, which keystroke saves the file (before you exit)?
- vim is modal: Esc returns to Normal mode (keys = commands); i/a/o enter Insert mode; : opens command-line mode
- You are never trapped: Esc :q! Enter always exits without saving
- Editing is operator + motion: dd, cw, yy/p — with u to undo, Ctrl-r to redo, and . to repeat
- :%s/old/new/g is the config-editing superpower (add c to confirm each change)
- Macros (qa…q, 14@a) turn 15 identical edits into one; buffers (:wn, :wa) handle multi-file sessions
- nano is fine for quick tweaks, but vi is the only editor guaranteed on every box — and on the exams
| Goal | Keys |
|---|---|
| Back to safety (Normal mode) | Esc |
| Quit, discard changes | :q! |
| Save and quit | :wq or ZZ |
| Insert before / append after | i / a |
| New line below / above | o / O |
| Delete line / word / char | dd / dw / x |
| Change word | cw |
| Copy line / paste | yy / p |
| Undo / redo / repeat | u / Ctrl-r / . |
| Top / bottom / line N | gg / G / :N |
| Search next / prev | /pat then n / N |
| Replace all in file | :%s/old/new/g |
| Record / replay macro | qa…q / @a |
| Next buffer, saving as you go / write all+quit | :wn / :wqa |
