The following article is translated from "Learn vim progressively", I think this is the best vim for the Novice upgrade tutorial, did not enumerate all the commands, just list the most useful commands. Very good.
Start —————————— body ——————————
Do you want to learn the best text editor vim in human history at the fastest speed? You have to know how to survive vim and then learn tricks at 1.1 o ' all.
Vim The Six billion Dollar editor
Better, Stronger, Faster.
Learn vim and it will be your last text editor to use. There's no better text editor, it's hard to learn, but it's incredibly easy to use.
I suggest the following four steps:
- Survive
- Feel good
- Feel better, stronger, faster
- Use Vim's powers
As you walk through this article, you will become a vim superstar.
Before I start learning, I need to give you some warning:
- Learning Vim is painful at first.
- Take time
- You need to practice constantly, just as you learn an instrument.
- Don't expect you to be able to make vim more efficient than other editors within 3 days.
- In fact, you need 2 weeks of hard practice, not 3 days.
First Level – Survival
- Install Vim
- Start vim
- Don't do anything! Please read first
Once you've installed an editor, you'll want to enter something in it and see what the editor looks like. But Vim is not like this, please follow the following command:
- After starting vim, Vim is in Normal mode.
- Let's go into Insert mode, press the key I. (Chenhao Note: You will see a –insert– word in the lower left corner of vim, indicating that you can enter it as inserted)
- At this point, you can enter the text as if you were using Notepad.
- If you want to return to Normal mode, press the
ESC
key.
Now, you know how to switch between Insert and Normal mode. Here are some commands that will allow you to survive in Normal mode:
i
→ Insert mode, press ESC
back to Normal mode.
x
→ Delete one character of the current cursor.
:wq
→ Save + exit (save :w
, :q
exit) (Chenhao Note:: W can be followed by file name)
dd
→ Delete the current line and save the deleted line to the Clipboard
p
→ Paste Clipboard
Recommended :
hjkl
(strong example recommends using it to move the cursor, but not necessary) → You can also use the cursor key (←↓↑→). Note: j
just like the down arrow.
:help <command>
→ Displays help for related commands. You can also type without :help
command. (Chenhao Note: Exit help requires input: Q)
If you can survive vim and only need the 5 commands above, you will be able to edit the text, and you must practice these commands in a subconscious state. So you can start to step into the second level.
When you enter the second level, you need to say the Normal mode again. In the general editor, when you need to copy a paragraph of text, you need to use a Ctrl
key, such as: Ctrl-C
. In other words, the CTRL key is like a function key, when you press the function key CTRL, C is not C, and is a command or a fast key, in the normal mode of vim, all keys are function keys . This you need to know.
Mark:
- In the following text, if it is
Ctrl-λ
I will write <C-λ>
.
- To
:
start with the command you need to enter a <enter>
carriage return, for example-if I write :q
that you want to enter :q<enter>
.
Second level – Feels good
The above commands only allow you to survive, now it's time to learn some more commands, here's my advice: (Chenhao Note: All commands need to be used in normal mode, if you don't know what mode you are in now, you can press the ESC key a few times)
- Various insertion modes
a
→ Insert after cursor
o
→ Insert a new row after the current line
O
→ Insert a new row before the current line
cw
→ Replace the character from the position of the cursor to the end of a word
- A simple Move cursor
0
→ Digital zero, to the wardrobe
^
→ To the first position of the bank that is not the blank character (so-called blank character is space, tab, line break, carriage return, etc.)
$
→ To the end of our line
g_
→ To the last position of the bank that is not a blank character.
/pattern
→ Search pattern
for strings (Chenhao Note: If you search for multiple matches, you can press N to next)
- copy/paste (Chenhao Note: p/p can be, p is indicated after the current position, p means before the current position)
P
→ Paste
yy
→ Copy current line of business inddP
- Undo/redo
- Open/Save/exit/change Files (Buffer)
:e <path/to/file>
→ Open a file
:w
→ Disk
:saveas <path/to/file>
→ Save As<path/to/file>
:x
, ZZ
or :wq
→ Save and exit ( :x
means save only when needed, ZZ does not need to enter a colon and return)
:q!
→ Exit does not save any :qa!
files that are being edited by forcibly exiting, even if other files have changed.
:bn
and :bp
→ You can open many files at the same time, use these two commands to switch to the next or previous file. (Chenhao Note: I like to use: N to the next file)
Take the time to familiarize yourself with the above commands, and once you've mastered them, you can almost do what other editors are capable of. But until now, you still think it's a bit awkward to use vim, but it doesn't matter, you can step to the third level.
Level three – better, stronger, faster
Congratulations First! You did a good job. We can start something a little more interesting. At the third level, we'll talk about only those commands that are compatible with VI.
Better
Below, let's look at how vim repeats itself:
.
→ (decimal point) You can repeat the last command
- n<command>→ repeat a command n times
Here is an example, find a file you can try the following command:
2dd
→ Delete 2 rows
3p
→ Paste text 3 times
100idesu [ESC]
→ 会写下 “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu de su desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu d ESU desu desu desu "
.
→ Repeat the previous command--100 "Desu".
3.
→ Repeat 3 times "desu" (Note: Not 300, you see, vim how clever AH).
More powerful
You want to make your cursor move more efficiently, you must understand the following commands, never skip .
- n
G
→ to Nth line (Chenhao Note: Note that the G in the command is uppercase, and I generally use: N to nth rows, such as: 137 To line 137th)
gg
→ to the first line. (Chenhao Note: equivalent to 1G, or: 1)
G
→ to the last line.
- 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 lowercase e and W. By default, a word consists of letters, numbers, and underscores (Chenhao Note: program variables)
> If you think the word is delimited by the blank character, then you need to use uppercase E and W. (Chenhao Note: program statement)
Below, let me say the strongest cursor movement:
%
: Match parentheses move, including (
, {
, [
. (Chenhao Note: You need to move the cursor to the parentheses first)
*
and #
: matches the current word of the cursor, moves the cursor to the next (or previous) match word (* is next, #是上一个)
Believe me, the above three commands are quite powerful for programmers.
Faster
You must remember to move the cursor, as many commands can be linked to the commands that move the cursor. Many commands can be done as follows:
<start position><command><end position>
For example 0y$
the command means:
0
→ first come to the wardrobe
y
→ Start copying from here
$
→ Copy to the last character of the bank
You can enter ye
, copy from the current position to the last character of the word.
You can also enter a y2/foo
string to copy between 2 "foo".
There is still a lot of time that you must press Y to copy, and the following command will be copied:
d
Delete
v
(Visual selection)
gU
(Change capital)
gu
(Variable lowercase)
- Wait a minute
(Chenhao Note: Visual selection is a very interesting command, you can first press V, then move the cursor, you will see the text is selected, and then, you may be d, can also Y, can also be capitalized, etc.) fourth level –VIM Super ability
You just have to master the previous commands and you can use vim very comfortably. But now, we're going to introduce you to the Vim killer feature. The following features are the reason I use vim only.
Move the cursor over the current line:
0
^
$
f
F
t
T
,
;
0
→ to Wardrobe
^
→ First non-blank character to the bank
$
→ To the end of the line
g_
→ To the last position of the bank that is not a blank character.
fa
→ To the next character of a, you can also fs to the next character for S.
t,
→ To the first character before a comma. Commas can become other characters.
3fa
→ Find the third occurrence of a in the current line.
F
and T
→ and f
the t
same, just the opposite direction.
There is also a very useful command is dt"
→ Delete all the content until you encounter double quotes--"。
Regional selection
<action>a<object>
Or
<action>i<object>
In visual mode, these commands are powerful and their command format is
<action>a<object>
And<action>i<object>
- The action can be any command, such as
d
(delete), ( y
copy), v
(can be selected depending on the mode).
- The object may be:
w
A word, W
a word separated by a space, a word, a s
p
paragraph. It can also be a special character: "、
‘、
)、
}、
]。
Suppose you have a string (map (+) ("foo"))
. The cursor key is in the o
first position.
vi"
→ Will choose foo
.
va"
→ Will choose "foo"
.
vi)
→ Will choose "foo"
.
va)
→ Will choose ("foo")
.
v2i)
→ Will Choosemap (+) ("foo")
v2a)
→ Will Choose(map (+) ("foo"))
Block Operation:
<C-v>
Block operations, typical operations:0 <C-v> <C-d> I-- [ESC]
^
→ to Wardrobe
<C-v>
→ Start block operation
<C-d>
→ Move down (you can also use HJKL to move the cursor, or use%, or something else)
I-- [ESC]
→i is inserted, insert " --
", press ESC to take effect for each line.
Under Windows Vim, you need to use <C-q>
instead <C-v>
of copy the <C-v>
Clipboard.
Auto Tip:
<C-n>
And
<C-p>
In Insert mode, you can enter the beginning of a word, and then press<C-p>或是<C-n>,自动补齐功能就出现了……
Macro recording:
qa
Sequence of operations
q
,
@a
,
@@
qa
Record your operation in the registera。
- This
@a
will replay the macro that was recorded.
@@
is a shortcut key used to replay the latest recorded macro.
Example
In a text that has only one row and this line has only "1", type the following command:
qaYp<C-a>q
→
qa
Start recording
Yp
Copy rows.
<C-a>
increased by 1.
q
Stop recording.
@a
→ under 1 write down 2
@@
→ Write down 3 on 2 front
- Doing this now
[email protected]@
creates a new 100 rows and increases the data to 103.
Visualization options:
v
,
V
,
<C-v>
Earlier, we saw <C-v>
The example (under Windows should be <C-q>) that we can use v
and V
. Once you've been chosen, you can do the following:
J
→ Connect all rows together (into one line)
<
or >
→ Left and right indent
=
→ Auto-indent (Chenhao Note: This feature is quite powerful, I love it too)
Add something after all the selected lines:
<C-v>
- Select the relevant line (you can use or or ...
j
<C-d>
/pattern
%
)
$
To the end of the line
A
, enter a string, pressESC。
Split screen:
:split
And
vsplit
.
Here are the main commands you can use to help with Vim :help split
. You can refer to the previous article of the site Vim split screen.
:split
→ Create split screen ( :vsplit
create vertical split screen)
<C-w><dir>
: dir is the direction, can be hjkl
or one of ←↓↑→, it is used to switch split screen.
<C-w>_
(or <C-w>|
): Maximized size (<c-w>| vertical split screen)
<C-w>+
(or <C-w>-
): Increase size
Conclusion
- Above are the 90% most commonly used commands by the author.
- I suggest you learn 1 to 2 new commands every day.
- After three weeks, you will feel the power of vim.
- Sometimes, learning vim is like dying with something.
- Fortunately, VIM has a lot of great tools and excellent documentation.
- Run Vimtutor until you are familiar with the basic commands.
- In its online Help documentation you should read carefully
:help usr_02.txt
.
- You will learn
!,
many other functions such as directories, registers, plugins, and so on.
Learning Vim is just like learning to play the piano, and once learned, the benefits are endless.
—————————— body Ends ——————————
For Vi/vim, it's just a little bit of a review: This is an editor where you don't need to use a mouse, you don't have to use a keypad, you just need to use a large keyboard to do a lot of complex text editing.
Vim Practice Level Guide