Summary of common Linux VI (Vim) commands)

Source: Internet
Author: User
Tags alphanumeric characters
Preface

The Vim VI is a widely used text editor in Unix/Linux. Most machines have different types of VI, we often use different variant software on different machines, among which Vim is relatively easy to use and widely used. Vim is short for VI improved, which indicates better VI, commonly usedCommandIf you are proficient in using these commands, you will find that editing files is very helpful, especially for developers, which can quickly improve development efficiency.

Description

In the following example, xxx indicates entering XXX in command mode and press Enter.
In the following example: XXX indicates entering XXX in extended mode and press ENTER
The commands in parentheses indicate related commands.
The command entered in edit mode or visual mode will be noted separately

 

1. Search
/XXX (?XXX) indicates searching for strings matching XXX in the entire document,/Indicates searching down ,?It indicates that xxx can be a regular expression in the upward search. We will not talk about the regular expression much. generally, it is case-sensitive. To make it case-insensitive, you must first enter: Set ignorecase to find it, then enter n to find the next match, and enter n to search for it.*(#)When the cursor stays on a word, enter this command to search for the next (top) word that matches the word. Then enter n to find the next match, and enter n to search for G.* (G#)This command is similar to the previous command, except that it does not fully match the word where the cursor is located, but matches all strings containing the word GD. This command looks for words that match the word where the cursor is located, place the cursor in the non-comment section of the document where the word appears for the first time.%This command looks for the parentheses that match the cursor, including () [] {} f (f) X. This command indicates searching in the row where the cursor is located, search for the first X character on the right (left) side of the cursor. After the cursor is found, enter the cursor. Search for the cursor in the opposite direction.

 

2. move the cursor quickly
In VI, moving the cursor and editing are two things. Because they are distinguished, It is very convenient to position and edit the cursor, so it is very useful to move the cursor faster.
 
W (e) move the cursor to the next word B move the cursor to the previous word0Move the cursor to the beginning of the line ^ move the cursor to the beginning of the line$Move the cursor to the end of the line h move the cursor to the first line of the screen M move the cursor to the middle line of the screen l move the cursor to the end of the screen Gg move the cursor to the first line of the document G move the cursor to the end of the document c-F(Press ctrl and F together) this command is page downc-B (that is, press Ctrl and B together, and then the same) this command is page up ''this command is quite useful, it moves the cursor to the previous tag, such as using Gd,*After a word is found, enter this command and return to the position where it was last stayed. This command is quite good, so it moves the cursor to the last modified line. 'This command is quite powerful, it moves the cursor to the last modification point.
3. Copy, delete, and paste

In VI, y indicates copying, d Indicates deleting, and P indicates pasting. Copy and delete are combined with the cursor moving command. You can see the following examples.

 
YW indicates copying the content from the current cursor to the end of the word where the cursor is located DW indicates deleting the content from the current cursor to the end of the word where the cursor is located y0 indicates copying the content D0 from the current cursor to the beginning of the row where the cursor is located deletes content from the current cursor to the beginning of the row where the cursor is located. Y$Copy content from the current cursor to the end of the row where the cursor is located d$Indicates to delete the content from the current cursor to the end of the row where the cursor is located. yfa indicates to copy the content between the current cursor and the first a character after the cursor. dfa indicates to delete the first character from the current cursor to the end of the cursor. content between a characters

Specifically:

 
YY: copy the row where the cursor is located. DD: Delete the row where the cursor is located. D: Delete the content from the current cursor to the end of the row where the cursor is located.

The complex usage of copying, deleting, and pasting is related to registers. You can query them by yourself.

 

4. Numbers and commands

In VI, the combination of numbers and commands often indicates repeated execution of this command. If the command appears at the beginning of the extended mode, it indicates that the row number is located, for example:

5 fx: Search for the cursor. The cursor is 5th x characters 5 W (e). move the cursor to the next five words. 5 YY: copy the cursor below.5Line 5dd indicates deleting the cursor below5Line y2fa indicates copying the content from the current cursor to the second a character after the cursor:12, 24y indicates copying content between 12th rows and 24th rows:12, Y indicates copying the content between the 12th rows and the row where the cursor is located:, and 24y indicates copying the content between the row where the cursor is located and the 24th rows.

 

5. Enter characters quickly.

In Vi, you do not need to enter every character. There are many ways to quickly enter some characters. If you use Linux/Unix, you must have an experience, when you enter a command in the command line, the system will automatically complete the remaining characters by typing the first few characters and then pressing the tab, if there are multiple matches, it will be printed out. This is the famous command completion (in fact, the file name completion function is also available in Windows). VI has many string completion commands, which are very convenient.

C-P (c-N) in editing mode, enter a few characters and then enter this command, then VI starts to search for and complete the matching words starting with (bottom, if you enter this command repeatedly, this command will be found in allProgramC-X-L in editing mode, this command is used to quickly complete the entire line of content, but only matches the C-X-FIn edit mode, this command indicates filling in the file name. For example, if you enter:/usr/local/Tom and then enter this command, it will automatically match:/usr/local/tomcat/Abbr stands for a macro operation. You can use an abbreviation in the editing mode to replace another string. For example, if you write a Java file and often input systemoutprintln, this is very troublesome, therefore, we should use abbreviations to reduce the number of words that can be used to do this: Abbr SPRT systemoutprintln and then enter other non-alphanumeric characters After SPRT, it will be automatically extended to systemoutprintln

 

6 replace

Replacement is the strength of VI, because regular expressions can be used to match strings. Below are several examples:

: S/AA/BB/ G. Replace AA in all strings containing AA displayed in the row where the cursor is located with BB: S  // BB/ G: replace all AA that appear in the row where the cursor is located with BB, replace the word AA only:  % S/AA/BB/ G, replace AA in all strings containing AA in the document with BB:  000000 S/AA/BB/ G replaces AA in all strings containing aa that appear from row 12 to row 23 with BB:  S/^/# /Add # characters to the beginning of a row from 12 to 23 : % s = * $ =  Delete unnecessary spaces at the end of all rows: G /^ \ s * $/d delete all blank lines that do not contain characters (space does not contain) 
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.