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)- Power of g • Vim Tips Wiki 📖
:[range]g/pattern/cmd
= act on the specifiedrange
(default whole file) by executingcmd
on each line matchingpattern
- Enable in VSCodeVim via the Neovim integration
- How to survive without multiple cursors in vim • Great ideas for keymaps that make it easier to apply the same change multiple times • Heiker Curiel 📖
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
- Add that great VimTips article on this
- In selection:
- Lightning Fast Neovim Startup Time! // Migrating to Lazy • Includes an example of a substitution over the body of a function and discusses the alternative of using a macro that steps through search results • DevOps Toolbox 📺
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 📺
-
Vim: how to stay in visual mode after executing a command - Stack Overflow
-
using the normal command in vim - Richard Natt och Dag
-
Vim Tips I Wish I Knew Earlier - Sebastian Daschner
-
Vim stuff you NEED to know. Editing macros • You can edit a macro if it doesn’t quite work • typecraft 📺
-
Reindenting — Vim Commands you NEED TO KNOW • Automatically indent with
=
, which also works with counts (how many lines to indent) and motions • typecraft 📺 -
Vim Commands you NEED TO KNOW • Good one minute summary of how substitution works • typecraft 📺