How to Use the VI editor in Unix/Linux

Source: Internet
Author: User

 

I first enters the editing mode after the initial startup. In this case, you can use predefined buttons to move the cursor, delete text, copy or paste text. These buttons are common characters. For example, if l is to move the cursor to the right, K is to move the cursor down, and K is to move the cursor down. In editing mode, you can also use some special buttons to select text and then delete or copy the text.

 

After you enter commands such as I, A, and O in editing mode, you can enter the insert 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.

 

In command mode, VI moves the cursor to the bottom of the screen and displays a colon at the first character ). In this case, you can enter some commands. These commands can be used to save files, Read File Content, execute shell commands, set VI parameters, find strings or replace strings in a regular expression.

 

Edit 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 mode. In this mode, you can use the cursor to move the command to select text visually and then perform other editing operations, such as deleting and copying. V character selected command v line selected command

 

Insert mode

1. Enter the 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 + [.

1.6.4 command mode

In the command mode of Vi, 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 to execute 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.

The symbol "___ fckpd ___ 8 rdquo; indicates the row number of the last row of the body. A simple expression, such as". + 5 ", indicates the second 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.

Option settings

 

To control different editing functions, VI provides many internal options. Use the SET command to set options. The basic syntax is:

: Set option set Option

Common features include:

Autoindent: sets this option, and the body is automatically indented.

If this option is set for ignorecase, the differences between upper and lower case letters in the Rule expression are ignored.

Number.

Ruler sets this option to display the position of the row and column of the cursor at the bottom of the screen.

Tabstop sets the number of spaces skipped by pressing the tab key. For example, set tabstop = n. The default value of N is 8.

MK saves the options in the. exrc file of the current directory.

Shell Switch

When editing the body, use the shell switch Command provided in VI command mode to execute the Linux Command without exiting VI.

Convenience. Syntax format:

:! After executing the shell command, return to VI

In edit mode, Enter K to run the VI command to find the manual page of the word where the cursor is located. This is equivalent to running the man command.

Advanced features of vim and gvim

Vim stands for VI improved. As its name implies, VIM exists as an upgraded version of the standard UNIX system VI editor. Besides providing the same powerful functions as the VI Editor, VIM also provides multi-level recovery, command line history, command and file name completion functions.

 

Gvim is the X Window version of vi. It supports mouse selection and some advanced cursor movement functions with menus and tool buttons.

 

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.