Signal9Signal9 — home
online
all lessons

vim Essentials: Editing Without a GUI

Beginner35 min7 steps
What you'll learn
  • 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
  1. 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 in vi/vim or 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 with Esc.
    • 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?

  2. 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:

    • :q Enter — quit (only works if you haven't made changes)
    • :q! Enter — quit and throw away changes. Your panic button.
    • :wq Enter — write (save) and quit
    • ZZ (Normal mode, no colon) — save and quit; :x is 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?

  3. 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 after
    • o — open a new line below · O — open a new line above
    • A — 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 word
    • 0 — start of line · ^ — first non-blank · $ — end of line
    • gg — top of file · G — bottom · 42G or :42 — line 42
    • /pattern Enter — jump to next match; n next, N previous

    Counts multiply motions: 5j moves down 5 lines, 3w jumps 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?

  4. 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): dw deletes a word, dd a whole line, d$ to end of line, 5dd five lines
    • c — change (delete, then Insert mode): cw changes a word
    • y — yank (copy): yy copies a line · p/P paste after/before
    • x — delete the character under the cursor · r — replace one character
    • u — 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: yy then p.

    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?

  5. 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/ — first foo on the current line
    • :s/foo/bar/g — all foo on the current line (g = every match on the line)
    • :%s/foo/bar/g — all foo in 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?

  6. 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: v selects characters, V whole lines, Ctrl-v a block/column (great for columns of config). Then apply an operator (d, y, c).
    • Registers: "ayy yanks a line into register a; "ap pastes it — multiple clipboards.
    • Macros: qa records into register a, do the edits, q stops; @a replays, 14@a replays 14 times, @@ repeats the last replay. Perfect for repetitive line edits.
    • Buffers (multi-file): vim *.env opens several files in one session. :ls lists buffers, :n/:prev walk the argument list, :wa writes all, :wqa writes all and quits. One catch worth knowing before it bites you: :n will not abandon a buffer you have edited but not written — it answers E37: No write since last change. Use :wn (write and next), or :set autowrite once so :n saves for you.

    And a minimal ~/.vimrc makes 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, :sort and :g, a vimrc you can verify, windows and tabs, and :argdo across a whole directory.

    cat > ~/.vimrc <<'EOF'
    syntax on
    set number
    set expandtab tabstop=4 shiftwidth=4
    set ignorecase smartcase hlsearch
    EOF
    Line 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?

  7. nano: the quick-edit alternative

    Use nano for a fast edit when modal editing isn't worth the ceremony.

    nano file opens a modeless editor: type to edit, no modes at all. The shortcut bar at the bottom is the whole manual — ^ means Ctrl:

    • Ctrl-o Enter — save ("write out") · Ctrl-x — exit
    • Ctrl-w — search ("where is") · Ctrl-\ — search & replace
    • Ctrl-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 (vi is 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)?

Recap
  • 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
Survival cheat-sheet
GoalKeys
Back to safety (Normal mode)Esc
Quit, discard changes:q!
Save and quit:wq or ZZ
Insert before / append afteri / a
New line below / aboveo / O
Delete line / word / chardd / dw / x
Change wordcw
Copy line / pasteyy / p
Undo / redo / repeatu / Ctrl-r / .
Top / bottom / line Ngg / G / :N
Search next / prev/pat then n / N
Replace all in file:%s/old/new/g
Record / replay macroqa…q / @a
Next buffer, saving as you go / write all+quit:wn / :wqa