Efficient editor vim-operations

Source: Internet
Author: User

Although Vim was used a long time agoTuneHanging
Sub-statements. Recently, to improve writingCodeEfficiency, as well as coding fun, and re-study vim, found a good Vim
Quick StartArticleThe original text is in the English version. I think it is very suitable for getting started with vim, So I translated it. Here is a simple introduction to the vim operation method, and does not explain why it is used.
Vim, 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
Just like a "dumb" editor. Many new users will stay in the insert mode because it is easy to use. But Vim
Its command line mode is powerful! 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. Use
Vim, you do not need to move between the arrow key and the primary key frequently, which saves you a lot of time. When you are in command mode, you can use H, J, K, 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 section is
Vim seems to be a long line. 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
It 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 "Cross", 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 word, then press a key, and then Vim
The long term is entered for you. For example, you have a variable named iamalongandawkwardvarname.
Somewhere in the code you write. 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 Motion
      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 will delete the exclamation point from the cursor position to the next one (but not the package
      And then enter 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.

Valid text for moving large segments

Use Visual selections and the appropriate selection mode

Unlike the original VI, VIM allows you to highlight (select) some text and perform operations. Three Visual selection modes are available:

    • V: select by character. Frequently used mode, so try it yourself.
    • V: select by row. This is especially useful when you want to copy or move many lines of text.
    • <C-V>: select by block. Very powerful. This function is available only in a few editors. You can select a rectangle and the text in the rectangle is highlighted.

When selecting a mode, use the arrow keys and commands described above (motion ). For example, vwww highlights the three words in front of the cursor. Vjj will highlight the current line and the following two lines.

Cut and copy in visual selection mode

Once you highlight the selection area, you may want to perform some operations:

    • D: paste the selected content to the clipboard.
    • Y: copy the selected content to the clipboard.
    • C: paste the selected content to the clipboard and enter the insert mode.

Cut and copy in non-visual selection mode

If you know exactly what you want to copy or cut, you do not need to enter the visual selection mode. This will save time:

    • D {motion}: Cut the characters crossed by the motion command to the clipboard. For example, DW cut a word and DFS cut the character from the current cursor to the next S to the clipboard.
    • Y {motion}: similar to the above, but it is a copy.
    • C {motion}: similar to d {motion}, but enters the insert mode.
    • DD: Cut the current row.
    • YY: copy the current row.
    • Cc: Cut the current row and enter the insert mode.
    • D: cut from the cursor position to the end of the row to the clipboard.
    • Y: copy the current row.
    • C: similar to D, and finally enters the insert mode.
    • X: Cut the current character to the clipboard.
    • S: similar to X, but enters the insert mode at last.

Paste

Pasting is simple. Press p.

Use multiple clipboard

Many editors only provide one clipboard. There are many Vim instances. The clipboard is called a register in vim ). You can list all register names and their contents defined currently. The command is ": Reg ". It is best to use lower-case letters as the register name, because some of the upper-case letters are occupied by VIM.

The register command is double quotation marks ".

For example, we want to copy the current row to register K. You should press "kyy. (You can also use v "Ky. Why ?) Now the current row should already exist in register K until you copy something into Register K. Now you can use the "kp" command to paste the content in register K to the desired position.

Avoid repetition

Amazing. Command

In VI, input. (decimal point) will repeat the previous command you entered. For example, if your last command is "DW" (delete a word), VI will delete another word.

Use numbers

Using numbers is also a powerful and time-saving feature of vim. You can use a number before many Vim commands. This number will tell Vim how many times this command needs to be executed. For example:

    • 3j moves the cursor down three rows.
    • 10 DD will delete 10 rows.
    • Y3 "copies the content from the current cursor to the third quotation mark to the clipboard.

Numbers are very effective methods to extend the scope of the motion command.

Record macro

Sometimes, you will find that you repeat the same series of actions in each segment or line of the article. Vim allows you to record a macro to fulfill your special needs.

    • Qregister: Record macro to register. Here, register is your register name. For example, QA records and stores Macros in register.
    • Q: The Record of the ending macro.
    • @ Register: Use a macro with a register. For example, @ A will use Macros in register.

You must remember that macros only record your series of keys and repeat them. They are not magic. There are many methods to accomplish the goal in Vim, so sometimes you need to carefully select commands to record your macros. Because they will be executed in all the places where you want to execute it.

Use Vim to write code

Vim is an excellent editor for writing code, because it has some features dedicatedProgramDesigned by members. Here are some common examples:

    • ] P: similar to P, but it automatically adjusts the indent of the pasted text to adapt to the position of the current Code. Try it!
    • %: Matching curly braces, square brackets, and brackets. On the top of a bracket, press %, and the mouse will appear at the other half of the matching bracket.
    • >>: Indent all selected codes.
    • <: Similar to the preceding but not indented
    • GD: arrives at the position where the cursor is located, where the function or variable is defined.
    • K: Search for the word at the current position of the cursor in man.

[Original text | translation/Jay]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.