Learn Vim Progressively (3)
4th Level – Vim Superpowers
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.
Move on current line: 0
^
$
g_
f
F
t
T
,
;
0
→ go to column 0
^
→ go to first character on the line
$
→ go to the last column
g_
→ go to the last character on the line
fa
→ 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 "
.
Zone selection <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"))
Select rectangular blocks: <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 bejjj
or%
, etc…)I-- [ESC]
→ write--
to comment each line
Note: in Windows you might have to use <C-q>
instead of <C-v>
if your clipboard is not empty.
Completion: <C-n>
and <C-p>
.
In Insert mode, just type the start of a word, then type <C-p>
, magic…
Macros : 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 2Now do
100@@
will create a list of increasing numbers until 103.
Visual selection: 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 indent
Add something at the end of all visually selected lines:
<C-v>
go to desired line (
jjj
or<C-d>
or/pattern
or%
etc…)$
go to the end of the lineA
, write text,ESC
.
Splits: :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
Conclusion
That was 90% of the commands I use every day. I suggest that you learn no more than one or two new commands per day. After two to three weeks you’ll start to feel the power of vim in your hands.
Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read this page carefully: :help usr_02.txt
.
Then, you will learn about !
, folds, registers, plugins and many other features. Learn vim like you’d learn piano and all should be fine.
You must Sign up as a member of Effecthub to view the content.
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_ACCEPT_LANGUAGE
Filename: helpers/time_helper.php
Line Number: 22
1539 views 2 comments
You must Sign up as a member of Effecthub to join the conversation.