Learn Vim Progressively
原文在这里
Vim the Six Billion Dollar editor
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
By the end of this journey, you’ll become a vim superstar.
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.
In a standard editor, typing on the keyboard is enough to write something and see it on the screen. Not this time. Vim is in Normal mode. Let’s get in Insert mode. Type on the letter <pre class="inline:true decode:1 " >i</pre>.
You should feel a bit better. You can type letters like in a standard notepad. To get back in Normal mode just tap the <pre class="inline:true decode:1 " >ESC</pre> 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. TypeESCto return to Normal mode. x→ Delete the char under the cursor :wq→ Save and Quit (:wsave,:qquit) dd→ Delete (and copy) current line p→ Paste
hjkl(highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint:jlook like a down arrow. :help <command>→ Show help about<command>, you can start using:helpwithout anything else.
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.
But before, just a little remark on Normal mode. In standard editors, to copy you have to use the <pre class="inline:true decode:1 " >Ctrl</pre> key (<pre class="inline:true decode:1 " >Ctrl-c</pre> generally). In fact, when you press <pre class="inline:true decode:1 " >Ctrl</pre>, it is a bit like if all your key change meaning. With vim in Normal mode, it is a bit like if your <pre class="inline:true decode:1 " >Ctrl</pre> key is always pushed down.
A last word about notations:
- instead of writing
Ctrl-λ
, I’ll write<C-λ>
. - command staring by
:
will must end by<enter>
. For example, when I write:q
it means:q<enter>
.
2nd Level – Feel comfortable
You know the commands required for survival. It’s time to learn a few more commands. I suggest:
- 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<C-r>
→ redo
- Load/Save/Quit/Change File (Buffer)
:e <path/to/file>
→ open:w
→ save:saveas <path/to/file>
→ save to<path/to/file>
:x
,ZZ
or:wq
→ save and quit (:x
only save if necessary):q!
→ quit without saving, also:qa!
to even if there are some modified hidden buffers.:bn
(resp.:bp
) → show next (resp. previous) file (buffer)
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.
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
Let’s look at how vim could help you to repeat yourself:
.
→ (dot) will repeat the last command,- N<command> → will do the command N times.
Some examples, open a file and type:
2dd→ will delete 2 lines 3p→ will paste the text 3 times 100idesu [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.
- N
G
→ Go to line N gg
→ shortcut for1G
, go to the start of the fileG
→ Go to last line- Word moves:
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 WORD,E
→ go to the end of this WORD.
Now let’s talk about very efficient moves:
%: Go to corresponding(,{,[. *(resp.#) : go to next (resp. previous) occurrence of the word under the cursor
Believe me, the last three commands are gold.
Faster
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 : <pre class="inline:true decode:1 " >0y$</pre> means
0
→ go to the beginning of this liney
→ yank from here$
→ up to the end of this line
We also can do things like <pre class="inline:true decode:1 " >ye</pre>, yank from here to the end of the word. But also <pre class="inline:true decode:1 " >y2/foo</pre> yank up to the second occurrence of “foo”.
But what was true for <pre class="inline:true decode:1 " >y</pre> (yank), is also true for <pre class="inline:true decode:1 " >d</pre> (delete), <pre class="inline:true decode:1 " >v</pre> (visual select), <pre class="inline:true decode:1 " >gU</pre> (uppercase), <pre class="inline:true decode:1 " >gu</pre> (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
,
;
0→ go to column 0 ^→ go to first character on the line $→ go to the last character on the line fa→ go to next occurrence of the letteraon the line.,(resp.;) will seek for the next (resp. previous) occurrence. t,→ go just before the character,. 3fa→ search the 3rd occurrence ofaon this line. FandT→ likefandtbut backward.
A useful tip is: <pre class="inline:true decode:1 " >dt”</pre> → remove everything until the <pre class="inline:true decode:1 " >”</pre>.
Zone selection <action>a<object>
or <action>i<object>
These command can only be used after an operator of in visual mode. But they are very powerful. Their main pattern is:
<action>a<object>
and <pre class="inline:true decode:1 " ><action>i<object></pre>
Where action can be any action, for example, <pre class="inline:true decode:1 " >d</pre> (delete), <pre class="inline:true decode:1 " >y</pre> (yank), <pre class="inline:true decode:1 " >v</pre> (select in visual mode). And object can be: <pre class="inline:true decode:1 " >w</pre> a word, <pre class="inline:true decode:1 " >W</pre> a WORD (extended word), <pre class="inline:true decode:1 " >s</pre> a sentence, <pre class="inline:true decode:1 " >p</pre> a paragraph. But also, natural character such as <pre class="inline:true decode:1 " >”</pre>, <pre class="inline:true decode:1 " >’</pre>, <pre class="inline:true decode:1 " >)</pre>, <pre class="inline:true decode:1 " >}</pre>, <pre class="inline:true decode:1 " >]</pre>.
Suppose the cursor is on the first <pre class="inline:true decode:1 " >o</pre> of <pre class="inline:true decode:1 " >(map (+) (“foo”))</pre>.
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 to comment many lines of code. Typically: <pre class="inline:true decode:1 " >0<C-v><C-d>I– [ESC]</pre>
^
→ go to start of the line<C-v>
→ Start block selection<C-d>
→ move down (could also bejjj
or%
, etc…)I-- [ESC]
→ write--
to comment each line
Not on windows you might have to use <pre class="inline:true decode:1 " ><C-q></pre> instead of <pre class="inline:true decode:1 " ><C-v></pre> if your clipboard is not empty.
Completion: <C-n>
and <C-p>
.
In Insert mode, just type the start of a word, then type <pre class="inline:true decode:1 " ><C-p></pre>, magic…
Macros : qa
do something q
, @a
, @@
qa
record your actions in the register <pre class="inline:true decode:1 " >a</pre>. Then <pre class="inline:true decode:1 " >@a</pre> will replay the macro saved into the register <pre class="inline:true decode:1 " >a</pre> as if you typed it. <pre class="inline:true decode:1 " >@@</pre> is a shortcut to replay the last executed macro.
Example On a line containing only the number 1, type this:
qaYp<C-a>q→
qastart recording. Ypduplicate this line. <C-a>increment the number. qstop 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: v
,V
,<C-v>
We saw an example with <pre class="inline:true decode:1 " ><C-v></pre>. There is also <pre class="inline:true decode:1 " >v</pre> and <pre class="inline:true decode:1 " >V</pre>. 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:
<C-v>
- go to desired line (
jjj
or<C-d>
or/pattern
or%
etc…) $
go to the end of lineA
, write text,ESC
.
Splits: :split
and vsplit
.
Here are the main commands, but you should look at <pre class="inline:true decode:1 " >:help split</pre>.
:split→ create a split (:vsplitcreate a vertical split) <C-w><dir>: where dir is any ofhjklor ←↓↑→ to change split. <C-w>_(resp.<C-w>|) : maximise size of split (resp. vertical split) <C-w>+(resp.<C-w>-) : 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.
Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and an excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read carefully this page: <pre class="inline:true decode:1 " >:help usr_02.txt</pre>.
Then, you will learn about <pre class="inline:true decode:1 " >!</pre>, folds, registers, the plugins and many other features. Learn vim like you’d learn piano and all should be fine.