General
-
actions
-
modifiers
-
text objects
-
{operator}{motion}
-
{operator}{count}{motion}
-
{count}{operator}{motion}
Inserting text
Unlike most editors, in Vim’s normal mode, your cursor is always directly on top of a character. That’s why the idea of inserting text before or after the cursor becomes relevant in vim.
- insert before cursor: i, I
- insert after cursor: a, A
Deleting text
d
= deletedw
doesn’t mean “delete a word”; it means “put me in delete-what-ever-i-touch mode and then move to the next word”; or, “turn my cursor into a force that deletes anything it travels through and then travel to the beginning of the next word”; so, you’re really just moving the cursor and having a “delete” effect on whatever you pass on your journey; it’s kind of like being in star mode in mario :)
Changing text
c
= change (i.e. delete and enter insert mode)r
= replace character under cursorO
,o
= add a blank line above or below cursor (requires vim-unimpaired)
Selecting text
v
= visual modeV
= visual line modeCtrl-v
= visual block mode
Links:
- Reselect last visual selection • VimTricks 📖
- The most amazing built-in feature nobody ever mentions: smart select! : r/neovim • Reddit 📖
Text Objects
- Objects:
w
= words
= sentence (delimited by.
,?
, or!
)p
= paragraph""
,''
, or``
= quoted string()
= parenthesized block{}
= braced block[]
= bracketed blockt
= tag block (HTML)
- Modifiers:
i
= inner (not including surrounding whitespace)a
= all (including surrounding whitespace)
- Examples:
diw
= delete inner worddap
= delete all paragraphci)
= change inner parenthesized blockcas
= change all sentencevi{
= visually select inner braced blockvat
= visually select all tag block
- References:
:help text-objects
- Vim Text Objects: The Definitive Guide • Carbon Five 📖
The key is to try to always edit by text objects. Editing by motions e.g., by part of a line, to the next occurrence of a character, is tedious, clumsy, and slow.
- kana/vim-textobj-user Wiki • Vim plugin for defining custom text objects (or installing plugins that do that) 🛠️
While Text objects exist in Vim, but not in Vi mode in the shell (which is based on Vi, not Vim).
Find and replace in file
:%s/before/after/gc
- Reference vim help command for before/after syntax and flags
Find and replace in project
Replace a symbol
- When renaming a symbol, don’t rely on test pattern matching
- Leverage LSP to intelligently rename all occurrences that symbol instead
- TODO: define symbol
Replace text using Telescope -> Quickfix list
- Find occurrences you’re looking for using Telescope
- Send results to Quickfix list
:cdo s/before/after/gc | update
Links:
- Interactive replace across files • VimTricks 📖
Spectre
- Omit this option?
- Any benefits?
Inbox
- Vim Speed and Excellence • ThePrimeagen 📺
- Vim Registers: What, How, and Macros • ThePrimeagen 📺
- Learning Vim: The Path to Mastery • Matt Boehm 📺