Notes from Practical Vim
Some of the content of this book I already knew, but it was good to understand them in a deeper level and how can I extend the usage from most of the basic commands that I have been using before.
- the
.
command let us repeat the last change. It is the most powerful and versatile command vim - the
>G
command increases the indentation from the current line until the end of the file - the
x
,dd
and>
commands are all executed from Normal mode, but we also create a change each time we dip into Insert mode. From the moment we enter inIM
, vim records every keystroke A
to insert in the end of the current line- the
;
command will repeat the last search that thef
command performed - to perform an substitution in a file
:s/target/replacement
*
searchs for the next occurrence of the word under the cursor- vim records our keystrokes until we leave
IM
- the author mentions the
dot formula
: one keystroke to move, one keystroke to execute dw
deletes a worddw
deletes a word and a space- list of vim
operators
: they can operate on a single characterdl
, a complete worddaw
, or an entire paragraphdap
:c
: changed
: deletey
: yank into registerg~
: swap casegu
: make lowercasegU
: make uppercase>
: shift right<
: shift left=
: autoindent
- grammar: an action is composed from an
operator
+motion
- when invoking the operator in duplication (
dd
per example) it acts upon the current line - you can add operators to vim: take the
commentary.vim
plugin. The operator it’sgc
, sogcc
comments the current line - editing from the
IM
:ctrl + h
: delete back one charctrl + w
: delete back one wordctrl + u
: delete back to start of the line
viw
: select inner word in visual mode- to enter in
IM
:i
it’s for before the current char anda
(append) after the current char - when we open a file, vim creates a new buffer and loads it into the current window
inline search
f{char}
searchs for the next char occurrenceF{char}
for previous char occurrencet{char}
searchs for the char before next char occurrenceT{char}
searchs for the char before previous char occurrence;
repeat the last character-search command,
reverse the last character-search command
motions for lines
j
: down one real linegj
: down one display linek
: up one real linegk
: up one display line0
: to first character of the real lineg0
: to first character of the display lineˆ
: to first nonblank character of the real linegˆ
: to first nonblank character of the display line$
: to end of the real lineg$
: to end of the display line
motions for words
w
: forward to start of next wordb
: backward to start of current/previous worde
: forward to end of current/next wordge
: backward to end of previous word- append to end of word:
ea
- append to end of previous word:
gea
- definition of a
WORD
: sequence of nonblank characters separated with whitespaces - vim’s text objects consist of two characters, the first of which is always either
i
ora
. In general, we can say that the text objects prefixed withi
select inside the delimiters, whereas those that are prefixed witha
select everything including the delimiters ci"
: change inside the double quotesd{motion}
tends to work well withaw
,as
andap
whereas thec{motion}
command works better withiw
and similar
other motions
"
: position before the last jump within current file%
: jumps between opening and closing sets of(), {}, []
<Leader>f{char}
: my leader key is,
, so,f{char}
would show the keys that I can press to go to{char}
. This also works withAceJump
IntelliJ plugin with you configure the keys properly.
surround.vim plugin
cs"'
: change surround from"
to'
ds'
: delete surround'
ysiw[
: add surround in an inner word with[
windows management
ctrl + w + s
: split current window horizontallyctrl + w + v
: split current window verticallyctrl + w + w
: cycle between open windowsctrl + w + h
: focus the window to the leftctrl + w + j
: focus the window belowctrl + w + k
: focus the window abovectrl + w + l
: focus the window rightctrl + w + c
: close the active windowctrl + w + T
: move the current window into its own tab{N}gt
: switch to tab page number {N}gt
: switch to the next tab pagegT
: switch to the previous tab page
visual mode
v
enables character-wise visual modeV
enables line-wise visual modectrl + v
enables block-wise visual modegv
reselects the last visual selection- the
.
command in visual mode acts on the same amount of text as was marked by the most recent visual selection. But doesn’t work so well sometimes vit
: select the inner contents of a tag
copy and paste
- in Vim’s terminology, we don’t deal with the clipboard but instead with registers
- the
x
command cuts the character under the cursor, placing a copy of it in the unnamed register after the cursor position yyp
: copy and paste line- “Oops! I clobbered my yank”: you had something to paste, but before pasting it you had to cut something and then your previously yanked text was replaced by the cut that you did
- vim’s delete command is equivalent to the standard cut operation
- black hole register is addressed by the
-
symbol, so_d{motion
performs a true deletion - delete, yank and put commands interact with one of Vim’s registers. We can specify which register we want to use
by prefixing the command with
"{register}
. If we don’t specify the register, then Vim will use the unnamed register - example: cut the current line into register
b
with"bdd
and paste it with"bp
- when we use
y{motion}
command, the specified text is copied not only into the unnamed register but also into the yank register, which is addressed by the0
symbol. The yank register is reliable, whereas the unnamed register is volatile. We can cut something and when using"0p
we would still have what we want - copying and paste from clipboard:
- if we capture text in an external application, then we can paste it inside Vim using
"+p
command - if we want to copy text from vim to an external application, then we have to use the register
"+
- if we capture text in an external application, then we can paste it inside Vim using
- the visual selection in the document swaps places with the text in the register
p
: pastes after the cursor positionP
: pastes before the cursor position
macros
- when recording a macro, ensure that every command is repeatable
w, b, e
is better for macros thanh, j, k, l
q{register}
(you don’t need the"
) to start recording a macro. The wordrecording
should appear in the statusq
is a good register for a macro, so you just need to pressqq
to start recording and then a finalq
to finish@{register}
command executes the macro in the given register@@
repeats the that was invoked most recently- to append commands in a macro: if you macro was
q
, then useQ
. Per exampleqQ
to start appending commands in the previous macro that you recorded usingqq
search and replace
%s/going/rolling/g
: it will search and replace in the whole file the wordgoing
forrolling
%s/going/rolling/gc
: it will ask every time that it finds agoing
if we want to replace it forrolling