VI Editor
I. UNIX editor Overview
Editor is an important tool for using computers. In various operating systems, editors are essential components. UNIX and similar IX
The operating system series provides a series of ex editors, including ex, edit, Ed, and
Vi. Ex, edit, and Ed are all row editors, which are rarely used now. The reason UNIX provides them is that they meet the needs of various users.
Is the need of some end users.
Fortunately, Unix provides a full-screen VI Editor, which makes our work much easier. Many dos users complain that the VI editor is not like
The editor in DOS is as easy to use as edit, because VI does not use some general editing keys (
Different terminals have different definitions, and some terminals do not even have these keys ). However, the status switch method is used.
It's just a habit. Once you use VI skillfully, you will think it is actually very useful.
Although VI adopts the status switch method, the hardware and operating system of the computer are diverse. Some computers do not have the specific keyboard? Parent role?
Key! Isn't some functions unavailable? This problem is also the same in Unix systems. Almost all major computer manufacturers have their own
UNIX system, and the VI operation method will also be a little different. Here we use the PC keyboard to describe VI operations, but in the specific
It is worth noting that you should also refer to the relevant materials in the environment.
II. Introduction to VI
(1) Go to VI
When you enter VI <File Name> under the system prompt characters (such as $, #), 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 of the screen. Any column with this symbol at the beginning will be replaced
This column in the table 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
A and VI are considered to be characters inserted at the current position. In the input mode, VI treats the Input key as the inserted character. Command
To switch mode to input mode, you only need to enter the corresponding Input commands (such as A and A). to switch from input mode to command mode, you must
Enter the ESC key in input mode. If you do not know the current mode, you can press [ESC] several times. If the system generates a beep
The command 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 Q command will also become invalid. In this case, you can use W command to save the file and then use Q
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, this command will not save the file
Directly exit VI.
(4) Basic editing
Works with function keys on the keyboard, such as 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. Here we will introduce vi
How to delete and modify. (Note: In the original concept of VI, input and editing are two things. Editing is performed in command mode
, First use the command to move the cursor to locate the place to be edited, and then edit 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, to delete a character, directly Press
[Delete. However, the insert and replace statuses can be directly switched using [insert], making it difficult to compile the command mode.
Command. 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 PC directly.
In command mode or input mode. Not easy.
Of course, the PC keyboard also has some shortcomings. There is a good command u to restore the deleted text, while the U command can restore the cursor
All changes in the column. 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] What is next? [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 move to the first letter of the previous word ??
E. Move to the last letter of the next word.
^ Move the first non-blank character to the column where the cursor is located
Instruction description ???
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 to move to the front of X characters ???
TX moves to the left before X characters
; Used with F & T, repeated ??
,?? Used 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? Cooperation /&? Use, repeat once
N? Cooperation /&? Used, repeated in the opposite direction
Instructions? Remarks
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 a number 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, DW is composed of the DELETE command D and the range W.
To delete a word D (elete) W (ORD ).
The command list is as follows:
D. Delete)
Y replication (Yank)
P placement (Put)
C Change)
The range can be:
E. move the cursor to the last letter of the word.
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. Worth
Note that the deletion and replication will put the content within the specified range in the temporary storage area, and then you can use the command P to paste it to other places.
The VI method is used to process the copy and movement of segments.
Some VI versions, such as Elvis for Linux, can greatly simplify this instruction. If you take a look at these edit commands
We will find that the problem is actually a bit complicated in a fixed range. In fact, there are only four commands. Command V is very useful, as long as you press the V key,
The position where the cursor is located is reversed, and then you can move the cursor to set the range, and then directly run the command to edit it.
For the entire column operation, VI also provides more convenient editing instructions. I mentioned earlier that delete? Which of the following values is false? Dd is one of them.
CC can modify the text of the entire column, while YY can copy the text of the entire column. Command D can delete all the text from the cursor until the end of the column.
(4) file operation commands
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