This tutorial describes some of the basic techniques for using VIM in different modes of operation-insert mode, command mode, access to files, and so on. The goal is to help newcomers who have just touched vim to use this excellent editor more efficiently.
Description: In this article,<c-x> represents CTRL + X--just hold down the CTRL key and then press X. And you can use it in many cases: help command to get most of the commands, this is VIM's internal Help file command.
1.? High-efficiency mobile 1.1? Outside the Insert mode
Basically, you should stay in the insertion mode as little as possible, because Vim is like a "dumb" editor in insert mode. Many novices will always stay in the insert mode because it is easy to use. But the power of vim is in his command-line mode! You'll find that after you get to know vim more and more, you'll spend less time using insert mode.
1.2? Using h,j,k,l
The first step in using Vim for efficient editing is to give up using the arrow keys. With VIM, you don't have to move between the arrow keys and the letter keys frequently, which can save you a lot of time. When you are in command mode, you can use h,j,k,l to implement the function of left, bottom, upper, and right arrows respectively. You may need to get used to it at first, but once you get used to it, you'll find the efficiency of this operation.
When you edit your e-mail or other text that has paragraphs, you may find that using the arrow keys is not the same as the effect you expect, and sometimes you may skip a lot of lines at a time. This is because your paragraph in vim appears to be a large long line. You can then type a g before pressing H,J,K or L so that vim moves as you wish on the line above the screen.
1.3? Moves the cursor effectively within the current line
Many editors only provide simple commands to control the movement of the cursor (e.g. left, top, right, bottom, to beginning/end, etc.). VIM provides a number of powerful commands to satisfy your desire to control the cursor. When the cursor moves from one point to another, the text between these two points (including these two points) is called "Crossing", and the command here is called motion. (briefly, this important concept will be used later)
Here are some of the commonly used commands (motion):
FX: Moves the cursor to the next X at the current line. Obviously, X can be any one letter, and you can use it to repeat your last F command.
TX: Similar to the above command, but moves to the left of X. (This is really useful.)
FX: Like FX, it's just looking backwards.
W: The cursor moves forward one word.
B: The cursor moves backwards by one word.
0: Move the cursor to the beginning of the current line.
^: Moves the cursor to the first letter position of the current line.
$: Moves the cursor to the end of the line.
): Move the cursor to the next sentence.
(: Move the cursor to the previous sentence.)
1.4? Move the cursor effectively throughout the file
Vim has a lot of commands that you can use to get to where you want to go in the file. Here are some commands to move inside a file:
<C-F>: Move down one screen.
<c-b>: Move up one screen.
G: To end of file
NUMG: Moves the cursor to the specified line (num). (e.g. 10G is to line 10th)
GG: To the top of the file
H: Move the cursor to the top of the screen
M: Move the cursor to the middle of the screen
L: Move the cursor to the bottom of the screen
*: Reads the string at the cursor and moves the cursor to where it appears again.
#: Similar to the above, but looking in the opposite direction.
/text: Searches for the string text from the current cursor and reaches the text where it appears. You must use carriage return to start this search command. If you want to repeat the last search, press N.
? Text: Similar to the above, but in the opposite direction.
MA: Marks a bookmark at the position of the current cursor, named A. The book signature can only be lowercase letters. You can't see the existence of a bookmark, but it's already there.
' A: Go to bookmark A. Note that this is not a single quote, it is generally on the left side of 1 of most keyboards.
'.: To the place where you last edited the file. This command is useful, and you don't have to tag it yourself.
2.? Efficient input 2.1? Auto-complete with keywords
VIM has a very beautiful keyword auto-complete system. This means that you can enter a part of a long word, then click on a key, and then vim will complete the input of the long word for you. For example: You have a variable named Iamalongandawkwardvarname somewhere in the code you write. Maybe you don't want to enter it every time you go through a letter.
With the Keyword AutoComplete feature, you only need to enter a few letters (such as Iamal), then press <C-N> (Hold down CTRL, press N), or <C-P>. If Vim doesn't give you the word you want, basically press until you're satisfied, and Vim will loop through the matching string it finds.
2.2? Smart into insert mode
Many beginners go into insert mode with just I. This, of course, can go into insert mode, but it is usually not so appropriate because VIM provides a lot of commands to enter the insert mode. Here are some of the most common:
I: Insert on the left of the current character
I: Inserting at the beginning of the current line
A: Insert to the right of the current character
A: Insert at the end of the current line
o: Insert a new row below the current line
O: Insert a new row above the current line
C{motion}: Removes the characters that the motion command crosses and enters insert mode. For example: C $, which removes the character from the cursor position to the end of the line and enters insert mode. Ct! , which removes from the cursor position to the next exclamation mark (but not included) and then into insert mode. The deleted characters are present in the Clipboard and can be pasted again.
D{motion}: Same as above, but not into insert mode.
3.? Effectively move large segments of text
3.1? Use visual selection (visual selections) and the appropriate selection mode
Don't want the initial vi,vim to allow you to highlight (select) some text and manipulate it. There are three types of visual selection modes:
V: Select by character. Often use the pattern, so try it out for yourself.
V: Select by row. This is especially useful when you want to copy or move a lot of lines of text.
<c-v>: Select by block. Very powerful, only in a few editors have such a function. You can select a rectangular block, and the text inside the rectangle will be highlighted.
Use the arrow keys and commands (motion) described above when selecting the mode. For example, VWWW will highlight the three words in front of the cursor. VJJ will highlight the current row and the following two lines.
3.2? Cut and copy in Visual selection mode
Once you highlight the selection, you might want to do something like this:
D: Clip the selected content to the Clipboard.
Y: Copies the selected content to the Clipboard.
C: Clip the selected contents to the Clipboard and enter insert mode.
3.3? Cut and copy in non-visual selection mode
If you know exactly what you want to copy or cut, you don't need to enter the visual selection mode at all. This will also save time:
D{motion}: Cut the character spanned by the motion command to the Clipboard. For example, DW cuts a word and DFS cuts the character from the current cursor to the next s to the Clipboard.
Y{motion}: Similar to the above, but a copy.
C{motion}: Similar to D{motion}, but finally into insert mode.
DD: Cuts the current line.
YY: Copies the current line.
CC: Cuts the current line and enters insert mode.
D: Cut from the cursor position to the end of the line to the Clipboard.
Y: Copies the current line.
C: Similar to D, and finally into insert mode.
X: Cuts the current character to the Clipboard.
S: similar to X, but finally into insert mode.
3.4? Paste
Paste is simple, press p.
3.5? Using multiple clipboard
Many editors provide only one clipboard. There are many vim. The Clipboard is called a register (registers) in Vim. You can list all of the current defined register names and their contents, with the command ": Reg". It is best to use lowercase letters as the name of the register, because some of the capitalization is used by VIM.
The command that uses the register is a double quote ".
For example: We want to copy the current line to register K. You should press "Kyy." (You can also use V "Ky. Why is this possible? Now the current line should already exist in register K until you copy something into register K. Now you can use the command "KP" to paste the contents of the register K into the position you want.
4.? Avoid repeating 4.1? It's amazing. Command
Inside VI, enter. (decimal symbol), the last command you gave is repeated. For example, your last command is ' DW ' (delete a word), vi will then delete a word.
4.2? Using numbers
Using numbers is also one of the most powerful and time-saving features of vim. A number can be used before many VIM commands, and this number will tell vim how many times this command needs to be executed. Like what:
3J will move the cursor down three lines.
10DD will delete 10 rows.
Y3 "will copy the contents from the current cursor to the third occurrence of the quotation marks to the Clipboard.
Numbers are a very effective way to extend the scope of motion commands.
4.3? Record macros
Sometimes you will find yourself repeating the same series of actions in every paragraph or line of the article. Vim allows you to record a macro to complete your special needs.
Qregister: Record the macro to register register, where register is the name of any of your registers. such as QA, will be recorded and the macro exists in register a.
Q: End the record of the macro.
@register: Use a macro that has register registers. For example, @a will use a macro that exists in register a.
It's important to remember that macros only record your series of keystrokes and repeat them. They're not magic. Because there are many ways to accomplish a goal in vim, there are times when you should be careful to choose commands to record your macros. Because they will execute wherever you want to execute it.
5.? Write code with VIM
Vim is an excellent editor to write code because it has some features that are specifically designed for programmers. Here are some of the commonly used:
]p: Similar to the function of P, but it automatically adjusts the indentation of the pasted text to fit the position of the current code. Try it!
%: Match curly braces, square brackets, brackets, and so on. At the top of a bracket, and then by%, the mouse appears in the other half of the matching brackets.
>>: Indent all the selected code
<<: Similar to above, but anti-indent
GD: At the definition of the function or variable where the cursor is located.
K: In man, look for the word where the cursor is currently located.
Turn Vim for efficient operation