vim The learning curve is quite large （ See the learning curve of various text editors ）,
therefore , If you start with a bunch of VIM Command classification of , You will lose interest in this editor . The following article is translated from 《Learn Vim Progressively》, This is the best for beginners VIM Upgrade the tutorial , It doesn't list all the commands , It's just a list of the most useful commands . Very good .
You want to learn as quickly as possible the best text editor in human history VIM Do you ？ You have to know how to be in VIM Survived , And learn all kinds of tricks bit by bit .
Vim the Six Billion Dollar editor
Better, Stronger, Faster.
Study vim And it will be your last text editor to use . There is no better text editor than this , It's very difficult to learn , But it's incredibly easy to use .
I suggest the following four steps ：
- Feel good
- Feel better , stronger , faster
- Use VIM The power of
When you finish this article , You're going to be a vim Of superstar.
Before learning , I need to give you some warning ：
- Study vim It was painful at the beginning .
- It takes time to
- It needs constant practice , It's like learning an instrument .
- Don't expect you to be in 3 In the day vim Practice more efficiently than other editors .
- in fact , You need 2 Weeks of hard work , instead of 3 God .
- first stage – Survive
- Level second – Feel good
- Level third – Better , stronger , faster
- Level fourth – Vim Superpowers
first stage – Survive
- install vim
- start-up vim
- Don't do anything ！ Please read first
When you install an editor , You must want to type something in it , And see what the editor looks like . but vim That's not the truth , Please follow the command below ：
- Qi dynamic Vim after ,vim stay Normal In mode .
- Let's get into Insert Pattern , Please press the key i .( Chen haozhu ： You'll see vim There is a... In the lower left corner –insert– word , Express , You can type in as an insert ）
- here , You can enter the text , It's like you use “ Notepad ” equally .
- If you want to go back Normal Pattern , Please press
Now? , You know how to do it in Insert and Normal Mode switched . Here are some commands , I can let you in Normal Survive in mode ：
i→ Insert Pattern , Press
ESCgo back to Normal Pattern .
x→ Delete a character of the current cursor .
:wq→ Save + sign out (
:qsign out ) （ Chen haozhu ：:w Can be followed by the file name ）
dd→ Delete current row , And save the deleted lines to the clipboard
p→ Paste the clipboard
hjkl( It is recommended to use its moving cursor , But it doesn't have to be ) → You can also use the cursor keys (←↓↑→). notes :
jIt's like a down arrow .
:help <command>→ Display help for related commands . You can also just type in
:helpInstead of following orders .（ Chen haozhu ： Exit help requires input :q）
Can you in vim Only the one mentioned above is needed to survive 5 An order , You can edit the text , You have to practice these commands into a subconscious state . So you can start to advance to the second level .
when , When you get to level two , I need to say it again Normal Pattern . In a general editor , When you need copy A paragraph of text , You need to use
Ctrl key , such as ：
Ctrl-C. in other words ,Ctrl Keys are like function keys , When you press the function key Ctrl after ,C It's not about it C 了 , And it's just a command or a fast key , stay VIM Of Normal In mode , All the keys are function keys . You need to know .
- In the following text , If it is
Ctrl-λI can write
:To start, you need to type
<enter>enter , for example — If I were
:qThat means you have to type in
Level second – Feel good
The orders above will only keep you alive , Now it's time to learn some more commands , Here are my suggestions ：（ Chen haozhu ： All commands need to be in Normal Use... In mode , If you don't know what kind of model you're in right now , You just press it a few times ESC key ）
Various insertion modes
a→ Insert... After the cursor
o→ Insert a new row after the current row
O→ Insert a new row before the current row
cw→ Replace characters from the end of the cursor to the end of a word
Simply move the cursor
0→ The number zero , To the head of the line
^→ The first one in the line is not blank Character position （ So-called blank Characters are spaces ,tab, Line break , Return, etc ）
$→ At the end of the line
g_→ The last one in the line is not blank Character position .
/pattern→ Search for
patternString （ Chen haozhu ： If you find multiple matches , May press n Key to the next ）
Copy / Paste
（ Chen haozhu ：p/P Fine ,p After the current position ,P Before the current position ）
yy→ Copy the current line on
open / preservation / sign out / Change file
:e <path/to/file>→ Open a file
:saveas <path/to/file>→ Save as
:wq→ Save and exit (
:xMeans to save only when needed ,ZZ You don't need to enter a colon and enter )
:q!→ Exit without saving
:qa!Forcibly exit all files being edited , Even if other documents are changed .
:bp→ You can open a lot of files at the same time , Use these two commands to switch between the next or previous file .（ Chen haozhu ： I like to use :n To the next file ）
Take a moment to familiarize yourself with the above commands , Once you've got them , You can do almost anything other editors can do . But so far , You still feel like using vim Still a little clumsy , But that's okay , You can go up to level three .
Level third – Better , stronger , faster
Congratulations first ！ You did a good job . We can start something more interesting . At level three , We're just talking about those and vi Compatible commands .
below , Let's take a look vim How to repeat yourself ：
.→ ( decimal point ) You can repeat the last command
→ Repeat a command N Time
Here's an example , To find a file, you can try the following command ：
2dd→ Delete 2 That's ok
3p→ Paste text 3 Time
100idesu [ESC]→ Will write down “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 “
.→ Repeat the last order —— 100 “desu “.
3.→ repeat 3 Time “desu” ( Be careful ： No 300, You see ,VIM How clever ).
You need to make your cursor move more efficiently , You must understand the following commands , Don't skip .
G→ To the first N That's ok （ Chen haozhu ： Pay attention to the... In the order G It's in capital letters , I usually use : N To the first N That's ok , Such as :137 To the first 137 That's ok ）
gg→ To the first line .（ Chen haozhu ： amount to 1G, or :1）
G→ To the last row .
Move by word ：
w→ To the beginning of the next word .
e→ To the end of the next word .
> If you think the word is by default , Then use lower case e and w. By default , A word consists of letters , Numbers and underscores （ Chen haozhu ： Program variables ）
> If you think the word is by blank Character separator , Then you need to use uppercase E and W.（ Chen haozhu ： Program statement ）
below , Let me talk about the strongest cursor movement ：
%: Match bracket move , Include
[. （ Chen haozhu ： You need to move the cursor over the brackets first ）
#: Match the current word of the cursor , Move the cursor to the next （ Or the last one ） Match words （* It's the next one ,# It's the last one ）
believe me , These three commands are quite powerful for programmers .
You must remember the movement of the cursor , Because a lot of commands can be linked to these commands that move the cursor . A lot of orders can be done like this ：
<start position><command><end position>
0y$ Command means ：
0→ First of all
y→ Copy from here
$→ Copy to the last character of the line
You can type in
ye, Copy from the current position to the last character of the word .
You can also type in
y2/foo To copy 2 individual “foo” String between .
There's a lot of time left, and you don't have to press y To copy , The following command will also be copied ：
d( Delete )
v( The choice of Visualization )
gU( Capitalize )
gu( Change to lowercase )
（ Chen haozhu ： Visual selection is an interesting command , You can press first v, Then move the cursor , You'll see the text selected , then , You may be d, Can also be y, It can also be capitalized ）
Level fourth – Vim Superpowers
You just need to master the command ahead , You can use it very comfortably VIM 了 . however , Now? , We introduce you to VIM Killer features . The following functions are only used by me vim Why .
Move the cursor on the current line :
0→ To the head of the line
^→ Not the first one in the line blank character
g_→ The last one in the line is not blank Character position .
fa→ Go to the next one for a At the character of , You can also fs Go to the next one for s The characters of .
t,→ To the first character before the comma . Commas can be changed into other characters .
3fa→ Find the third occurrence in the current line a.
tequally , It's just in the opposite direction .
Another useful command is
dt" → Delete everything , Until the double quotation marks ——
stay visual In mode , These commands are powerful , The command format is
<action>a<object>` and `<action>i<object>
- action It can be any order , Such as
d( Delete ),
y( Copy ),
v( You can choose according to the mode ).
- object May be ：
wA word ,
WA word separated by spaces ,
sA word ,
pA paragraph . It can also be a special character ：
Suppose you have a string
(map (+) ("foo")). And the cursor key is at the first
o The location of .
vi"→ Will choose
va"→ Will choose
vi)→ Will choose
va)→ Will choose
v2i)→ Will choose
map (+) ("foo")
v2a)→ Will choose
(map (+) ("foo"))
Block operation :
Block operation , Typical operation ：
0 <C-v> <C-d> I-- [ESC]
^→ To the head of the line
<C-v>→ Start block operation
<C-d>→ Move down the ( You can also use hjkl To move the cursor , Or use %, Or something else )
I-- [ESC]→ I It's insertion , Insert “
--”, Press ESC Key to work for each line .
stay Windows Under the vim, You need to use
<C-q> instead of
<C-v> It's the copy clipboard .
Automatic prompt ：
stay Insert In mode , You can enter the beginning of a word , Then press
<C-p> or <C-n>, The auto complement function appears ……
Macro recording ：
qa Operation sequence
qaRecord your operations in a register
@aMeeting replay Recorded macro .
@@It's a shortcut key for replay The latest recorded macro .
* Example *
There is only one line in a row and this line has only “1” In the text of , Type the following command ：
YpCopy line .
qStop recording .
@a→ stay 1 Write it down below 2
@@→ stay 2 Write on the front 3
Do it now
100@@New will be created 100 That's ok , And add the data to 103.
Visual selection ：
front , We see that
<C-v> An example of （ stay Windows It should be
V. Once selected , You can do the following ：
J→ Connect all the lines together （ Become a line ）
>→ Indent left and right
=→ Indent automatically （ Chen haozhu ： It's quite powerful , I love ）
Add something after all the selected lines ：
- Select the relevant line ( You can use
$At the end of the line
A, Input string , Press
Split screen :
Here are the main commands , You can use VIM With the help of the
:help split. You can refer to a previous article on this site VIM Split screen .
:split→ Create split screen (
:vsplitCreate vertical split )
<C-w><dir>: dir It's the direction , It can be
hjklor ←↓↑→ One of them , It's used to switch split screens .
<C-w>|) : Maximize size (
| Vertical split screen )
<C-w>-) : Increase the size
The above is the most commonly used one 90% The order of .
I suggest that you learn... Every day 1 To 2 A new order .
In two or three weeks , You will feel vim Powerful .
occasionally , Study VIM It's like memorizing something .
Fortunately, ,vim There are lots of great tools and excellent documentation .
function vimtutor Until you get familiar with the basic commands .
What you should read carefully in its online help document is
You'll learn things like
!,Catalog , register , Plug ins and many other functions .
Study vim It's like learning to play the piano , Once you learn , benefit .
about vi/vim Just a little comment ： You don't need a mouse , No need to use a keypad , It is an editor that can complete many complex functions of text editing just by using the big keyboard . Otherwise ,Visual Studio And there won't be vim Plugin .