Recommended vim Practical Skills

Source: Internet
Author: User

Note the following before reading this article:

1. The goal of this article is to provide some vim usage skills, which can improve the efficiency of vim operations. Some tips can also be used on vi, but now vim is basically used.

2. this article describes how to sort out and summarize the usage skills, rather than explaining how to get started with vim, so it does not involve basic usage, such as how to move the cursor up or down, for such operations, see any vim or vi tutorial.

3. This article is intended for those who have learned about the basic operations of vim and want to use vim efficiently. Anyone familiar with vim naturally does not have to read it carefully. If you can leave your valuable comments, I would be grateful.

4. I will collect and sort this article. For more information, see the source.

In general, it is case-insensitive to use the letters in this article. Unless otherwise specified) It means to press ctrl and press the relevant letters. Add an I before the command to use this command in insert mode.

1. Select text/copy and paste

V is the visual mode. You can select multiple rows. After multiple rows are selected, you can use yy or dd to copy and cut them.

P is paste

Y and d can directly copy or cut the selected content

Yw is a copy of a word.

The simplest way to copy the entire row is V, y, and p.

V can select any character in a row, and V is selected in a row. Multiple rows are selected by moving the cursor down or up one row at a time.

For the values selected by v, after the copy operation is completed, the values will be copied as much as the values selected. For the values selected by V, the paste operation will automatically switch to the next line.

In command mode, it is also the block selection, but the column block is selected.

2. Collapse code

Zf can be used to fold, zo can be used to open the fold, or the direction key can be used to open the fold to the right, zc can close the fold (as long as any statement in the collapsed block)

3. indent code

Right indent

After selecting by v, press = to automatically format the code and indent the code. The internal recursive indentation is achieved.

Line left shift>, this command is used to adjust the source code indent format is simple and fast.

4. move the cursor

% Is to move from the beginning of braces to the end of braces

: Adding the row number to the back is to jump to this line.

The cursor returns to the previous position. It is equivalent to a "undo" move by the cursor"

The cursor returns to a later position. It is equivalent to "Restoring" when the cursor moves"

5. Multi-file editing/buffer commands

In vim, each opened file corresponds to a buffer ).

Multi-file editing involves two situations. One is that the parameter used before entering vim is multiple files. This is called argument list ). Another scenario is to enter vim and open another file called buffer list ). However, they can all be collectively referred to as buffer.

1) open the file

Vi flname1 flname2... FlnameN

Call multiple files into the buffer, which is an argument list.

: E filename

After entering vim, open other files without leaving vim. As long as the file you want to edit is in the current directory, you can still use the Tab to complete the key. Is the buffer list.

Note: e or: new or: split can be followed by the Directory, so that you can slowly find the file to open under the directory.

2) buffer jump

: N: edit the next file.

: 2n: Edit two files.

: N: edit the previous file. Note that this method can only be used in the case of argument list.

: Rew back to the first file

: Args view the status of the currently edited buffer File

: E # or Ctrl-^ edit the previous file, which is quite useful for mutual editing. This method can be used between argument list or buffer list files. The Ctrl-^ command is more convenient, but the function will be invalid if the terminal type is incorrect.

Use: ls to display the files in the buffer. # indicates the previous file, which can be entered through: e #. % a indicates the current file, if there is nothing, you can enter through: bn, where n represents the file number.

: Move the file name or number B to the file.

In: ls, the number of each file is displayed, which will not be changed before vim is left. This command is also available for elvis. Of course: e # numbers are also acceptable. This rule is applicable to all vi clones.

In buffers, minus sign-indicates that the buffer is not loaded. However, you don't have to worry about it. loading is quite fast. The plus sign + indicates that the buffer has been modified.

: Bn buffer next. N indicates the letter n.

: Bl buffer last.

The preceding two commands are not applicable to elvis.

If you use the vim GUI, the Buffers option will be available on the menu, so you can easily know and move the buffer.

3) Remove the buffer

