i
→ Insert mode. Type ESC
to return to Normal mode.x
→ Delete the char under the cursor:wq
→ Save and Quit (:w
save, :q
quit)dd
→ Delete (and copy) the current linep
→ Pastehjkl
(highly recommended but not mandatory) → basic cursor move (←↓↑→):help <command>
→ Show help about <command>
. You can use :help
without a <command>
to get general help.Insert mode variations:
Basic moves
Copy/Paste
Undo/Redo
Load/Save/Quit/Change File (Buffer)
Let’s look at how vim could help you to repeat yourself:
.
→ (dot) will repeat the last command,Some examples, open a file and type:
2dd
→ will delete 2 lines3p
→ will paste the text 3 times100idesu [ESC]
→ will write “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu”.
→ Just after the last command will write again the 100 “desu”.3.
→ Will write 3 “desu” (and not 300, how clever).
Knowing how to move efficiently with vim is very important. Don’t skip this section.
G
→ Go to line Ngg
→ shortcut for 1G
- go to the start of the fileG
→ Go to last lineNow let’s talk about very efficient moves:
%
: Go to the corresponding(
,{
,[
.*
(resp.#
) : go to next (resp. previous) occurrence of the word under the cursor
Believe me, the last three commands are gold.
Remember about the importance of vi moves? Here is the reason. Most commands can be used using the following general format:
<start position><command><end position>
For example : 0y$
means
0
→ go to the beginning of this liney
→ yank from here$
→ up to the end of this lineWe also can do things like ye
, yank from here to the end of the word. But also y2/foo
yank up to the second occurrence of “foo”.
But what was true for y
(yank), is also true for d
(delete), v
(visual select), gU
(uppercase), gu
(lowercase), etc…
With all preceding commands you should be comfortable using vim. But now, here are the killer features. Some of these features were the reason I started to use vim.
0
^
$
g_
f
F
t
T
,
;
0
→ go to column 0^
→ go to first character on the line$
→ go to the last columng_
→ go to the last character on the linefa
→ go to next occurrence of the lettera
on the line.,
(resp.;
) will find the next (resp. previous) occurrence.t,
→ go to just before the character,
.3fa
→ find the 3rd occurrence ofa
on this line.F
andT
→ likef
andt
but backward.![]()
A useful tip is: dt"
→ remove everything until the "
.
<action>a<object>
or <action>i<object>
These command can only be used after an operator in visual mode. But they are very powerful. Their main pattern is:
<action>a<object>
and <action>i<object>
Where action can be any action, for example, d
(delete), y
(yank), v
(select in visual mode). The object can be: w
a word, W
a WORD (extended word), s
a sentence, p
a paragraph. But also, natural character such as "
, '
, )
, }
, ]
.
Suppose the cursor is on the first o
of (map (+) ("foo"))
.
vi"
→ will selectfoo
.va"
→ will select"foo"
.vi)
→ will select"foo"
.va)
→ will select("foo")
.v2i)
→ will selectmap (+) ("foo")
v2a)
→ will select(map (+) ("foo"))
<C-v>
.Rectangular blocks are very useful for commenting many lines of code. Typically: 0<C-v><C-d>I-- [ESC]
^
→ go to the first non-blank character of the line<C-v>
→ Start block selection<C-d>
→ move down (could also be jjj
or %
, etc…)I-- [ESC]
→ write --
to comment each lineNote: in Windows you might have to use <C-q>
instead of <C-v>
if your clipboard is not empty.
<C-n>
and <C-p>
.In Insert mode, just type the start of a word, then type <C-p>
, magic…
qa
do something q
, @a
, @@
qa
record your actions in the register a
. Then @a
will replay the macro saved into the register a
as if you typed it. @@
is a shortcut to replay the last executed macro.
Example
On a line containing only the number 1, type this:
qaYp<C-a>q
→qa
start recording.Yp
duplicate this line.<C-a>
increment the number.q
stop recording.@a
→ write 2 under the 1@@
→ write 3 under the 2- Now do
100@@
will create a list of increasing numbers until 103.
v
,V
,<C-v>
We saw an example with <C-v>
. There is also v
and V
. Once the selection has been made, you can:
J
→ join all the lines together.<
(resp. >
) → indent to the left (resp. to the right).=
→ auto indentAdd something at the end of all visually selected lines:
<C-v>
jjj
or <C-d>
or /pattern
or %
etc…)$
go to the end of the lineA
, write text, ESC
.:split
and vsplit
.These are the most important commands, but you should look at :help split
.
:split
→ create a split (:vsplit
create a vertical split)<C-w><dir>
: where dir is any ofhjkl
or ←↓↑→ to change the split.<C-w>_
(resp.<C-w>|
) : maximise the size of the split (resp. vertical split)<C-w>+
(resp.<C-w>-
) : Grow (resp. shrink) split