VI command description

Source: Internet
Author: User
Document directory
  • 1.6.1 VI Working Mode
  • 1.6.2 editing mode
  • 1.6.3 Insert Mode
  • 1.6.4 command mode
  • 6. Restore Files
1.6.1 VI Working Mode

After the initial startup, VI enters the editing mode. When you type commands such as I, A, and O in the editing mode, you can enter the insertion mode. Type:
Enter the naming mode. In insert mode, any character other than ESC entered by the user will be considered as a character inserted into the editing buffer. Press ESC to switch from insert mode to edit mode.

1.6.2 editing mode 1. move the cursor
To modify the body content, you must first move the cursor to the specified position. The simplest way to move the cursor is to press the top, bottom, left,
Right arrow. In addition to this primitive method, you can also use the many character combination keys provided by VI to move the cursor in the body
To locate the specified row or column. For example:
K, J, H, and l functions are equivalent to the upper, lower, left, and right arrow keys.
CTRL + B Move up a page in the file (equivalent to the Pageup key)
CTRL + F move down a page in the file (equivalent to the Pagedown key)
H move the cursor to the top of the screen (highest)
NH moves the cursor to line N of the screen
2 h move the cursor to the second line of the screen
M move the cursor to the middle of the screen (middle)
L move the cursor down the screen (lowest)
NL moves the cursor to the nth line of the screen
3l move the cursor to the last 3rd rows of the screen
W shifts the cursor right in the specified row to the beginning of the next word.
E move the cursor right in the specified row to the end of a word
B. move the cursor left in the specified row to the beginning of the previous word.
0: 0. move the cursor left to the beginning of the line.
$ Move the cursor right to the end of the row
^ Move the cursor to the first non-empty character in the row
2. Replace and delete
After the light is calibrated to a specified position in the file, you can use other characters to replace the characters pointed to by the cursor, or delete one or more
Characters. For example:
RC uses C to replace the current character pointed to by the cursor
NRC uses C to replace the first n characters pointed to by the cursor
5rc replaces the first five characters pointed to by the cursor with C
X deletes the current character pointed to by the cursor
NX deletes the first n characters pointed to by the cursor
3X Delete the first 3 characters pointed to by the cursor
DW Delete the word on the right of the cursor
Ndw deletes n words on the right of the cursor
3 DWS Delete the three words on the right of the cursor
DB deletes words on the left of the cursor
NDB deletes n words on the left of the cursor
Delete 5 words on the left of the cursor in 5db
Dd deletes the row where the cursor is located and removes gaps
NDD deletes n rows of content and removes gaps
3 dd delete 3 rows and remove gaps
3. paste and copy
The content (such as characters, words, or rows) deleted from the body is not really lost, but is cut and copied to a memory buffer. Users can
Paste it to the specified position in the body. The command to complete this operation is:
P: lowercase letter P. paste the content of the buffer to the end of the cursor.
P capital letter P, paste the content of the buffer to the front of the cursor
If the buffer content is a character or word, it is directly pasted before or after the cursor; if the buffer content is the whole line of body, it is pasted in the current
The top or next line of the row where the cursor is located.
Note the uppercase and lowercase letters in the preceding two commands. The VI editor often provides a pair of similar functions with a large and lowercase letter (such as P and P ).
. Generally, lower-case commands operate after the cursor, and upper-case commands operate before the cursor.
Sometimes you need to copy a piece of text to a new location, while retaining the content of the original location. In this case, you should first copy the specified content (instead
Cut) to the memory buffer. The command to complete this operation is:
YY copies the current row to the memory buffer
Nyy copies n lines of content to the memory buffer.
Copy 5 lines of content to the memory buffer in 5yy
4. Search for strings
Like many advanced editors, VI provides powerful string search functions. To find the location where a specified word or phrase appears in the file, you can
Search directly with VI instead of manually. The search method is: Enter the character/, followed by the string to be searched, and then
Press enter. The editing program performs a forward search (toward the end of the file) and stops the cursor
Enter the N command to continue the search and find the next position of the string. Use characters? Replace/to reverse
Search (beginning with a file ). For example:
/Str1 forward search string str1
N continue searching to find the position where the str1 string will appear next time
? Str2 reverse search string str2
Regardless of the search direction, when the end or the beginning of the file is reached, the search will loop to the other end of the file and continue to execute.
5. Revocation and repetition
When editing a document, you can use the undo command to eliminate the consequences of an incorrect editing command. In addition, if you want
The cursor position of repeat the previously executed edit command. You can use the Repeat command.
U undo the result of the previous command
. Repeat the last command to modify the body.
6. Select text

Vi can enter a visual
In this mode, you can use the cursor to move the command to select text visually, and then perform other editing operations, such as Delete and copy. V character selected command v line selected command

# DEMO #

1.6.3 Insert Mode 1. Enter insert mode
After you locate the cursor correctly in edit mode, you can switch to insert mode with the following command:
I enter the body on the left of the cursor
A. Enter the text on the right of the cursor.
O Add a new line to the next row of the cursor
O Add a new line to the row where the cursor is located
I enter the body at the beginning of the row where the cursor is located
A. Enter the body at the end of the row where the cursor is located.
The preceding describes several simple methods to switch to the insert mode. There are also some commands that allow you to delete a segment before entering the insert mode
Text to replace the text. These commands include:
S. Replace the character pointed to by the cursor with the input body.
NS replaces n characters on the right of the cursor with the input body
CW replaces the word on the right of the cursor with the input body
NCW replaces n words on the right of the cursor with the input body
CB replaces the word on the left of the cursor with the input body
NCB replaces n words on the left of the cursor with the input body
CD replaces the row of the cursor with the input body
NCD replaces n rows under the cursor with the input body
C $ replace all characters starting from the cursor to the end of the line with the input body
C0 replaces all characters starting from the beginning of the line with the input body with the cursor
2. Exit the insert mode.
To exit the insert mode, Press ESC or Ctrl + [.

# DEMO #

1.6.4 command mode

In vi
In command mode, you can use complex commands. In edit mode, type ":". Then, the cursor jumps to the last line of the screen and a colon is displayed. The command mode is displayed. Command mode, also known as "last line mode", displays user input in the last line of the screen, press the Enter key, vi
Run the command.

1. Exit the command
In editing mode, you can use the ZZ command to exit the VI editing program. This command saves the changes made to the body and overwrites the original file. If you only need
Exit the editing program and do not want to save the edited content. You can run the following command:
: Q exit without modification
: Q! Discard all modifications and exit the editing program
2. line number and file
Each line in the editing contains its own line number. You can use the following command to move the cursor to the specified line:
: N move the cursor to line N
In command mode, you can specify the range of line numbers for command operations. A numeric value is used to specify an absolute line number. The character "." indicates the line number of the row where the cursor is located.
"$" Indicates the row number of the last line of the body. A simple expression, for example, ". + 5" indicates the row down the current row. For example:
: 345 move the cursor to line 345th
: 345 million file write 345th rows into the file File
: 3rd W file: Write lines 5th to file files
: 1,. W file writes row 1st to the current row into the file File
:., $ W file: Write the current row to the last row to the file File
:... + 5 W file: write 6 lines of content into the file from the current row
: 1, $ W file writes all content to the file, equivalent to the w file command
In command mode, you can read or write the body from a file. For example:
: W writes the edited content to the original file to save the intermediate editing result.
: WQ writes the edited content to the original file and exits the editing program (equivalent to the ZZ command)
: W file writes the edited content to the file, keeping the content of the original file unchanged.
: A, BW file writes the content from row A to row B to the file File
: R file reads the content of the file and inserts the content behind the row where the current cursor is located.
: E file: edit the new file to replace the original content.
: F file: Rename the current file to file
: F print the name and status of the current file, such as the number of lines of the file and the number of lines where the cursor is located.
3. string SEARCH
Returns a string that can be searched to reach the specified row. If you want to perform a forward search, place the string to be searched in two
/"; If you want reverse search, place the string in Two"?" . For example:
:/Str/forward search, move the cursor to the next row containing the string Str
:? Str? Reverse search: move the cursor to the previous row containing the string Str
:/Str/W file: Forward search, and write the first row containing the string STR to the file.
:/Str1/,/str2/W file: Forward search, and write the row containing str1 to the row containing str2
Input File
4. Text replacement
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/g functions are the same as above
From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search
String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.
5. Delete the body
In command mode, you can also delete content in the body. For example:
: D. Delete the row where the cursor is located.
: 3D delete 3 rows
:., $ D Delete the current row to the end of the body
:/Str1/,/str2/d delete all rows from string str1 to str2
6. Restore Files
VI will generate another temporary file when editing a file. The file name usually starts with. And ends with. SWP. VI in
When the file Exits normally, it is deleted. If the file unexpectedly exits without saving the latest modification content, you can use the recovery command:
: Recover recovery file
You can also use the-r option when starting VI.

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.