Visual mode: lets you visually confirm what lines of text you intend to manipulate.
ctrl+v to enter visual mode. You can use any navigation command availabe in command mode within visual mode, eg. h, j, k, l for navigation, {, } for skipping code blocks.
Misc. β other helpful keys
Macros
Registers
Scrolling
Motions
Motions are movement commands for moving the cursor to different positions.
You can use operators with motions: {operator}{count}{motion}. For example, d3j.
You can repeat motions: {count}{motion}. For example, 5k.
Text Objects
You can perform operations on common text objects that occur in code and prose. Format: {operator}{a|i}{text-object-id}, where:
a β apply the operator βaroundβ the text object.
i β apply the operator βinsideβ the text object.
text-object-id includes:
w
"
'
(
Yanking
y is an operator just like d, so you can use it in {operator}{count}{motion}. I prefer to yank visual mode selections instead since it gives me better confidence of precisely what I yanked.
Registers: Vim gives you a set of registers that you can yank text into. The operators d and c are actually cutting things, not deleting them.
You save to a register by doing "{register}{y|d|c}{motion} or if you use visual mode, then itβs "{register}{y|d|c}.
" β this is where text gets yanked into by default (when you donβt specify a register).
a-z β general-purpose registers you can yank text into explicitly.
1-9 β stores the last 9 things you cut.
+ or * β system clipboard. I prefer to override Vim to still use the classic ctrl+c and ctrl+v however.
You can also paste from a register while in insert mode: ctrl+rΒ <register>. For example, ctrl+rΒ " inserts whatever is in register β
Search
In normal mode:
Forward search:/<pattern>
Backward search:?<pattern>
Press n to see the next occurrence, or N for previous occurrrence.
Command-Line Mode
There are a lot ex commands that you can invoke with : in normal mode. Note: the VSCodeVim extension has a limited set of ex commands.
Some ex commands can be applied over a range of lines. Format: [range]commandΒ [options].
Ranged ex commands are sometimes faster than if you were to navigate the cursor to the right positions before executing some command.
Marks
Often you are at, say, line 123 and want to check line 321, then go back to line 123. A good way to do this is to use marks.
m <letter> β assigns a letter to the current line number.
ma marks the current line as βmark aβ.
'a jumps to βmark aβ.
:marks lists all marks.
Combos
A collection of frequently used combos.
Deletion/Edit combos:
In general, d can be combined with any navigation command.
vi( β highlight all code between the current pair of parentheses. Works across multiple lines
ci( β delete everything between current pair of parentheses and go into insert mode
Works for all types of βcontainerβ characters (eg. ci{, ci[, ci<, ci")
c% β the % motion finds the next open parenthesis and jumps to its matching parenthesis
Everything between ^ and B are deleted
% also works for [, {, <, etc.
Word operations:
caw β delete the current word and go into insert mode. Remember caw == "change a word"
ciw β I tend to use this one more
ciW β deletes the entire word youβre on but a βwordβ is delimited by whitespace characters rather than by punctuation characters like by default
ea β jump to the end of the current word and go into insert mode
Next occurrence:
dt<char> β deletes everything up to a particular character in the forward direction
dT<char> β in the backward direction
Copy and paste:
yaw β yanks the current word. Remember yaw == "yank a word"
Indenting:
Highlight the block of code in visual mode
> to indent forward or < to indent backwards
gv> to continue indenting the same block forward. Use . to repeat this and u to undo
gv< for indenting backwards
Visual mode:
gv β highlights the last highlighted block of code
o β toggles the cursor between the top and bottom of the selected code
<num>G β highlights up to the specified line number
Tab changing:
gt β next tab
gT β previous tab
<num>gt β jump to numbered tab
Themes
To add a new theme:
mkdir ~/.vim
git clone $THEME_GIT_REPO ~/.vim
Then in .vimrc add the line: colorscheme $THEME_NAME.
Plugins
Note: the VSCodeVim extension ships with some of the most useful Vim plugins built-in. Eg. Vim Surround.
Vim Surround
Provides operations for affecting enclosing characters such as (, [, <, {, <p>, etc.
Delete surroundings: ds{char}
Change surroundings: cs{old_char}{new_char}
Surround currently highlighted: S{char}
ds<char> # Removes the enclosing character specified
dsb # Deletes surrounding ()
ds' # Deletes surround ''
dst # Deletes the surrounding HTML tags
cs<old><new> # Swaps the old enclosing character to a new enclosing character
Eg. cs'` # Changes 'hello' to `hello`
cs[{ # Changes [hello] to {hello}
cst" # Changes <tag>hello</tag> to "hello"
S<char> # Surround the currently highlighted text with the given character.
Eg. Sb # Surround with (). Same as S(
SB # Surround with {}. Same as S}
S" # Surround with ""
Stli> # Surround with <li>