Screen Text Editor VI This chapter describes the most common text editor VI on Linux. Text Editor is the most commonly used tool in all computer systems. When users use a computer, they often need to build their own files, whether they are general text files, data files, or source program files, which are inseparable from editors. The Linux system provides a complete editor family series, such as ED, EX, VI, and Emacs, etc., which can be divided into two categories: line editor (ED, EX) and full screen editor (vi, emacs) . Each editor can only operate a line each time, it is very inconvenient to use. The full screen editor edits the entire screen, the user edited files directly displayed on the screen, the modified results can be seen immediately, overcome the non-intuitive way of operation of the line, easy to learn and use, powerful functions. VI is the first full-screen interactive editor of the Linux system. It has been favored by the majority of users since the birth. After decades, it is still a text editing tool that is mainly used by people, and it is strong, and powerful vitality. It is a powerful function. In this chapter, we will gradually introduce how to build, edit, display, and handle files using VI. (Note: Combination of uppercase letters [Shift] button corresponding lowercase letters) VI Introduction VI is the abbreviation of "Visual Interface", which is the same as the EDIT program on DOS on Linux. It can perform numerous text operations such as output, delete, lookup, replace, block operation, and users can customize them according to their needs, which is not available in other editor. VI is not a typographic program, which can be arranged such as Word or WPS to arrange other properties such as fonts, formats, paragraphs, which is just a text editing program. VI has no menu, only commands, and there are many commands. VI has three basic working modes: command line mode, text input mode, and end mode. At any time, if the user is in the mode, simply press the key, you can enter the command line mode; we entered the SHELL environment (prompt $) Enter the launch vi command, when entering the editor, it is also This mode is in this mode. In this mode, users can enter a variety of legitimate VI commands for managing their own documents. At this point, any characters entered from the keyboard are explained as the editing command. If the input character is a legal VI command, the VI completes the corresponding action after accepting the user command. However, it is important to note that the input command is not displayed on the screen. If the input character is not the legal command of the VI, the VI will ring the alarm. Text Input Mode Enter the insert command I, add command A, open command O, modify command C, replace command R or replacement command S can enter text input mode. In this mode, any character entered by the user is saved as a file content as the file content and displays it on the screen. In the text input, if you want to return to the command mode, press the key. The end mode terminal mode is also known as the EX escape mode. The functionality of the VI and EX editors is the same, and the two main differences are the user interface. In VI, the command is usually a single key, such as I, A, O, etc., in EX, the command is the end of the key to press the Enter key. VI has a dedicated "escape" command to access a lot of row EX commands. In command mode, the user presses the ":" key to enter the last line mode, at which time the VI will display a ":" as a prompt of the last line of the display window (usually the last line of the screen), Wait for the user to enter the command. Most file management commands are performed in this mode (such as writing the contents of the edit buffer to the file).
After the end command is executed, the VI is automatically returned to the command mode. For example: 1, $ S / A / A / G will replace all uppercase A to lowercase A from the first line of the file to the file. If you change your mind during the end mode, you can change your mind, or press all the commands to remove the input command, then press the back to command mode. The conversion between the three working modes of the VI editor is shown in Figure 11-1. If you want to convert from the command mode to the edit mode, you can type command a or i; if you need to return from the text mode, press the ESC key. Enter ":" in command mode to switch to the last line mode, then enter the command. Chapter 25 The entry of VI is logged in to the system after entering the user from 2000 / June / 13, and the system gives the prompt "$". Type VI after the prompt and you want to edit (or establish) file names, you can enter the VI. [Example 1] Type the command: $ vi eXample.c screen is shown below: ~~~~~~~~~~~~~ "example.c" [new file] If you only type VI, without file name, You can enter the VI. After that, type the file content at the cursor, when you exit the VI, you only need to enter the file name after exiting the command. After entering the VI, the first entry is the command mode, that is, waiting for the command input instead of text input. The alphabet entered at this time will be interpreted as a command. The cursor stops on the first line of the screen (with the representation), and the remaining rows have a "~" symbol, indicating that the behavior is blank. The last row also called the status line, showing the file name and its status currently being edited. If this example is [new file], eXample.c is a newly built file. If the example.c file has exist in the system, then after the above command is input, the contents of the file are displayed on the screen, and the cursor stops in the first line of the first line, and the file name of the file is displayed in the status line. Number and characters. [Example 2] Type command: $ VI EXAMPLE.C and type: #includemain () {INT K; for (k = 0; k <3; k ) add () add ();} add () {static int x = 0; X ; Printf ("x =% d / n", x);} ~~~~ "example.c" [new file] Note that we only show 15 lines in order to save space. In fact, the initial display line is related to the terminal used by the user, and the general CRT terminal can display 25 rows. In the window system, the number of lines is displayed is related to the window running VI. However, we can set the number of display lines. For example, on the CRT terminal that can display 25 rows, allows VI to display only 15 rows. The method of setting will be described later in this section. When a new file is created with VI, you may not give the file name in the command that enters the VI. When editing the file, you need to save the data, then specify the file name. When entering VI, users can specify a file name to be edited, but also have many additional operations. If it is desired to enter the VI, the cursor is on a certain line in the file, and the option n can be added after the VI, where n is the specified number of rows.
[Example 3] After the command: $ VI 5 example1.c, the screen is displayed as follows: #includemain () {INT K; for (k = 0; k <3; k ) add ();} add () {static INT x = 0; x ; Printf ("x =% D / N", x);} ~~~~~ "Example.c" 12 Lines, 125 Characters cursor will be located on the 5th line in the file eXample1.c . If the cursor is in the final line after entering the VI, simply omit the numbers n after the appearance " " in the command. When entering the VI, in addition to the cursor starting line number, you can specify a mode string in the command. At this time, the cursor is in the file in the file, which matches the specified mode string. On line. [Example 4] Type command: $ VI / INT EXAMPLE1.C screen displays as follows: #includemain () {INT K; for (k = 0; k <3; k ) add ();} add () {static int int X = 0; x ; Printf ("x =% D / N", x);} ~~~~~ "Example.c" 12 Lines, 125 Characters cursor will be on page 3 in the file eXample1.c. Using VI can edit multiple files at the same time, as long as you write the file to be operated in the command to enter the VI, you can use wildcard. [Example 5] Type command: $ vi * .cat You can edit all the suffixs for CAT. Of course, you can also load files with built-in pattern in the shell, which will be better. Once you contain all the edited file names in the command line, you can remove it in VI. Exit VI When editing the file, you can use one of the following methods when you prepare to exit the VI to return to the shell. In command mode, connect to the uppercase letter z, if the current edited file has been modified, then the VI saves the file and returns to shell; if the current edited file is not modified, VI directly exits, Return to the shell. In the end mode, enter a command: WVI saves the current editing file, but does not quit, but continues to wait for the user to enter the command. When using the W command, you can give a new file name to the editing file. [Example 6]: w NewFile At this time, VI will save the contents of the current file to the specified newfile, and the original file remains unchanged. If newfile is an existing file, the VI gives the prompt information in the status line of the display window: File Exists (use! To override) At this time, if the user really wants to replace the current content of the file in Newfile Central Plains, Use the command: w! Newfile otherwise you can select another file name to save the current file. In the end mode, enter a command: Q System exits VI to return to the shell.
When you exit the VI with this command, the editing file is not saved, then the Live line of the VI is displayed as follows: No Write Since Last Change prompts that the file is not saved after the file is modified, then VI does not quit and continue to wait for the user command. If the user does not want to save the modified file, you can use the command: Q! VI to give up the changes directly to the shell. In the end mode, enter the command: WQVI will save the file first, then exit the VI to return to the shell. In the last line mode, enter a command: x The function of the command is the same as the zz command function in command mode.
Many of the commands in the line number VI in the VI are used to use the number of line numbers and rows. If the edited file is large, you can go to the number of yourself is very inconvenient. To this end, VI provides a function of adding a trine number to a text. These line numbers are displayed on the left of the screen, while the contents of the corresponding row are displayed behind the line number. The command used: Enter a command in the last mode :: Set Number What is needed, the line number added here is only displayed to the user, which is not part of the file content. In a large file, the user may need to know which row is the current line of the cursor, where is in the file, can use the key combination
J,
Main ()
{INT K;
FOR (k = 0; k <3; k ) add ();
Add () {static int x = 0;
X ;
Printf ("x =% d / n", x);
}
~
~
~
~
~
"EXample.c" [new file]
After entering the 4h command in command mode, the cursor is moved to the letter F of this line starting with FOR.
It is worth mentioning that the use of the command DH will delete all the contents of the first line of the screen displayed from the cursor.
2. m Command
This command moves the cursor to the head of the middle line of the screen display file. That is, if the current screen is full, move to the middle line of the entire screen; if it is not full, move to the middle line of the lines of the text. With this command, you can quickly move the cursor from any position of the screen to the middle line of the screen display file. For example, in the case of the above screen display (regardless of where the cursor is on the screen), after the command mode is input, the cursor will move to the letter A of the ADD.
It is also worth mentioning that the use of the command DM will delete the entire content of the middle line from the cursor current to the screen display file.
3. L command
When the file display exceeds a screen, the command moves the cursor to the bottom of the bottom of the screen; when the file display is less than one-screen, the command moves the cursor to the last line of the file. It can be seen that this command can quickly accurately and accurately move the cursor to the last line of the bottom or file. If you add a digital N before the L command, the cursor moves to the first line of the Nth line from the bottom of the screen. For example, in the case of the above screen display (regardless of where the cursor is on the screen), after the command 3L is input, the cursor will move to the letter X of X this line.
It is also worth mentioning that the use of the command DL will delete all the contents from the cursor current to the bottom line.
Move cursor by word
First introduce the concept of "word" in VI. There are two meanings in "Word" in VI. One is a broad word, which can be anything between two spaces.
[Example 10] The 4th line in the file eXample.c:
{INT K;
It only has 2 words, one is {Int, the other is K;
The other word in VI is a narrow word, in this sense, English words, punctuation symbols and non-alphabetic characters (such as!, @, @, $,%, ^, &, *, (,), -, , {,}, [,], ~, |, /, <,>, /, etc.) are all being used as a word. Therefore, the above line includes {, int, k, etc .; 4 words.
Using uppercase commands in VI is generally referring to the word as a broad sense, using lowercase commands is treated as a narrow.
After engage in the meaning of the word in the vi, we can introduce the command to move the cursor in word.
VI provides a total of three sets of commands on the word movement cursor, namely:
1. w and w command
Move the cursor to the header of the next word;
[Example 11] The screen is displayed as follows:
Printf ("Hello Mr.huang! / N");
Now use the W command, move the cursor to the next word (narrow "word" ("on:
Printf ("Hello Mr.huang! / N");
The following W command will be used to move the cursor to the next word (broad) word "m":
Printf ("Hello Mr.huang! / N");
2. E and E command
If the cursor starting position is in the word (ie, the non-word tail), the command will move the cursor to this character; if the cursor start position is at the tail, the command will move the cursor to the next word. suffix. 3. B and B
If the cursor is in the word (ie, the first), the command will move the cursor to this word; if the cursor is in the word first, the command will move the cursor to the first word.
[Example 12] The screen is displayed as follows:
Printf ("Hello Mr.huang! / N");
Now use the b command, since the cursor is in the middle of this word, the cursor moves to the first word "H":
Printf ("Hello Mr.huang! / N");
If you use a b command, you are as follows:
Printf ("Hello Mr.huang! / N");
Move the cursor in the sentence
In VI, a sentence is defined as a comma (,), the number (.), The question mark (?) And the exclamation mark (!), And it follows at least two (including two) spaces or a newline character Character sequence.
The VI provides two commands on the operation of the cursor, respectively:
Command
Move the cursor to the beginning of the previous sentence.
2.) Command
This command moves the cursor to the beginning of the next sentence.
Move cursor by segment
In VI, a segment is defined as a segment that starts and ends with a blank row. VI provides two commands on the movement cursor in segment, respectively:
1. {command
This command moves the cursor forward to the beginning of the previous paragraph;
2.} command
This command moves the cursor to the beginning of the next paragraph.
Screen scroll
The screen command is a screen and paging that moves cursors in units of screens. It should be noted that the screen command is not a cursor motion command, and cannot be used as a text qualifier to delete the command.
You can use the screen scroll command in command mode and text input mode.
Schep command
There are two on the scroll command:
·
·
You can add a number N before these two commands, then the screen is forward or backward. And this value is remembered, and the corresponding number of rows is also rolled by using the
2. Package command
There are two paging commands:
·
·
You can also add a number n before these two commands, then move n pages forward or backward.
3. Status Command
The command displays the VI status information on the VI status line, including the file name being edited, whether it modifies, the current line number, the number of files, and the percentage of the entire file before the cursor.
4. Screen zero command
VI provides three commands related to screen zero. Their formats are:
· [Number] Z [Row number]
· [Number] z [line number].
· [Line Number] Z [Route] _ If the line number and the number of rows are omitted, these three commands are redisplayed to the current line where the cursor is located, the middle row, and the last line are redisplayed; if it is given No., the line corresponding to the line number is displayed as the first line of the screen, the middle row, and the last line; if the number of times is given, it specifies the number of rows displayed on the screen.
[Example 13]
8Z16
15Z.: The middle line in the file is displayed as the middle line displayed by the screen, and the number of lines is displayed.
15Z 5_: Take the 15th line in the file as the last line displayed, the number of lines is 5 lines.
Text insertion operation
Any character of the user input in command mode is explained as a command as a command. If the user wants to use the character as a text content, first, the working mode of the VI should be switched from the command mode to the text input mode. The way to switch is to use the following command.
Insert (insert) command
VI provides two insert commands: I and I.
I command
Inserting text begins before positioning from the cursor, and can be used during the insertion
Key Delete the incorrect input. At this time, the VI is inserted, and the screen is displayed at the bottom of the screen "--ensert -" (insert).
[Example 14] A file being edited as follows:
Welcome to vi world! Come ON!
~
~
The cursor is located in the first "!", You need to insert it in front:
This is an esample!
Use the i command and enter the corresponding text, the screen is displayed as follows:
Welcome to vi world this is an esample !! come on!
~
~
As can be seen from this example, the cursor is originally in the first "!", But since it starts to insert from the position where the cursor is located, this "!" Is squeezed to the newly inserted text.
2. i command
This command is to move the cursor to the head of the current line, then insert the text before it.
}
Additional (APPEND) command
VI provides two additional insert commands: A and A.
A command
This command is used to add new text after the cursor is currently located. After the newly input text is placed behind the cursor, the original text after the cursor will move backwards. The cursor can be in any position in one line.
[Example 15] Taking Example 14 as an example, use a command, and enter the corresponding text, the screen is displayed as follows:
Welcome to vi world! This is an example! Come on!
~
~
The text "Come ON!" After the cursor in this example is squeezed behind the newly entered text.
2. a command
The command is different from the A command, the A command will move the cursor to the line of the line, starting to insert a new text. When the A command is input, the cursor is automatically moved to the row of the row.
The A and A commands are the only way to insert text into the end of the line.
Open (open) command
Whether it is the insert command, or the append command is also, the inserted content begins with a location in the current row. If we want to insert some new rows before or after a row, you should use the Open command.
VI provides two open commands: O and O.
1. o command
This command will open a row under the row of the cursor and place the cursor in the line of the row, waiting to enter text. Pay attention to when using
When you delete characters, you can only delete characters from the starting position from the insert mode, which does not work for previous characters. And you can also enter some control characters in a text input mode, for example, Ctrl L is inserted into a paging, and is displayed as ^ L. [Example 16] As an example, the original case of Example 14 is exemplified, but this time it is necessary to enter text in the current row. Use an O command, and enter the corresponding text, the screen is as follows:
Welcome to vi world! Come ON!
This is an esample!
~
~
The newly entered text appears next to the next line of the original text.
2. o Command
In contrast to the O command, the O command is inserted in the top of the cursor, and places the cursor in the line of the line, waiting for input text.
[Example 17] That is the same as the original situation of Example 14, you must enter text on the top of the current row. After using the O command and enter the text, the screen is displayed as follows:
This is an esample!
Welcome to vi world! Come ON!
~
~
The text is inserted into the current row.
11.3.4 Text Modification
In command mode, you can modify the text using the various commands provided by VI, including deletion, replication, replacement, and replacement of text content.
Text delete
When editing the text, you often need to delete some unwanted text, we can use
The keys delete the error or unwanted text, but there is a limit that is not possible after deleting a line, it is impossible to delete the upper surface.
In command mode, VI provides a lot of delete commands. Most of these orders are at the beginning of D. Commonly used:
1. Delete a single character
X: Delete the character at the cursor. If you add a number N before X, remove the n characters from the position where the cursor is located.
X: Delete the character in front of the cursor. If you add a number N before X, remove the n characters from the front of the cursor to the left.
Obviously these two commands are shortcuts that delete a small amount of characters.
2. Delete multiple characters
DD: Delete the entire row where the cursor is located. A number N can be added before the DD indicates the content of the current line and the subsequent line thereafter.
D or D $: Two command functionality is the content that starts from the cursor to the end to the end.
D0: Delete the content starting from the previous character from the cursor to the lead.
DW: Delete a word. If the cursor is in the middle of a word, then delete the word tail from the position where the cursor is located. Like the DD command, you can add a number N before DW, indicating that the N-specified words can be deleted.
If the user accidentally deletes the operation, it is not tight, and the VI provides a command to recover mistakes, and the recovered content can be moved, placed anywhere in the text. The recovery command is used 2 NP, where n is the register number. This is because there are nine registers for maintaining the delete operation, with numbers 1, 2, 1/4, 9, respectively, which saves content deleted with DD commands. These registers form a queue, such as the content that is most recently used by the DD command is placed in the register 1; when the text content is removed using the DD command next time, the VI will transfer the contents of the register 1 into the register 2, and the register 1 It will be the content of the most recent DD command deletion. In this class, the VI can save the content that has recently been deleted with the DD command, while the previous use of the DD command deleted is abandoned.
[Example 18] Assume that the current edited file is xu.c
/ * this is a example * /
#include
void main ()
{
INT I, J;
Printf ("Please Input a Number: / N");
Scanf ("% d", & i); j = i 100;
Printf ("/ n j =% d / n", j);
Return;
}
We do this:
1. Move the cursor to the first line of the file, press the DD command, the content of the file first row is deleted, and the content being deleted is saved in the register 1;
2. Press 5J to move the cursor to the first PrintF statement line;
3. Press the DD command to delete the row, at which time the register 1 will save the content just deleted:
Printf ("Please Input a Number: / N");
And register 1 original content:
/ * this is a example * /
Then be saved to register 2;
In the last mode, you can also delete the content of the file, but it can only delete the entire rightener, and you can delete all the rows within a specified range (starting line number, termination line number). It should be noted that when this method is deleted, the VI does not place the deleted content into the register, so when the error is removed, the 2 NP command is not recovered, and only a limited recovery can be performed with a U command.
Finally, how to delete the text in the text input mode. User use
The combination key can be, at which time the cursor will return the position of the insert, and the VI is still in the text input mode.
Cancel the previous order (UNDO)
Cancel the previous command (undo), also known as the restoration command, is a very useful command, it can cancel the previous misoperation or inappropriate operation of the influence of the file, make it reply to this misoperation or inappropriate operation Before being executed.
There are two forms in cancellation, and in command mode, type characters U and U. Their functions are canceled the command that just entered and returns to the original situation. Small write u and uppercase U have different details, the difference between the two is that the function of the uppercase U command is to restore the case before the misoperation command, that is, if the U command is used after the command is inserted, it deletes the content just inserted; If you use a U command after deleting the command, it is equivalent to inserting the content just deleted again at the cursor. Here, all modified texts are treated as inserted commands. That is, the U command can only cancel the previous step, if the previous step is to undo, when pressing the U key, not to undo the next step, but revoke the operation of the current U command, That is, it is restored to the status before the first time using the u command, and the result is nothing. The function of lowercase U commands is to restore the current row into the status before editing, regardless of how many times the line is edited.
[Example 19] The original screen display is:
#include
Main ()
{
}
Enter the command o in command mode, insert a new line, enter the content you need to insert, then press
Back to the command mode, the screen display is:
#include
Main ()
{
PRINTF ("How do you do!");
}
If you want to cancel this insertion operation, press the command U, the screen is restored to the original display.
Note: You can still use the cancel command for the cancel command. At this time, a "negative negative" effect will be generated. The file status will be restored to the status before the first execution of the cancel command, as if not do anything. For example, in the above example, once a command U is used, the screen will display the contents of the display.
Duplicate command (REDO)
Duplicate commands are also a very common command. You often encounter some operations that need to be mechanically repeated in text editing, and you need to use repeat commands. It allows users to easily perform a complex command that is just completed in front.
The repetition command can only work in command mode, and press "." Button in this mode. When a repetitive command is executed, its result is dependent on the current position of the cursor.
[Example 20] Screen display content is:
#include
Main ()
{
}
Enter the command O and enter a line of content, then
#include
Main ()
{
PRINTF ("How do you do!");
}
At this point, enter the command ".", The screen display is:
#include
Main ()
{
PRINTF ("How do you do!");
PRINTF ("How do you do!");