Notes From "Mastering Vim Quickly"
Contents:
Verbs
s
- delete char under cursor and enter Insert Mode.r
- replace char under cursor.c/hello
- change until next occurrence of 'hello'
Registers
"ayy
yank the entire row into registera
."Ay
yank to register A and append the new text to the existing contents of the register.:registers
- preview the contents of your registers.
Insert Mode
<C-W>
- delete back one word.<C-U>
- delete back to the start of the line or start of current insert.cgn
- if you are searching for a word (either by using/
or*
or#
) and you want to change each instance of the search result, use<verb>gn
to change or delete and then go to the next result. This will let you use the.dot
operator to repeat both the steps (moving and changing).<C-R> 0
- paste.<C-R><C-P>0
if there are new-line chars causing trouble.
Normal Mode
<C-A>
or<C-X
increase or decrease a number.
Command Mode
set ft?
- find out which filetype is loaded.
<C-V>
Visual Block Mode - Select a column of numbers you want to increment, then
g<C-A>
will turn them into an incremented list.
Ranges
:put =range(1,10)
- insert a list of ascending numbers.:for i in range(1,100) | put ='192.168.1.'.i | endfor
- use a loop to generate a long list.
Searching
g#
org*
for partial matches, like#
or*
for exact matches.- Search for the word under the cursor, or similar:
- Press
/
. <C-R>
<C-W>
- this will copy and paste the word under the cursor into the search box. Edit it as necessary.- After you've done your search,
<C-o>
to jump back to where your cursor was before.
- Press
- Find and replace whole words only:
:s/<old_word\>/new/g
- Find and replace either old-word1 or old_word2:
:s/(old_word1\|old_word2\)/new/g
g <C-G>
- show some stats about current bugger - word count, line count, char count.
Undo
g-
andg+
- undo branches.- Under changes within a period of time:
:earlier 2d
- undo changes in the last 2 days:later 5m
- redo all changes in the last 5 minutes:earlier 3f
- undo all changes in the last three buffer writess
seconds,m
minutes,h
hours,d
days,f
saves
:g/my_string/normal @a
- Use the global command to execute macroa
on all lines of the current buffer containing string 'my_string':g/good/s/bad/ugly/g
- For every line containing “good” substitute all “bad” with “ugly”
Splits
<C-W> r
- rotate the splits from left to right but only if they are split vertically.<C-W> R
- rotate the splits from right to left.<C-W> H
- move the current split to the far left and make it full height.<C-W> J
- move the current split to the bottom of the screen and use the full width.:only
- close all splits except the current split.
Macros
:g/pattern/norm @o
- do the macro stored in bufferO
on all lines that match the pattern "pattern"
Other
<C-O>
in Insert Mode will jump you into Command Mode for one command only and then put you back into Insert Mode automatically.- The
.dot
command only repeats commands that changes the buffer content. It wont repeat navigation commands.