Skip to content

Instantly share code, notes, and snippets.

@markcallen
Created August 28, 2025 00:43
Show Gist options
  • Select an option

  • Save markcallen/58ac3d7db053b3e11274a20714619f73 to your computer and use it in GitHub Desktop.

Select an option

Save markcallen/58ac3d7db053b3e11274a20714619f73 to your computer and use it in GitHub Desktop.
Example file for learning vim
Vim in One File — A Practical Teaching Guide
===============================================
Use this file to teach someone Vim from zero to productive. Read top to bottom, or jump by section.
0) Panic Button — How to quit
-----------------------------
- Press ESC to make sure you’re in Normal mode.
- Type :q and press ENTER to quit (fails if file changed).
- Type :q! to quit and discard changes.
- Type :w to save. :wq (or :x) saves and quits.
- If you’re truly stuck: hold ESC, then type :qa! to quit all.
1) Vim’s Core Idea — Modes
--------------------------
Vim has modes. You spend most time in **Normal** mode, where keys are commands.
- Normal: ESC to enter. Move, delete, yank (copy), paste.
- Insert: i to insert before cursor, a to insert after, o to open a new line below, O above.
Exit Insert with ESC.
- Visual: v selects characters, V selects lines, CTRL+v selects a column block.
- Command-line (Ex): : to run commands, / to search forward, ? to search backward.
2) Moving with Purpose
----------------------
Single steps:
- h left, j down, k up, l right.
- 0 start of line, ^ first non-blank, $ end of line.
By words and bigger chunks:
- w next word start, e next word end, b previous word start.
- ) next sentence, ( previous sentence. } next paragraph, { previous paragraph.
By lines and file:
- gg go to first line, G go to last line, 42G go to line 42.
- H top of screen, M middle, L bottom.
- % jump to matching (), {}, [].
Search moves:
- /text ENTER to search forward; n next, N previous.
- ?text ENTER to search backward.
- * search word under cursor forward, # backward.
Marks (bookmarks):
- m a sets mark a on this position. ' a jumps to it. '' returns to last jump.
3) Counts + Motions = Power
---------------------------
Prepend a number to repeat. Examples:
- 5j moves down five lines.
- 3w jumps three words forward.
- d2w deletes two words. 4dd deletes four lines.
- 10>> indents ten lines to the right.
4) Editing Essentials
---------------------
Insert quickly:
- i insert before cursor. a after. o new line below. O new line above.
- r x replaces one character. R replaces many until ESC.
Delete / change / yank (copy):
- x delete character under cursor. X delete before cursor.
- d{motion} delete by motion: dw delete word, d$ delete to end of line, d^ delete to first non-blank.
- dd delete current line. D delete to end of line.
- c{motion} change (delete then insert): cw change word, c$ change to end of line.
- cc change whole line.
- y{motion} yank by motion: yw yank word, y$ yank to end of line, yy yank line.
- p paste after cursor/line. P paste before.
- ~ toggle case (Normal). gu{motion} lowercase, gU{motion} uppercase.
Undo / redo / repeat:
- u undo. CTRL+r redo. . repeats your last change.
5) Visual Mode (Select, then Act)
---------------------------------
- v character, V line, CTRL+v block.
- After selecting: d delete, y yank, c change, > indent, < outdent, = auto-indent, ~ toggle case.
- Block trick: CTRL+v to select a column, I to insert at start or A to append at end for every selected line, then ESC.
6) Search and Replace
---------------------
- /error finds “error”. n to move forward, N backward.
- :%s/old/new/g replace all in file. Add c to confirm each: :%s/old/new/gc
- In a visual selection use :s/old/new/g to change only that region.
- Use to make patterns “very magic” (fewer backslashes): :%s/ foo(\d+)/bar/g
7) Files, Buffers, Windows, Tabs
--------------------------------
Files and buffers:
- :e filename open file. :w save. :saveas newname write to a new file.
- :ls list buffers. :bnext (or :bn), :bprev (or :bp), :b 5 to jump to buffer 5.
Windows (splits):
- :sp split horizontally, :vsp split vertically.
- CTRL+w then: s split, v vertical split, w switch window, h/j/k/l move focus, c close, = equalize sizes.
Tabs:
- :tabnew, :tabnext (:tabn), :tabprev (:tabp), :tabclose. Tabs contain windows; windows show buffers.
8) Indentation, Formatting, and Code Tools
------------------------------------------
- == reindent current line. =i{ reindent inside braces. =G reindent to end.
- >> indent line, << outdent line. In Visual mode use > and <.
- gf open file under cursor (useful on import/include lines). CTRL+o jumps back.
- ]m and [m move between methods/functions in some languages (requires plugins or ctags in many setups).
9) Registers and the System Clipboard
-------------------------------------
- Named registers "a to "z store yanks/deletes: "ayy yanks a line into a, "ap pastes it.
- The unnamed register holds the last yank/delete. "" explicitly uses it.
- System clipboard: on most builds use "+ for the clipboard and "* for the selection.
Examples: "+y to copy to OS clipboard, "+p to paste from it.
- Black hole register: "_d deletes without yanking (keeps your last copy intact).
10) Macros — Automate Yourself
------------------------------
- q a starts recording into register a. Do stuff. q stops.
- @ a plays macro a. @@ repeats the last macro.
- 10@a runs it ten times. Combine with searches and motions for power.
11) The Undo Tree and Time Travel
---------------------------------
- g; jumps to older change, g, to newer change.
- :earlier 5m go back five minutes; :later 30s go forward thirty seconds.
- :undolist shows branches if persistent undo is configured.
12) Working Faster — Practical Routines
---------------------------------------
- Quick fix typos: /mispell ENTER, cw correct ENTER, n . repeats change on next match.
- Align CSV columns: CTRL+v block select, then :!column -t (requires column tool) or use plugin.
- Wrap long paragraphs: gqap rewraps a paragraph, gq} rewraps to next blank line.
- Jump list: CTRL+o go back, CTRL+i go forward (like browser history).
13) Settings You’ll Actually Use (in your ~/.vimrc or init.vim)
----------------------------------------------------------------
Copy these lines to a config file to make life easier:
set number
set relativenumber
set ignorecase smartcase
set incsearch hlsearch
set tabstop=2 shiftwidth=2 expandtab
set clipboard=unnamedplus
set mouse=a
set splitbelow splitright
set undofile
set updatetime=300
syntax on
filetype plugin indent on
What they do:
- number / relativenumber show line numbers (relative makes motions like 5j obvious).
- ignorecase + smartcase makes searches case-insensitive unless you use capitals.
- incsearch shows matches as you type; hlsearch highlights them.
- tabstop / shiftwidth / expandtab control indentation.
- clipboard=unnamedplus uses your OS clipboard by default (if supported).
- splitbelow / splitright make new splits appear where you expect.
- undofile enables persistent undo across sessions.
- updatetime speeds up diagnostics and swap/undo writes.
14) Help That Actually Helps
----------------------------
- :help user-manual — the book. Start here.
- :help quickref — dense cheat sheet.
- :help motion.txt, :help change.txt, :help visual.txt — deep dives.
- K on a keyword often opens language help (with tools configured).
15) Habit Drills (5–10 minutes each)
------------------------------------
Drill A — Motions:
- Open any text. Navigate using only hjkl, w, b, e, 0, ^, $, gg, G for three minutes.
- Add counts: 3w, 2b, 5j, 10k. No arrow keys.
Drill B — Edit Without the Mouse:
- Replace a word with cw.
- Duplicate a line with yy p.
- Move a line up/down by dd then p or P.
- Undo and redo: u, CTRL+r.
- Repeat last change with . five times in a row.
Drill C — Search and Replace:
- /the then n to move through matches.
- :%s/the/THE/g then :u to undo, :%s/the/THE/gc to confirm each.
- Practice using in a pattern: :%s/ ([A-Z]{2,})/__/g
Drill D — Visual Block Magic:
- Use CTRL+v to select the first 10 characters of several lines.
- Press I, type //, ESC to insert // at the start of those lines.
- Use A to append text to the ends of a column selection.
16) Common Pitfalls and Fixes
-----------------------------
- “Why doesn’t typing work?” You’re not in Insert. Press i (or a, o) to insert; ESC to return.
- “Can’t paste from the system?” Your Vim might lack clipboard support. Run :echo has('clipboard'). If 0, install a clipboard-enabled build (e.g., macOS: brew install vim).
- “Weird keys or colors?” Try :set nocompatible (default in modern Vim) and ensure a proper TERM (e.g., xterm-256color).
17) Minimal Plugin Advice (Start Here)
--------------------------------------
Vanilla Vim is fast. Add only what fixes a pain:
- File navigation: netrw is built-in (:Ex). If you want more, consider one plugin later.
- Fuzzy find: use grep/rg via :vimgrep or :grep before adding a fuzzy finder.
- Linting/LSP: if you write code, consider Neovim with built-in LSP instead of heavy Vim plugins.
18) Micro-Cheat Sheet
---------------------
Modes: ESC (Normal) | i/a/o/O (Insert) | v/V/CTRL+v (Visual) | : / ? (Cmd/Search)
Move: h j k l | w b e | 0 ^ $ | gg G | % | n N | * #
Edit: x r | d{motion} c{motion} y{motion} | dd cc yy | p P | . | u CTRL+r
Search/Replace: / ? n N | :%s/old/new/gc
Windows: :sp :vsp | CTRL+w w/h/j/k/l | :q (close)
Files: :e :w :wq :q! :x | :ls, :bn, :bp
Clipboard: "+y "+p | "_d
Macros: qa … q | @a @@ | 10@a
19) Your First Hour Plan
------------------------
- Learn Normal vs Insert. Practice switching.
- Memorize hjkl, w, b, e, 0, ^, $, gg, G, %.
- Practice dw, dd, cw, yy, p, u, CTRL+r, .
- Search with / and replace with :%s/…/…/g
- Split windows with :sp and :vsp; move with CTRL+w w.
- Save and quit without fear: :w, :q, :q!, :wq.
That’s it. Keep this open in a split and practice. Vim rewards habits—tiny motions, repeated often, become superpowers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment