VI editor Learning

Source: Internet
Author: User
Tags add numbers

1) Go to VI

In the system prompts (such as $, #), Type VI <File Name>, vi can automatically help you load the file to be edited or open a new file (if the file does not exist or the file name is missing ). After entering VI, A Tilde sign will appear on the left side of the screen. If this symbol is at the beginning of a column, it indicates that this column is currently empty.

(2) Two modes

As mentioned above, there are two modes in VI: Command mode and input mode. In command mode, keys entered are processed as commands. For example, if a is input, VI inserts a character at the current position. In the input mode, VI treats the Input key as the inserted character. To switch from the command mode to the input mode, you only need to enter the corresponding Input commands (such as A and A). to switch from the input mode to the command mode, you need to enter the ESC key in the input mode, if you do not know the current mode, you can press [ESC] several more times. If the system beeps, it indicates that it is in the command mode.

Pay: Instructions for entering the input mode in command mode:

Add (append)

A: add data from the end of the cursor. The data after the cursor is moved to the backend with the new data.

A: add data at the bottom of the column where the cursor is located.

Insert)

I: insert data from the front of the cursor. The data after the cursor moves to the back as the new data is added.

I: insert data starting from the first non-blank character in the column where the cursor is located.

Start (open)

O: Add a column under the column where the cursor is located and enter the input mode.

O: Add a new column to the column where the cursor is located and enter the input mode.

(3) Exit vi

In command mode, type: Q,: Q !, : WQ or: X (Note: No.), the VI will be exited. Where: WQ and: X are stored and exited, while Q is directly exited. If the file has a new change, VI will prompt you to save the file and the Q command will also be invalid, in this case, you can use the W command to save the file and then use Q to exit, or use the WQ or X command to exit. If you do not want to save the changed file, you need to use: q! Command to exit VI without saving the file.

(4) Basic editing

Use the function keys on the keyboard, such as the direction keys, [insert], and [delete]. Now you can use VI to edit files. Of course, VI also provides many other functions to facilitate text processing.

What is editing? It is generally regarded as the addition, modification and deletion of text, and even the moving and copying of text blocks. This section describes how to delete and modify VI. (Note: In the original concept of VI, input and editing are two things. Editing is performed in command mode. You can use the command to move the cursor to locate the place to be edited before editing the command .)

Command for deleting and modifying files:

X: Delete the character of the cursor.

DD: Delete the column where the cursor is located.

R: Modify the character where the cursor is located and the character to be corrected after R.

R: Enter the retrieval and replacement status. The new text will overwrite the original text until you press [ESC] to return to the command mode.

S: Delete the cursor character and enter the input mode.

S: Delete the column where the cursor is located and enter the input mode.

Actually, there is no such trouble on PC! Both input and edit operations can be completed in input mode. For example, if you want to delete a character, simply press [delete. The insert and replace statuses can be directly switched using [insert], making it impossible to edit commands in any command mode. However, as mentioned above, these commands can be used on almost every terminal, rather than just on a PC.
In command mode, the basic commands for moving the cursor are H, J, K, and L. I think you should be able to guess that you only need to use the direction keys of the PC, and you can use them either in the command mode or in the input mode. Not easy.
Of course, the PC keyboard also has some shortcomings. The command u can be used to restore deleted text, while the command u can restore all changes in the column where the cursor is located. This is the same as the [undo] button function on some computers.
Iii. Attachment: VI detailed instruction table
(1) Basic editing commands:
Add (append)

A: add data from the end of the cursor. The data after the cursor is moved to the backend with the new data.

A: add data at the bottom of the column where the cursor is located.

Insert)

I: insert data from the front of the cursor. The data after the cursor moves to the back as the new data is added.

I: insert data starting from the first non-blank character in the column where the cursor is located.

Start (open)

O: Add a column under the column where the cursor is located and enter the input mode.

O: Add a new column to the column where the cursor is located and enter the input mode.

X: Delete the character of the cursor.

DD: Delete the column where the cursor is located.

R: Modify the character where the cursor is located and the character to be corrected after R.

R: Enter the retrieval and replacement status. The new text will overwrite the original text until you press [ESC] to return to the command mode.

S: Delete the cursor character and enter the input mode.

S: Delete the column where the cursor is located and enter the input mode.

(2) cursor movement instructions:
Because a lot of editing work is positioned by the cursor, VI provides a lot of ways to move the cursor.
Several simple tables to illustrate (these are of course commands in the command mode ):

Command description function key
0 move to the front of the column where the cursor is located [home]
$ Move to the last [end] of the column where the cursor is located
[CTRL] [d] half a page [Pagedown]
[CTRL] [f] to the next page
[CTRL] [u] up half page
[CTRL] [B] [Pageup]

Instructions
H move to the first column of the window
M moves to the middle column of the window
L move to the last column of the window
B Move to the first letter of the next word
W: What is the value of the transferred shard? Nbsp;
E. Move to the last letter of the next word.
^ Move the first non-blank character to the column where the cursor is located

Instructions
N-Minus signs move to the first non-blank character in the previous column plus a number can be specified to move to the N column above
N + plus signs move to the first non-blank character in the next column plus a number can be specified to move to the following n columns
Ng directly uses the number N plus the capital G to move to column N
Instructions
FX
Move to the right to x characters
Move FX to the left to X characters
TX to the right before X
TX moves to the left before X characters
; Used with F & T, repeat once
, Used in combination with F & T, repeat once in the opposite direction
/Move the string to the right to a place with a string
? Move string to the left to a place with a string
N combination /&? Use, repeat once
N combination /&? Used, repeated in the opposite direction

Instructions
N (
Move the left parenthesis to the beginning of the sentence. The sentence is preceded by a number and can be specified to move n sentences forward! .? Three symbols to define
N) You can move the brackets to the beginning of the next sentence and add numbers to move n sentences to the end! .? Three symbols to define
N {move the left arc to the beginning of a paragraph. The Section is defined by a blank column between paragraphs.
N} You can move n paragraphs forward, right arc, to the beginning of the next paragraph, and add a number to the beginning of the next paragraph. You can move n paragraphs backward, which is defined by the blank columns between paragraphs.

(3) More editing commands
These editing commands are very flexible and basically consist of commands and scopes. For example, what is the best practice in DW? The combination of nbsp; D and range W indicates that a word D (elete) W (ORD) is deleted ).
The command list is as follows:
D. Delete)
Y replication (Yank)
P placement (Put)
C Change)
The range can be:
E? BR> W cursor position to the first letter of the next word
B. Place the cursor on the first letter of the last word.
$ Move the cursor to the last letter in the column
0 cursor position to the first letter in the column
) Place the cursor to the first letter in the next sentence.
(The cursor is located at the first letter in the sentence.
} Move the cursor to the last letter of the paragraph
{Place the cursor in the first letter of the paragraph
To be honest, it is a little artistic to combine these commands to edit files. In any case, they provide more text editing capabilities. It is worth noting that the deletion and replication will put the content within the specified range in the temporary storage area, and then the instruction P can be used to paste it to other places, this is how VI processes copying and moving segments.
Some VI versions, such as Elvis for Linux, can greatly simplify this instruction. If you take a look at these editing commands, you will find that the problem is actually a bit complicated by the fixed range method. In fact, there are only four commands. Command V is very easy to use. As long as you press the V key, the position of the cursor will be reversed. Then, you can move the cursor to set the range, and then directly edit the command. For the entire column operation, VI also provides more convenient editing instructions. As mentioned above, the command DD for deleting the entire text column is one of them. CC can modify the entire text column, while YY is to copy the entire text column; command D deletes all text from the cursor until the end of the column.
(4) file operation instructions (Source: http://blog.sina.com.cn/s/blog_53da61b601016mhw.html thanks to the author to share)
File Operation commands start with:, which is a little different from editing commands.
: Q end editing (quit)
: Q! You must discard edited files without archiving them.
: W: Save the file (write) and add the file name to be archived.
: WQ indicates that the archive is removed.
ZZ functions are the same as: WQ.
: X is the same as: WQ

(5) copy and paste

1. Multiple rows Dd
Delete a row
NDD
Delete n rows starting with the current row
DW
Deletes a character starting with the current character.
Ndw
Delete n characters starting with the current character
D $, d
Delete a line of characters starting with the current character
D)
Delete to the beginning of the next sentence
D}
Delete to the beginning of the next section
D. Press enter.
Delete two rows

2. Copy multiple rows
Task: Copy data from 9th rows to 15th rows to 16th rows
Method 1(Strongly recommended): 9, 15 copy 16 or: 9, 15 Co 16
As a result, you can:
: 9, 15 move 16 or: 9, 15 M 16 move the text content from 9th rows to 15th rows to the end of 16th rows

Method 2:
Move cursor to end line, Ma
Move the cursor to the starting line and enter 'A'
Move the cursor to the row to be copied. Input P. Input uppercase P if the cursor is copied before the row.

Method 3:
Move the cursor to 9th rows SHIFT + V
Move the cursor to row 3 Ctrl + C.
Then the cursor is killed to line 3 P MySQL

Method 4:
Move the cursor to the starting line and enter Ma
Move the cursor to the end line and enter MB
Move cursor to paste row, input MC
Then input 'a, 'B, and co' C to change Co to m, which is cut.
To delete multiple rows, enter 'a, 'B' de

VI: Set smartindent
VI: set number or set nu

In vim, how to select and copy and delete all: Press ESC, And Then DG
Copy all: Press ESC and then ggyg

Select all highlight: Press ESC, and then ggvg or ggvg

How Vim interacts with the clipboard (copy Vim content)

You get used to copying and pasting various applications in windows, such as ultraedit, notepad, and eclipse, using Ctrl + C and CTRL + v, how Can I copy and paste the file between vim and other Windows applications?
Of course, you can choose to use the mouse in Vim, select a piece of text, right-click to copy it, and then press Ctrl + V to paste it in the application, but this is much less efficient.
A better way is to use "* Y" in VIM for copying, and then press Ctrl + V in the application.
From the application to VIM, CTRL + C is used to copy in the application, and VIM usesShift + insert Paste.

For example:
"* Copy a row in YY
"* Copy two words in y2w
......

The implementation principle is:
"Registers used
"*Indicates that the current selection area is used.

I personally recommend using Ctrl + insert copy and SHIFT + insert paste.

Vim has multiple clipboard, including the system clipboard. Use commands: RegYou can see the content of each clipboard. Where""Indicates the currently used clipboard,"0-9Is the historical clipboard,"#It is the system clipboard (you can copy something in the system to see if it will appear"#Clipboard ). Use in VIMYYou can copy the content""Clipboard number. If you continue with Y, new items will be placed in""And the original""Will be pushed"0-9. In the X11 system"*In Windows.

Solve the first problem:

"+ YCopy the selected content to the "+" Clipboard, that is, the system clipboard.

"+ PPaste the system clipboard content to VIM, which can also be completed by SHIFT + insert.

Solve the second problem:

"0 pYou can paste the content A that has been squeezed into the "0 Clipboard" and paste it again.

A long map can be easily mapped to a function key or combination.

VI editor Learning

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.