Vim's learning curve is quite large (see the learning curve of various text editors). Therefore, if you see a lot of VIM command classification at the beginning, you will lose interest in this editor. The following article is translated from "Learn Vim progressively". I think this is the best Vim Upgrade tutorial for beginners. I did not list all the commands, but just listed the most useful commands. Very good.
---------- The text starts ----------
Do you want to learn vim, the best text editor in history, as quickly as possible? You must first learn how to survive vim and then learn various tricks.
Vim the six billion dollar Editor
Better, stronger, faster.
Learn vim and it will become your last text editor. There is no better text editor than this one, which is very difficult to learn, but incredibly easy to use.
I suggest the following four steps:
- Alive
- Feeling good
- Better, stronger, and faster
- Use Vim's super power
After you finish this article, you will become a Vim superstar.
Before getting started, I need to give you some warning:
- Learning Vim is painful at the beginning.
- Time required
- You need to practice constantly, just like learning an instrument.
- Don't expect you to practice Vim more efficiently than other editors in three days.
- In fact, you need two weeks of hard work instead of three days.
Level 1-survival
- Install Vim
- Start Vim
- Do nothing!Read
After installing an editor, you will definitely want to enter something in it and then see what the editor looks like. But Vim is not like this. Follow the command below:
- After Vim is enabled, VIMNormalMode.
- Let's enterInsertMode. Press I. (Chen Hao Note: you will see the "-insert-" in the lower-left corner of VIM, indicating that you can insert it)
- Now you can enter the text, just like using notepad.
- If you want to returnNormalMode. Press ESC.
Now, you know howInsertAndNormalSwitched. Below are some commands for youNormalMode:
- I →InsertMode, Press ESC to returnNormalMode.
- X → delete a character of the current cursor.
- : WQ → save disk + exit (: W save disk,: Q exit) (Chen Hao Note: W can be followed by the file name)
- Dd → Delete the current row and save the row to the clipboard.
- P → paste the clipboard
Recommendation:
- Hjkl (it is recommended that you use a strong example to move the cursor, but it is not required) → you can also use the cursor pointer (). Note: J is like the down arrow.
- : Help <command> → displays help information about related commands. You can also enter: help instead of the command. (Chen Hao Note: To exit the help, enter Q)
You only need the Five Commands mentioned above to survive vim, And you can edit the text. You must train these commands into a subconscious state. So you can start to level 2.
When you enter Level 2NormalMode. In a General Editor, when you need to copy a text segment, you need to use the ctrl key, for example, Ctrl-C. That is to say, the ctrl key is like a function key. After you press the ctrl key, C is no longer a C, and it is a command or a shortcut key,In normal mode of VIM, all keys are function keys.. You need to know this.
MARK:
- In the following text, if it is Ctrl-λ, I will write it as <c-λ>.
- To start a command, enter <enter> Press enter. For example, if Q is written as Q, enter q <enter>.
Level 2-good feeling
The above commands can only survive you. Now it is time to learn more commands. The following is my suggestion: (Chen Hao note: all commands must be used in normal mode. If you do not know the current mode, press the ESC key several times)
- Various insert 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 the character from the cursor position to the end of a word
- Simple move cursor
- 0 → zero number, to the line Header
- ^ → To the first position in the line that is not a blank character (the so-called blank character is a space, tab, line feed, carriage return, etc)
- $ → To the end of the row
- G _ → to the last position of the line that is not a blank character.
- /Pattern → search for the string of pattern (Chen Hao Note: if multiple matches are found, press n to the next one)
- Copy/paste(Chen Hao Note: Both P and P can be used. P indicates that after the current position, P indicates that before the current position)
- P → Paste
- YY → copy the current row to the DDP
- Undo/Redo
- Open/Save/exit/change the file(Buffer)
- : E <path/to/File> → open a file
- : W → Storage
- : Saveas <path/to/File> → save as <path/to/File>
- : X, ZZ or: WQ → save and exit (: X indicates to save only when needed, ZZ does not need to enter a colon and press Enter)
- : Q! → Exit and do not save: QA! Forcibly exit all the files being edited, even if other files are changed.
- : BN and: BP → you can open many files at the same time and use these two commands to switch to the next or previous file. (Chen Hao Note: I like to use: N to the next file)
Take a moment to get familiar with the above commands. Once you have mastered them, you can almost do what other editors can do. So far, you still think that using Vim is a little clumsy, but it doesn't matter. You can go to Level 3.
Level 3-better, stronger, and faster
Congratulations! You did a good job. We can start something more interesting. In Level 3, we only talk about commands that are compatible with VI.
Better
Next, let's take a look at how Vim repeats itself:
- . → (Decimal point) The last command can be repeated
- N <command> → repeat a command n times
The following is an example. To open a file, try the following command:
- 2dd → Delete 2 rows
- 3 P → paste the text three times
- 100 idesu [ESC] → "desu "desu desu"
- . → Repeat the previous command -- 100 "desu".
- 3. → repeat "desu" three times (Note: It's not 300. You see, how clever Vim is ).
Stronger
To make your cursor move more efficiently, you must understand the following commands,Never skip.
- Ng → to line N (Chen Hao Note: note that the G in the command is in upper case, and I generally use: n to line N, for example, lines 137 to 137th)
- Gg → to the first line. (Chen Hao Note: equivalent to 1g, or: 1)
- G → to the last line.
- Move by word:
- W → start with the next word.
- E → to the end of the next word.
> If you think the word is in the default format, use the lower case e and W. By default, a word consists of letters, numbers, and underscores (Chen Hao Note: program variables)
> If you think words are separated by blank characters, you need to use uppercase E and W. (Chen Hao Note: program statements)
Next, let me talk about the strongest cursor movement:
- %: The matching brackets are moved, including (, {, [. (Chen Hao Note: you need to move the cursor first to the brackets)
- * And #: match the current word of the cursor, move the cursor to the next (or the previous) match the word (* is the next, # Is the previous)
Believe me, the above three commands are quite powerful for programmers.
Faster
You must remember the movement of the cursor, because many commands can be linked with the commands that move the cursor. Many commands can be used as follows:
<Start position> <command> <End position>
For example, the 0y $ command means:
- 0 → first come to the line Header
- Y → copy from here
- $ → Copy to the last character of the row
You can enter ye to copy the last character from the current position to the word.
You can also input y2/Foo to copy the strings between two "foo.
There is still a lot of time and you may have to press y to copy the file. The following command will also be copied:
- D (delete)
- V (visual selection)
- Gu (change to uppercase)
- Gu (small write)
- And so on.
(Chen Hao Note: Visual selection is a very interesting command. You can press v first and move the cursor to see the selected text. Then, you may d, Y or uppercase) Level 4-Vim super power
You only need to master the previous commands, and you can use Vim comfortably. But now we will introduce you to the vim killer feature. The following are the reasons why I only use vim.
Move the cursor on the current row: 0 ^ $ f t ,;
- 0 → to the line Header
- ^ → The first non-blank character to the row
- $ → End of line
- G _ → to the last position of the line that is not a blank character.
- Fa → to the next character with a, you can also FS to the next character with S.
- T, → the first character before the comma. A comma can be another character.
- 3fa → search for the third a in the current row.
- F and t → the same as F and t, but in the opposite direction.
Another useful command is DT "→ delete all content until double quotation marks are encountered --".
Select <action> A <Object> or <action> I <Object> for the region.
In visual mode, these commands are powerful in the format
<Action> A <Object> and <action> I <Object>
- Action can be any command, such as D (delete), y (copy), and V (select as mode ).
- The object may be w a word, w a word separated by spaces, s a sentence, and p a paragraph. It can also be a special character: ", ',),},].
Suppose you have a string (MAP (+) ("foo"), and the cursor key is at the first o position.
- VI "→ Foo will be selected.
- Va "→" foo "will be selected ".
- Vi) → select "foo ".
- Va) → select ("foo ").
- V2i) → map (+) ("foo ")
- V2a) → select (MAP (+) ("foo "))
Block operation: <c-V>
Block operation, typical operation: 0 <c-V> <c-d> I -- [ESC]
- ^ → To the line Header
- <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 an insert, insert "--", and press the ESC key to take effect for each row.
In Vim in Windows, you need to use <c-q> instead of <c-V>, and <c-V> to copy the clipboard.
Automatic prompt: <c-N> and <c-P>
In insert mode, you can enter the beginning of a word, and then press <c-P> or <c-N> to automatically complete ......
Macro recording: Q, @ ,@@
- QA records your operations in register.
- So @ A will replay the macro recorded.
- @ Is a shortcut key used to replay the macro of the latest recording.
Example
In the text with only one line and only one line, type the following command:
- Qayp <c-A> q →
- QA starts recording
- YP copies rows.
- <C-A> Add 1.
- Q: stop recording.
- @ A → write down 2 under 1
- → Write 3 in front of 2
- Now [email protected] @ will create a new 100 rows and increase the data to 103.
Visualized options: V, V, <c-V>
We have seen the <c-V> example (<c-q> in Windows). We can use V and V. Once selected, you can do the following:
- J → connect all rows (into one row)
- <Or> → left and right indent
- = → Auto indent (Chen Hao note: this function is quite powerful and I like it too much)
Add something after all selected rows:
- <C-V>
- Select related rows (J, <c-d>,/pattern, %, etc. can be used ......)
- $ To the end of the row
- A. Enter the string and Press ESC.
Split screen: Split and vsplit.
The following are the main Commands. You can use Vim's help: help split. You can refer to the previous article Vim split.
- : Split → create a split screen (: Create a Vertical Split for vsplit)
- <C-W> <dir>: Dir is the direction, which can be one of hjkl or zookeeper →. It is used to switch the split screen.
- <C-W >_( or <c-W> |): Maximize the size (<c-W> | Vertical Split screen)
- <C-W> + (or <c-W>-): increase the size.
Conclusion
- The above is the author's most commonly used 90% command.
- I suggest you learn one or two new commands every day.
- In two to three weeks, you will feel the power of vim.
- Sometimes learning Vim is like memorizing something.
- Fortunately, VIM has many good tools and excellent documents.
- Run vimtutor until you are familiar with the basic commands.
- In the online help documentation, you should carefully read: Help usr_02.txt.
- You will learn such !, Directory, register, plug-in, and many other functions.
Learning Vim is just like learning to play the piano.
---------- Text ended ----------
For VI/vim, let's just comment: this is an editor that you don't need to use a mouse or a keypad. You only need to use a keyboard to edit many complex functions. Otherwise, Visual Studio won't have the vim plug-in.
Vim concise tutorial