Vim rewards a simple mental model: motions move, operators act, text
objects select structure, and everything composes. d deletes, w moves
a word, i" selects inside quotes, so di" deletes inside quotes and
ci" deletes and starts typing. This skill covers Vim 9.1 fluency:
the composable core, buffers and windows, macros, registers, search and
replace, visual block editing, and a maintainable vimrc. Most of it
applies to Neovim 0.10 as well.
Quick Workflow
- Learn the grammar first: operator + motion, or operator + text object; everything else is vocabulary.
- Move by meaning (paragraphs, brackets, search), not by character.
- Edit with text objects (
iw,i",ip,at) instead of visual selection. - Repeat with
., record withqwhen.is not enough. - Navigate files with buffers, splits, and the quickfix list, not one file at a time.
- Search precisely, replace with confirmation on risky patterns.
- Keep a small, understood vimrc; add plugins only for real pain.
- Practice one new motion per day until it is muscle memory.
1. The composable core
Operators: d delete, c change, y yank (copy), > indent, < dedent,
gu lowercase, gU uppercase, = auto-indent
Motions: w next word, b back word, e end of word,
0 start of line, ^ first non-blank, $ end of line,
gg first line, G last line, } next paragraph, { prev,
f{char} find char forward, t{char} till char,
% matching bracket, * search word under cursor
Text objects: iw inner word, aw a word (with space),
i" inner quotes, a" around quotes,
i( i[ i{ inner brackets, a( a[ a{ around,
ip inner paragraph, ap around paragraph,
it inner tag, at around tag (HTML/XML)
Composition examples (type these, do not memorize lists):
di" delete inside quotes (keep the quotes)
ci( change inside parens (delete + insert mode)
yap yank a paragraph
gUiw uppercase inner word
>ip indent paragraph
dt, delete till comma
ciw change inner word (the rename-variable workhorse)
Counts multiply: d3w deletes three words, y5j yanks five lines down.
2. Moving like you mean it
H M L top / middle / bottom of screen
Ctrl-d/u half-page down / up
Ctrl-f/b full page down / up
zz / zt / zb center / top / bottom the cursor line
n / N next / previous search match
gd go to definition (ctags/LSP)
/pattern search forward, ?pattern backward
# Jump to a line from the shell
vim +42 app.py
vim +/TODO app.py
Inside insert mode, Ctrl-o runs one normal command then returns:
Ctrl-o A jump to end of line, stay in insert mode
Ctrl-o zz center the view without leaving insert mode
3. Buffers, windows, tabs
:e file edit (opens in current window)
:ls list buffers
:b2 / :bnext / :bprev switch buffers
:bd delete (close) buffer
:sp / :vsp horizontal / vertical split
Ctrl-w hjkl move between splits
Ctrl-w = equalize split sizes
Ctrl-w o keep only the current split
:tabnew file new tab, gt / gT cycle tabs
Netrw (built-in file browser) covers 90 percent of file navigation:
:Ex open netrw in current window
- go up a directory (inside netrw)
% create a file, d create a directory
For projects, combine with the shell instead of plugins:
# Open every file matching a pattern, each in an argument slot
vim $(grep -rl "MAX_RETRIES" src/)
# Then :argdo %s/old/new/g | update (edit all, save all)
4. Macros: record once, replay everywhere
qa start recording into register a
...edits...
q stop recording
@a replay register a
@@ replay last macro again
10@a replay 10 times
Practical macro: turn a list into quoted CSV values.
# Start: apple
# banana
# cherry
# Record: qa I"<Esc> A",<Esc> j q
# Then: 2@a
# Result: "apple",
# "banana",
# "cherry",
Recursive macro for "until the end of file": record q at the end of
the macro itself (qa ... j q then @a stops at the last line).
5. Registers, marks, and jumps
"ayy yank line into register a
"ap paste from register a
"+y / "+p system clipboard (needs +clipboard build)
"0p paste last yank (not the last delete)
:reg show all registers
ma set mark a at cursor
'a jump to mark a (line), `a jump exact position
'' jump back to previous position
Ctrl-o/i jump older / newer through the jump list
# Check clipboard support before relying on "+
vim --version | grep -o '+clipboard'
The black hole register "_ deletes without clobbering the yank:
"_dd delete line, keep previous yank intact
6. Search and replace without fear
:%s/old/new/g whole file
:%s/old/new/gc confirm each replacement
:'<,'>s/old/new/g visual selection only
:s/old/new/g current line
Magic modes: \v (very magic) makes patterns behave like ERE:
:%s/\v(foo|bar)/X/g no backslash soup
:%s/\v(\d+)-(\d+)/\2-\1/g swap number groups
# Replace across many files from the shell (safer preview first)
grep -rl 'old_name' src/ | xargs sed -i 's/old_name/new_name/g'
# Or inside vim with quickfix:
:vimgrep /old_name/ src/**/*.py
:copen " browse matches
:cfdo %s/old_name/new_name/g | update
Case handling: set ignorecase + set smartcase (lowercase query is
case-insensitive, any uppercase makes it sensitive).
7. Visual block mode: column editing
Ctrl-v visual block select
I / A insert before / append after every selected line
r{char} replace the whole block with a char
Turn a list into an assignment block:
# Start: # Select the column before each name with Ctrl-v,
# apple # press I, type "name = '", Esc,
# banana # then A, type "'," Esc
# cherry
# Result:
# name = 'apple',
# name = 'banana',
# name = 'cherry',
8. A sane vimrc
cat > ~/.vimrc <<'EOF'
" Basics
set nocompatible
syntax on
filetype plugin indent on
set number relativenumber
set cursorline
set encoding=utf-8
" Search
set ignorecase smartcase incsearch hlsearch
" Indentation: spaces, 4 wide, keep it consistent
set expandtab tabstop=4 shiftwidth=4 softtabstop=4
set autoindent smartindent
" Behavior
set hidden " switch buffers without saving
set mouse=a " mouse works in terminal vim
set clipboard=unnamedplus
set undofile " persistent undo across sessions
set undodir=~/.vim/undodir
set wildmenu wildmode=longest:full
set backspace=indent,eol,start
" Leader key and the mappings you will actually use
let mapleader=" "
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>f :find<CR>
" Clear search highlight
nnoremap <silent> <leader><space> :nohlsearch<CR>
" Move lines up/down
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
" Find files recursively from cwd
set path+=**
EOF
mkdir -p ~/.vim/undodir
Plugins: start with none. Add vim-plug plus one fuzzy finder and one
LSP client only when :find and ctags genuinely slow you down. Every
plugin is a dependency you will debug at 2 AM.
Edge cases
vimopens asvi(compatible mode, no arrow keys): your vimrc is not loaded; check:echo $MYVIMRCand:set compatible?.- Paste mangles indentation:
:set pastebefore pasting,:set nopasteafter (or use bracketed paste in a modern terminal). - Macros break on a slightly different line: add
set nowrapscanawareness, or make the macro end with a search that fails cleanly. dthenppastes the delete, not the yank: use"0pfor the last yank, or delete into the black hole"_d.- Swap files after a crash:
vim -rlists recoverable sessions; delete stale.swpfiles once recovered. Ctrl-sfreezes the terminal: flow control, pressCtrl-qto resume; disable withstty -ixonin your shell rc.
Escalation
- Vim behaves bizarrely over SSH: check
$TERM(usexterm-256color), and test withvim -u NONEto rule out your config. - A plugin broke startup:
vim --startuptime /tmp/t.logshows what is slow; bisect plugins by commenting halves of the plug block. - Key mapping does nothing:
:verbose map <key>shows what claimed it; terminal emulators swallow some keys (Ctrl variants), remap around them. - Still slow on huge files:
set noswapfile,syntax off, and open withvim -u NONEfor emergency edits of multi-GB logs (or useless/sedinstead).
Detection and defender view
Vim artifacts matter in forensics and in spotting tampering: swap files, undo history, and modelines. Detect misuse and misconfig with:
# Swap files: unsaved edits, or someone editing where they should not
sudo find / -name '*.swp' -o -name '*.swo' 2>/dev/null | head
# Persistent undo history can leak file contents long after deletion
ls -la ~/.vim/undodir/ 2>/dev/null | head
# Modelines: vim executes options from file headers; malicious modelines
# are a known vector (CVE-2019-20807 and friends)
grep -rn 'vim:' /tmp /var/tmp --include='*' 2>/dev/null | head -5
Defensive rules: set nomodeline (or modelines=0) on any system
where untrusted files are edited, keep Vim patched (modeline and
:source vulnerabilities recur), and remember that editing a file as
root with a user-owned vimrc/plugins runs that code as root; use
sudo -e / sudoedit or a minimal root vimrc. In forensics, .swp
and undo files can recover attacker-edited content.
Key references
- Vim 9.1 documentation: https://www.vim.org/docs.php
- Vim user manual: https://vimhelp.org/usr_toc.txt.html
- Neovim 0.10 docs: https://neovim.io/doc/
- "Practical Vim" (Drew Neil): the composition model behind this skill.
- vimtutor: run
vimtutorfor the 30-minute interactive basics. - CVE-2019-20807: modeline code execution (why nomodeline matters).