: Bd (elete) buffer will not be removed before it leaves vim. You can use this command to remove it. What is it about to remove it? Vim is loaded only when you call it. Therefore, these buffers do not occupy memory like cache.

4) re-edit

Discard a modified file and edit it again.

(1) run the following command: q! Force exit and then re-enter vi flname.

(2) Use the command: e! Force re-edit is more convenient. In this way, the file will be opened, but the changes to the current edited file will be abandoned. Otherwise, if the file has changed, vim defaults will not allow you to leave. : E! If nothing is received, all modifications are discarded and the files in the editing are reloaded.

5) Other commands

: Files or: buffers or: ls lists all files in the current buffer.

In elvis, you can use B to call buffers.

: F or Ctrl-g: displays the currently edited file name, whether the file is modified, and where the current cursor is located.

: F file name changes the file name in editing. (File)

: R file name inserts the content of a file at the cursor position. (Read)

: The 35 r file name inserts the file into 35 rows.

Gf: this is a special method for vim to open a file. It will open the file named "word" at the place where the cursor is located. Of course, this file must be in the current directory; otherwise, a new file will be created.

6. Search commands

After you use/to search for a word, n can be jumped to the next one, and N is the previous one. nohls can be used to cancel highlighting.

When searching, the: set ignorecase option causes VIM to ignore case sensitivity and the ": set noignorecase" option to disable this function.

7. Modify text

Cw: deletes a word and enters the insert mode. cc: deletes a row and enters the insert mode.

R: The entered letters will replace the current letter and keep the command mode. R will not stop replacing (one by one ).

0 to the beginning of the line, $ to the end of the line.

8. jump between functions

Ctrl +] And ctrl + T are the definitions and responses of search functions, respectively. It seems that the support of ctag is required.

I jump to the definition line of the identifier referred to by the cursor. It is to open a new small window and display it. Remember to add an I. It is best to find a file under your project. Otherwise, it is better to find a library function than man.

Capital K to view the man help page of the identifier pointed by the cursor

9. Window commands

= Ctrl + w

: The split File Name displays the content of multiple files on a page at the same time, similar to multiple windows, use to switch the current window

F. Split the file name where the cursor is located. VIM searches for the file name in path. For example, it is often used to open the file in # include statement.

_ When several files are opened at the same time, press _ to maximize the current window

Run Ctrl-W to specify the cursor movement:

Ctrl-W + expand window

Ctrl-W-zoom out window

Ctrl-W h move to the left of the window

Ctrl-W j move to the bottom of the window

Ctrl-W k move to the top of the window

Ctrl-W l move to the right of the window

After you press it, release the keyboard and then press the next command.

If you want to close the split window, you can use close. If there is only one window left, it cannot be closed.

Multiple windows are split, just like opening multiple files with e is to put the files in the buffer zone.

10. Automatic completion

I. Search up and add all the words

I. Search down and add all the words

I.

For example, you have written a line for (int I = 0; I. If the line is not the one you want, you can press or

Select the last or next matching row I to search in the file system and add all the file names.

If the match is not found in the current file after the word is pressed or supplemented, VIM searches for the file in the # include statement, and the file location searches in the path.

I. Copy the characters in the corresponding column of the previous row.

I copied the characters in the corresponding column of the next row. Both of them can be pressed until the end of the row stops.

11. comment out the entire content

Annotation block method:

Select the line I // Esc to comment out

After I, the input is inserted to the front edge of the selected row until Esc.

To remove the annotation: select the annotator //, and then d

After column blocks are selected, I is inserted at the front and A is inserted at the back.

Is selected by column block mode, is selected by row, and v is selected by common

12. Other commands

U can undo the previous operation, ctrl + r can restore

I command to execute a common mode command, return to the insert mode after the execution is complete, no need to run multiple Esc

I. Enter special ASCII characters or keys for subsequent characters.

. Repeat the previous operation at the current position of the cursor

:! Execute an external command on the command line.

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.