Summary of common linux vi (vim) commands

Source: Internet
Author: User
Tags alphanumeric characters

The Vim VI is a widely used text editor in Unix/Linux. Most machines have different types of VI,
We often use different variants of software on different machines. Among them, VIM is relatively easy to use and widely used. Vim is short for VI improved, indicating better VI.
This article describes some useful and commonly used commands. If you are familiar with these commands, you will find it very helpful to edit files, especially for developers, can quickly improve development efficiency

Note:
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 ,? Indicates
In the above search, xxx can be a regular expression, so I won't say much about the regular expression.
Generally, it is case sensitive. To be case insensitive, you must enter
: Set ignorecase
After finding it, 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 matching words.
Next (top) a word is the same, then enter n to find the next match, and enter n Inverse
Forward to Lookup

G * (G #) this command is similar to the previous command, except that it does not fully match the word at the cursor.
Is to match all strings containing the word

GD this command looks for words that match the word where the cursor is located, and points the cursor to the non-
Place where the word appears for the first time in the Comment Segment

% This command looks for the parentheses matching the cursor location, including () [] {}

F (f) X: This command searches for the first X character on the right (left) side of the cursor in the row where the cursor is located.
After finding:
Input; indicates to continue searching
Input, indicating Reverse Lookup

2. move the cursor quickly
In VI, moving the cursor and editing are two things. Because they are distinguished, you can easily perform optical calibration.
It is very useful to move the cursor a little faster because of its location and editing.

W (e) move the cursor to the next word
B. move the cursor to the previous word.

0 move the cursor to the beginning of the line
^ Move the cursor to the starting character of the line
$ Move the cursor to the end of the row

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 down
C-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, for example, using Gd, *, and so on.
After a word is found, enter this command to return to the position where it was last stayed.

'The command is quite good, it moves the cursor to the last modified line

'The 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 the copy and delete commands with the cursor moving
In combination, you can see several examples.

YW indicates copying the content from the current cursor to the end of the word where the cursor is located
DW: deletes the content from the current cursor to the end of the word where the cursor is located.
Y0 indicates copying the content from the current cursor to the beginning of the row where the cursor is located
D0 indicates deleting the content from the current cursor to the beginning of the row where the cursor is located
Y $ indicates copying the content from the current cursor to the end of the row where the cursor is located
D $ deletes the content from the current cursor to the end of the row where the cursor is located.
Yfa indicates copying the content from the current cursor to the first a character after the cursor
DFA indicates removing the content from the current cursor to the first a character after the cursor

Specifically:
YY indicates copying the row where the cursor is located
Dd indicates deleting the row where the cursor is located.
D: deletes 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 it appears at the beginning of the extended mode, it indicates the line
As shown in the following figure:

5fx indicates 5th x characters after the cursor search

5 w (e) move the cursor to the next five words

5yy indicates copying 5 rows below the cursor
5dd indicates deleting 5 rows below the cursor

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 content between the 12th rows and the row where the cursor is located
:, 24y indicates copying the content between the row where the cursor is located and deleting the content between the row 24th is similar

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 are using linux/unix, you must have an experience. when you enter a command on the command line, enter the first few characters and then press
The TAB system will automatically complete the remaining characters. If there are multiple matches, it will be printed out. This is the famous command.
Complement (in fact, file name completion 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 searching up (down)
Enter the matching words at the beginning of the cable and fill them in again. Enter this command repeatedly to search for this command.
Will match all files opened in this vim Program

C-x-l in editing mode, this command is used to quickly complete the contents of the line, but only
Matching in the document

In c-x-f editing mode, this command indicates filling up the file name, as shown in the following:
/Usr/local/tom:
/Usr/local/tomcat/

Abbr stands for a macro operation. You can use one abbreviation to replace another in editing mode.
For example, if systemoutprintln is often input to write Java files
It is troublesome, so you should use abbreviations to reduce the number of words you can do this:
: Abbr SPRT systemoutprintln
After entering SPRT and other non-alphanumeric characters, it will be automatically extended to System
Outprintln

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

 

From: http://hi.baidu.com/s_jqzhang/blog/item/39d1b522840971fbd7cae2f0.html

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.