Vim usage and command Overview

Source: Internet
Author: User
VI usage and operations



Before introducing how to use VI, it is important to understand that VI has two basic modes. The two basic modes are input mode and instruction mode. In the input mode, the text is displayed on the screen. In the command mode, the command is executed when the input command is used, but the text is not displayed. The simplest way to understand the command mode is to imagine what happens when data is cut, copied, and pasted.

By default, VI is started in command mode, so no text is displayed when you enter the command, unless you enter the command to switch to the input mode. Most VI commands do not display anything on the screen. You only need to know how they work. You can use the [ESC] key to switch between the input and command modes. If you are in command mode, use [ESC] To tell VI that you have entered the command and will execute the command. If you do not enter any command, pressing the [ESC] key in command mode will interrupt the operation.

Now I have introduced the basic operations of vi. You can try it out. First, open a terminal window in your workstation. You only need to enter VI in the $ prompt and press the Enter key to start the VI editor.

Next, you will see a page in the lower left corner ~ Empty file of the symbol. This symbol is not easy to see, but what you see is ~ The symbol is at the bottom of the file. After the file is saved, these symbols are not displayed. To edit a file or name a blank file, enter VI filename. VI filename can be an existing file name or a name specified for the new file.
Common input mode



Once you enter the edit document, in addition to using the [ESC] switch method, there are many other ways to enable VI to enter the input mode. Many other methods use a VI command. These commands immediately enable VI to enter the input mode, and the entered characters will appear in the document immediately. Common commands are:

I is an input command, that is, to insert text from the front of the cursor, the text after the cursor moves with the new text. If it is a blank document, the text will be entered in the upper left corner of the document.

I. input mode at the beginning of the line command, that is, input text at the beginning of the current line. It is similar to inputting text after pressing the key in word.

  

A is a new command, that is, to add text after the cursor is located. The text after the cursor is moved backward with the new text.

 

A is an operation to add a command to a line, that is, to add text from the end of the line where the cursor is located. This is similar to pressing the key in word and then entering the text.

  

O this is a command to open a line, that is, add a line under the line where the cursor is located and enter the input mode.

   

O this is an open command above a command, that is, add a line above the line where the cursor is located and enter the input mode.

After editing, any of the preceding commands will be returned to the command mode.

Cursor movement command



Next, you need to learn the browsing commands in VI. You can use the following command to move data in the document in command mode:

H: Move a text to the left

J: Move a text down.

K: Move a text up

L: Move a text to the right

In the mouse and GUI environments, these mobile commands seem outdated, but they are widely used in all VI versions. In many cases, the optical label, top cursor, bottom cursor, left cursor, and right cursor all play a role, but the operator should include the termcap and terminfo attributes in these commands.

Which of the following mobile commands can be used to protect the system? Br/> [CTRL] f move down a page. This will move the cursor down a page in the text.

[CTRL] B moves one page backward. This will move the cursor one page backward in the text.

[CTRL] U move down half-page command. This will move the cursor down half a page in the text.

^ Start of the line command. The cursor moves to the beginning of the line.

$ The end of the line command. The cursor moves to the end of the row.

W moves to the beginning of the previous text.

B. Move it to the start of the next text.

E text terminal command.

G moves to the end of the file.
General functions

In a common text editor, all functions, such as deletion, modification, cut, copy, paste, and search/replace, can be found in VI. Many Commands can change the editing status to the input mode, and the [ESC] command can be used to terminate the operation. General editing Commands include:

X: run the delete character command to delete the character where the cursor is located.

DW: delete a text command, that is, delete the text at the cursor.

DD: deletes the row where the cursor is located.

D: Delete to the end of the row, that is, delete all text from the cursor position to the end of the row.

CW: Change the text command, that is, the text at all positions of the cursor will change with the input text, to press [ESC].

R: Replacement character command, that is, the character at the cursor position will be replaced by the entered character, to press [ESC].

R: replace the multi-line character command, that is, all the characters from the cursor position to the right will be replaced by the entered characters, to press [ESC].

Most of these commands can be combined to improve their functions.

For example, if you enter 2dd, VI will delete two lines of text.
Cut and paste operations

 

Like many other editors, you can cut and paste text in the document. The following command is the simplest operation to cut and paste data.

YY copy the line command. The text of all rows of the cursor will be copied to a buffer similar to the clipboard. Note that you can use multiple copy commands to change this command to cut multiple lines of text into a buffer.

YL copy character command. The characters of all rows of the cursor will be copied to a buffer similar to the clipboard. You can use multiple copy commands to change this command to cut multi-line characters into a buffer.

P and place the command. Place or paste the contents of the buffer after the current cursor position. For the Copy command line, the contents of the buffer will be pasted after the current line. For the copy character line command, the buffer content is pasted on the right of the cursor.

Place the command before P. Place or paste the contents of the buffer before the current cursor position. For the Copy command line, the contents of the buffer will be pasted before the current line. For the copy character line command, the buffer content is pasted on the left of the cursor.

When using the copy and place commands, you can also use multiple buffers or clipboard for character instructions. These characters can be a-Z and can only be lowercase letters. For example, if Ayy copies the content of the current row to buffer A, the AP command will paste the content of the buffer before the current row.

The most important command in VI is the undo command. This command is very simple: you only need to press u. Press the U key to undo the recently used command. The redo command is similar to the undo command. Press the period key (.) To re-type the most recent command. For example, if you use CW to change a text, you can search for the text and use periods.

The other two important commands are the SEARCH Command. In order to search forward in the document, you can type/text, text is the content you are looking. It is important to note that only when the input content exactly matches can you find it. VI does not support hybrid queries or fuzzy queries. For Backward Search, type? Text is the content to be searched.

  

Edit command

