Better, Stronger, Faster.Learn vim and it will be your last text editor. There isn’t any better text editor I know. Hard to learn, but incredible to use.
I suggest you to learn it in 4 steps:
- Survive
- Feel comfortable
- Feel Better, Stronger, Faster
- Use vim superpowers
But before we start, just a warning. Learning vim will be painful at first. It will take time. It will be a lot like playing a music instrument. Don’t expect to be more efficient with vim than with another editor in less than 3 days. In fact it will certainly take 2 weeks instead of 3 days.
1st Level – Survive
- Install vim
- Launch vim
- DO NOTHING! Read.
i
.You should feel a bit better. You can type letters like in a standard notepad. To get back in Normal mode just tap the
ESC
key.You know how to switch between Insert and Normal mode. And now, the list of command you can use in Normal mode to survive:
Recommended:
i
: Insert mode. TypeESC
to return to Normal mode.x
: Delete the char under the cursor:wq
: Save and Quit (:w
save,:q
quit)dd
: Delete (and copy) current linep
: Paste
Only 5 commands. This is very few to start. Once these command start to become natural (may be after a complete day), you should go on level 2.
hjkl
(recommended but not mandatory): basic cursor move (←↓↑→). Hint:j
look like a down arrow.:help
: Show help about, you can start using :help
without anything else.
But before, just a little remark on Normal mode. In standard editors, to copy you have to use theCtrl
key (Ctrl-c
generally). In fact, when you pressCtrl
, it is a bit like if all your key change meaning. With vim in Normal mode, it is a bit like if yourCtrl
key is always pushed down.
A last word about notation: instead of writingCtrl-λ
, I’ll write.
2nd Level – Feel comfortable
You know the commands required for survival. It’s time to learn a few more commands. I suggest:
Take the time to integrate all of these command. Once done, you should be able to do every thing you are able to do on other editors. But until now, it is a bit awkward. But follow me to the next level and you’ll see why.
- Insert mode variations:
a
→ insert after the cursoro
→ insert a new line after the current oneO
→ insert a new line before the current onecw
→ replace from the cursor to the end the word- Basic moves
0
→ go to first column^
→ go to first non-blank character of the line$
→ go to the end of lineg_
→ go to the last non-blank character of line/pattern
→ search forpattern
- Copy/Paste
P
→ paste before, rememberp
is paste after current position.yy
→ copy current line, easier but equivalent toddP
- Undo/Redo
u
→ undo→ redo
- Load/Save/Quit/Change File (Buffer)
:e
→ open:w
→ save:saveas
→ save to
ZZ
or:wq
→ save and quit:q!
→ quit without saving, also:qa!
to even if there are some modified hidden buffers.:bn
(resp.:bp
) → show next (resp. previous) file (buffer)
3rd Level – Better. Stronger. Faster.
Congratulation reaching this far! We can start the interesting stuff. At level 3, we’ll only talk about command which are compatible with the old vi.
Better
Lets look at how vim could help you to repeat yourself:
Some examples, open a file and type:
.
→ (dot) will repeat the last command,- N
→ will do the command N times.
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).Stronger
Knowing how to move efficiently with vim is very important. Don’t skip this section.
Now let’s talk about very efficient moves:
- N
G
→ Go to line Ngg
→ shortcut for1G
, go to the start of the fileG
→ Go to last line- Word moves:
By default, word are composed of letter and the underscore character. If you want to use word in the meaning of group of letter separated by spaces, just use uppercases:
w
→ go to the start of the following word,e
→ go to the end of this word.
W
→ go to the start of the following “extended” word,E
→ go to the end of this “extended” word.
Believe me, the last three commands are gold.
%
: Go to corresponding(
,{
,[
.*
(resp.#
) : go to next (resp. previous) occurrence of the word under the cursor
Faster
Remember about the importance of vi moves? Here is the reason. Most commands can be used using the following general format:
For example :0y$
means
We also can do things like
0
→ go to the beginning of this liney
→ yank from here$
→ up to the end of this lineye
, yank from here to the end of the word. But alsoy2/foo
yank up to the second occurrence of “foo”.
But what was true fory
(yank), is also true ford
(delete),v
(visual select),gU
(uppercase),gu
(lowercase), etc…
4th Level – Vim Superpowers
With all preceding commands you should be comfortable to use 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
^
$
f
F
t
T
,
;
Some Useful Tips
0
→ go to column 0^
→ go to first character on the line$
→ go to the last character on the linefa
→ go to next occurrence of the lettera
on the line.,
(resp.;
) will seek for the next (resp. previous) occurrence.t,
→ go just before the character,
.3fa
→ search the 3rd occurrence ofa
on this line.F
andT
→ likef
andt
but backward.
dt"
→ remove everything until the"
.vi"
→ select everything inside two"
.Select rectangular blocks:
Rectangular blocks are very useful to comment many lines of code. Typically:.
0
I-- [ESC]
^
→ go to start of the line→ Start block selection
→ move down (could also be
jjj
or%
, etc…)I-- [ESC]
→ write--
to comment each line
Not on windows you might have to useinstead of
if your clipboard is not empty.
Completion:
In Insert mode, just type the start of a word, then typeand
.
, magic…
Macros :
qa
do somethingq
,@a
,@@
qa
record your actions in the registera
. Then@a
will replay the macro saved into the registera
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
→q
qa
start recording.Yp
duplicate this line.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.
Visual selection:
We saw an example withv
,V
,
. There is also
v
andV
. Once the selection made, you can:
J
→ join all lines together.<
(resp.>
) → indent to the left (resp. to the right).=
→ auto indent
Add something at the end of all visually selected lines:
- go to desired line (
jjj
oror
/pattern
or%
etc…)$
go to the end of lineA
, write text,ESC
.
Splits:
Here are the main commands, but you should look at:split
andvsplit
.:help split
.
:split
→ create a split (:vsplit
create a vertical split)
: where dir is any of
hjkl
or ←↓↑→ to change split.(resp.
_ ) : maximise size of split (resp. vertical split)
| (resp.
+ ) : Grow (resp. shrink) split
-
Conclusion
That was 90% of commands I use every day. I suggest you to learn no more than one or two new command per day. After two to three weeks you’ll start to feel the power of vim in your hands.
Then, you will learn about!
, folds, registers, the plugins and many other features. Learn vim like you’d learn piano and all should be fine.
REFERENCES
http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/