Address: http://linuxtoy.org/archives/efficient-editing-with-vim.html
Although Vim was used a long time agoTuneClick here and use only the commands you want. Recently, in order to improve code writing efficiency and coding fun, I re-studied vim and found a good Vim getting started article. The original Article is in English, I think it is very suitable for getting started with vim, So I translated it. Here is a simple introduction to Vim's operation method, and it doesn't mean why Vim is used. If you want to know the answer, you can go to Google. Vim is hailed as the god of editors.
This tutorial describes some basic tips for using Vim in different working modes: insert mode, command mode, and file access. The goal is to help new users who have just been familiar with vim use this excellent editor more efficiently.
(In this article, <C-X> represents Ctrl + X -- is to press the ctrl key and then press X. In addition, you can use the: HELP command to get help from most commands in many cases. This is the internal help file command of vim.
High Efficiency mobile
Outside the insert mode
Basically, you should stay in the insert mode as little as possible, because in the insert mode, VIM is like a "dumb" editor. Many new users will stay in the insert mode because it is easy to use. But Vim is powerful in its command line mode! You will find that after you get to know Vim more and more, you will spend less and less time using the insert mode.
Use H, J, K, L
The first step to use Vim for efficient editing is to discard the arrow keys. With vim, you do not need to move between the arrow keys and the keys frequently, which saves you a lot of time. In command mode, you can use H, J, K, and l to implement the left, bottom, top, and right arrows respectively. You may need to adapt to this method at the beginning, but once you get used to this method, you will find the efficiency of this operation.
When you edit your email or other text with paragraphs, you may find that the direction keys are different from what you expected. Sometimes you may skip many lines at a time. This is because your paragraph is a long line in Vim's view. In this case, you can type a G before H, J, K, or l, so that Vim will move as you wish on the screen.
Move the cursor effectively in the current row
Many editors only provide simple commands to control the movement of the cursor (such as left, top, right, bottom, to the beginning/End of the line ). Vim provides many powerful commands to satisfy your desire to control the cursor. When the cursor moves from one point to another, the text between the two points (including the two points) is called "Crossing", and the command here is also called "motion. (This important concept will be used later)
Here are some commonly used commands (motion ):
- FX: move the cursor to the next X of the current row. Obviously, X can be any letter, and you can use; to repeat your previous F command.
- TX: similar to the preceding command, but it is moved to the left of X. (This is really useful)
- FX: it is similar to Fx, but you can look back.
- W: move the cursor forward to a word.
- B: Move a word behind the cursor.
- 0: move the cursor to the beginning of the current row.
- ^: Move the cursor to the first letter of the current row.
- $: Move the cursor to the end of the row.
- ): Move the cursor to the next sentence.
- (: Move the cursor to the previous sentence.
Move the cursor effectively throughout the file
Vim has many commands that can be used to reach the desired place in the file. The following are some commands for moving data in a file:
- <C-F>: Move a screen down.
- <C-B>: Move a screen up.
- G: to the end of the file
- Numg: move the cursor to the specified row (Num ). (For example, 10 Gb is to 10th rows)
- GG: to the beginning of the file
- H: move the cursor to the screen
- M: move the cursor to the center of the screen
- L: move the cursor to the screen
- *: Read the string at the cursor and move the cursor to where it appears again.
- #: It is similar to the above, but it is looking in the opposite direction.
- /Text: search for the string text from the current cursor and reach the place where the text appears. You must press enter to start the search command. If you want to repeat the previous search, press n.
- ? Text: similar to the above, but in the opposite direction.
- MA: Mark a bookmark at the position of the current cursor. Its name is. Only lowercase letters are allowed. You can't see the existence of bookmarks, but it is already there.
- 'A: To bookmarks. Note that this is not a single quotation mark. It is generally located on the left of 1 of most keyboards.
- '.: The last time you edited the file. This command is useful, and you do not need to mark it yourself.
Efficient input
Automatically complete with keywords
Vim has a very beautiful keyword auto-completion system. This means that you can enter a part of a long term, and then press a key. Then Vim completes the long term input for you. For example, you have a variable named iamalongandawkwardvarname somewhere in your code. Maybe you don't want to input it one by one every time.
With the keyword AutoComplete function, you only need to enter the first few letters (such as iamal), then press <C-N> (press Ctrl, then press N) or <C-P>. If Vim does not give the words you want, continue to press until you are satisfied, VIM will keep repeating the matching strings it finds.
Smart entry into insert mode
Many new users only use I to enter the insert mode. In this way, you can enter the insert mode, but it is usually not that appropriate, because Vim provides many commands to enter the insert mode. Below are some of the most commonly used:
- I: insert it to the left of the Current Character
- I: insert at the beginning of the current row
- A: insert it to the right of the Current Character
- A: insert at the end of the current row
- O: Insert a new row under the current row.
- O: Insert a new row on the current row.
- C {motion}: Delete the characters crossed by the motion command and enter the insert mode. For example: C $, this will delete the characters from the cursor position to the end of the line and enter the insert mode. CT !, This removes the exclamation point from the cursor position to the next one (but not included), and then enters the insert mode. The deleted character is stored in the clipboard and can be pasted out.
- D {motion}: similar to the above, but does not enter the insert mode.