In command mode, many group commands can be prefixed :. Unlike the commands mentioned above, once a colon is entered, all commands appear in the lower left corner of the screen and can be edited before execution. The following are commonly used important commands. : W: Write File commands. If you enter W and press enter, the current file will be written to the hard disk. If a blank document is used, the message "no current file name" is displayed. You can use the W file name to write a blank or current file to another file.

: W! Or: W file name overwrite FILE command. If you enter: W! Press enter to overwrite the file with the new name. This can be used to overwrite files.

: Q exit command. If it remains normal, enter Q to terminate the entire operation.

: Q! Exit Without saving the command. Exit Without saving the file.

: % S/find_text/replace_text/
Query and substitution commands

 

As mentioned above, the query command can be changed to a query and replacement command. Run the: % s command to notify VI to query all lines in the document. The/find_text command is similar to the preceding query command. When its suffix is/replace_text/, the first line of the/find_text operation result will be replaced. To replace all/find_text content, you can add a G (global), such as % S/find_text/replace_text/g.

You should not just use a single command. You can use multiple commands in combination. For example, WQ! Save the file to the hard disk, exit VI, and return to the command status. Vi = contains only a small part of the VI function mentioned above (very involved. You can run the command string together, copy and paste the text as a tag, or use the SED command. You can also insert shell script output or other commands into your documents. The examples and instructions mentioned above can bring you to the VI gate. Many VI users remember some commands during operations. You can learn more VI commands from other users, and you may be amazed at the power of VI. However, you should remember that the command is often used because vi
The automatic background document saving function is not provided.

Details are as follows:

* *** Enter the VI command ****

**********

VI filename: open or create a file and place the cursor at the beginning of the first line

Vi + N filename: open the file and place the cursor at the beginning of line N

Vi + filename: open the file and place the cursor at the beginning of the last line

Vi +/pattern filename: open the file and place the cursor at the first string matching pattern

VI-r filename: a system crash occurred when VI was being edited last time, restoring filename

VI filename... filename: open multiple files and edit them in sequence

**********

* *** Move cursor command ****

**********

H: move the cursor one character to the left

L: move the cursor one character to the right

Space: move the cursor one character to the right

Backspace: move the cursor one character to the left

K or Ctrl + P: move the cursor up a row

J or Ctrl + N: move the cursor down a row

Enter: move the cursor down a row

W or W: move one word to the beginning of the word to the right of the cursor.

B or B: move one word left to the beginning of the word

E or E: move one word to the end of the word to the right of the cursor.

): Move the cursor to the end of the sentence.

(: Move the cursor to the beginning of the sentence.

}: Move the cursor to the beginning of the paragraph

{: Move the cursor to the end of the paragraph

NG: move the cursor to the beginning of line N

N +: move the cursor down n rows

N-: Move n rows above the cursor

N $: move the cursor to the end of line N

H: move the cursor to the top line of the screen

M: move the cursor to the middle line of the screen

L: move the cursor to the last line of the screen

0: (Note the number is zero) move the cursor to the current row

$: Move the cursor to the end of the current row

**********

* *** Screen tumble command ****

**********

CTRL + u: half screen at the beginning of the file

CTRL + D: Flip the half screen to the end of the file

CTRL + F: Flip the screen to the end of the file

CTRL + B; open a screen at the beginning of the file

NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.

Insert text commands

I: Before the cursor

I: at the beginning of the current row

A: After the cursor

A: At the end of the current row

O: open a new row under the current row

O: open a new row above the current row.

R: Replace the current character

R: Replace the current and subsequent characters until you Press ESC.

S: Starting from the current cursor position, replace the specified number of characters with the input text

S: delete a specified number of rows and replace them with the input text

NCW or NCW: modify a specified number of characters

NCC: modifies a specified number of rows.

**********

* *** DELETE command ****

**********

Ndw or ndw: n-1 characters starting at and following the cursor

Do: Delete to the beginning of a row

D $: Delete to the end of the row

NDD: Delete the current row and the next n-1 row

X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.

CTRL + u: delete text entered in input mode

**********

***** Search and life replacement ****

**********

/Pattern: Search for pattern from the beginning of the cursor to the end of the file

? Pattern: Search for pattern from the beginning of the cursor

N: Repeat the previous search command in the same direction.

N: Repeat the previous search command in the reverse direction.

: S/P1/P2/G: replace all p1 in the current row with P2.

: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.

: G/P1/S // P2/G: replace all P1 files with P2.

**********

***** Option setting ****

**********

ALL: lists all options.

Term: Set the terminal type

Ignorance: Case Insensitive in search

List: displays the stop table (CTRL + I) and end mark ($)

Number: displays the row number.

Report: displays the number of changes made by line-oriented commands.

Terse: displays brief warning information

Warn: When transferring to another file, if the current file is not saved, no write information is displayed.

Nomagic: allows you to use special characters without "\" in search mode.

Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.

Mesg: Allows VI to display the information that other users write to their terminal using write.

**********

* *** Last-line command ********

**********

: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.

: N1, N2 M N3: Move the content from line N1 to line N2 to line N3

: N1, N2 D: Delete content from line N1 to line N2

: W: Save the current file

: E filename: open the file filename and edit it.

: X: Save the current file and exit.

: Q: Exit VI.

: Q! : Do not save the file and exit vi

:! Command: execute shell command

: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed. If n is not specified

1, N2, indicates that the entire file content is used as the command input.

: R! Command: place the output result of the command to the current line.

**********

* ** Register operation ****

**********

"? Nyy: Save the content of the current row and Its n rows to the register? , Where? It is a letter, and N is a number.

"? Nyw: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.

"? Nyl: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.

"? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number.

NDD: delete n lines of text from the current row and its bottom, and put the deleted content in the delete Register No. 1.
Add to favorites

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.