CentOS File View and edit introduction detailed
Introduction to 2.1 Cat commands
The original meaning of the cat command is a connection (concatenate) that connects multiple file contents and outputs to the standard output stream (the standard output stream defaults to the screen). In practice, we often use it to display the contents of a file. If you are familiar with the type command under MS-DOS, it is not difficult to master the cat command. A common example of this command is as follows:
1 Cat file1.txt Display the contents of file1.txt file;
2
3 Cat file1.txt File2.txt Display the contents of file1.txt and File2.txt files;
4
5 Cat-n File1.txt Displays the contents of the File1.txt file and displays the line number;
2.2 VI Command Introduction
VI is the "Visual Interface" abbreviation, it on the status of Linux as if the edit program in DOS. It can perform output, delete, find, replace, block operations and many other text operations, and users can customize it according to their own needs, which is not available in other editing programs.
VI is not a typesetting program, it is not like word or WPS to the font, format, paragraph and other attributes of the choreography, it is just a text editing program.
VI no menu, only commands, and command a wide range. Limited to space, this article only describes the commonly used commands.
Vi has three basic modes of operation: command line mode, text input mode and last line mode.
Command-line mode:
Any time, regardless of the user in what mode, just click the "ESC" key, you can make VI into the command line mode; When you enter the VI command in the shell environment to start the VI editor, it is also in this mode.
In this mode, users can enter a variety of legitimate VI commands for managing their own documents. At this point, any character entered from the keyboard is interpreted as an edit command, and if the entered character is a legal VI command, then VI completes the corresponding action after receiving the user command (but note that the command entered is not displayed on the screen). If the input character is not a legal order VI, VI will ring alarm.
Text input mode:
Enter the Insert command I, attach command A, open command O, modify command C, replace command R, or replace command s in command mode to enter the text input mode. In this mode, any characters entered by the user are saved by VI as the file contents and displayed on the screen. In the text input process, if you want to return to command mode, press the "ESC" key.
Last line mode:
In command mode, the user presses the ":" Key to enter the final line mode, at which time VI will display the last line of the window (usually the last line of the screen) to display a ":" as a terminal mode prompt, waiting for the user to enter the command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to the file medium) after the end of the last line command, VI automatically back to command mode.
If the input command in the last line of the process changed the idea, you can press the "ESC" key or use the backspace button to delete all the input commands, and then click the Backspace key, you can make VI back to the command mode.
Entry and exit of 2.2.1 VI
In shell mode, type VI and the file name you want to edit to enter VI. For example:
1 VI example.txt
You can edit the Example.txt file. If the file exists, the contents of the file are displayed in the editing interface, the cursor is positioned on the first line of the file, and if the file does not exist, there is nothing in the editing interface. If you need to enter the VI editing interface, the cursor placed in the file of the nth line, then the VI command after the "+n" parameter can be. For example, if you want to display from line 5th of the Example.txt file, use the following command:
1 VI +5 example.txt
When you exit VI, you need to enter the exit command "Q" in the last line mode. If in text input mode, first press "ESC" to enter command mode, and enter ":" Enter the last line mode in last line mode, you can use the following Exit command
1:q exit directly. If you modify the contents of the document in text input mode, you cannot exit.
2
3:wq to exit after saving.
4:x with "Wq".
5
6:q! – Do not save content, force exit.
2.2.2 VI Display line number
In last line mode, enter the following command.
1 Set Number
Enables VI to display line numbers in the editing interface.
In addition, in end-line mode, you can use the following "Nu" command (short of number) to display the line number of the line where the cursor is located and the contents of the row.
2.2.3 Cursor Move operation
In a full screen text editor, the movement of the cursor is undoubtedly the most frequently used operation. The user is skilled at using these commands to move the cursor to quickly and accurately reach the desired location for editing.
The cursor movement in VI can be either in command mode or in text input mode, but the method of operation is different.
In text input mode, you can move the cursor directly using the four arrow keys on the keyboard; in command mode, there are many ways to move the cursor. Not only can you use four arrow keys to move the cursor, you can also use H, J, K, l these four keys instead of four arrow keys to move the cursor, so as to avoid the different machines on the different keyboard definitions brought by the contradictions, and the use of skilled hands can not leave the letter keyboard position can complete all operations, thereby improving efficiency.
The following command completes the cursor movement in command line mode:
The h cursor moves to the left, if enters the number n before pressing the H command, then moves the cursor to the left n characters;
L The cursor moves to the right, if the number n is entered before the L command, the cursor is shifted to the right n characters;
The J cursor moves up, and if you enter a number n before pressing the J command, the cursor moves n characters;
K cursor down, if you enter the number n before the K command, the cursor Xia to move n characters;
0 (0) the cursor moves to the beginning of the line;
The $ cursor moves to the end of the line;
The H cursor moves to the first line displayed on the screen (not necessarily a file header);
The L cursor moves to the last line displayed on the screen (not necessarily the end of the file);
The M cursor moves to the middle line of the screen;
The NG cursor moves to the nth line;
W or W moves the cursor right to the beginning of the next word;
E or E If the cursor start position is within a word (that is, not at the end of a word), the command moves the cursor to the ending of the word, and if the cursor starts at the end of the word, the command moves the cursor to the suffix of the next word.
B or B if the cursor is within the word (that is, not the first word), the command moves the cursor to the beginning of the word, and if the cursor is at the beginning of the word, the command moves the cursor to the beginning of the previous word;
Ctrl+g status Command, which displays the status of the currently edited document. Includes the file name that you are editing, whether it has been modified, the current line number, the number of rows in the file, and the percentage of the line before the cursor that occupies the entire file.
Alternatively, you can complete the movement of the cursor throughout the file by following the command in the last row mode:
: n the cursor moves to the nth row of the file;
: $ The cursor moves to the last line of the file;
2.2.4 Text Insert operation
In command mode, any character entered by the user is interpreted by VI as a command, and if the user wants to use the entered character as a textual content, first switch the VI's working mode from command mode to text input mode. The way to switch is to use the following command:
1. Inserting (INSERT) command
VI provided two INSERT commands: I and I.
(1). The I command inserts text from the position before the cursor, and you can use the key to delete the wrong input during the insert process. At this time VI is in the Insert State, the screen is the most downward display "–insert–" inserted words.
(2). I command the command to move the cursor to the beginning of the current line, and then insert the text before it.
2. Additional (append) Order
VI provides two additional insert commands: A and A.
(1). A command the command is used to append new text after the cursor is currently in place, the new text is placed after the cursor, and the original text after the cursor is moved backward accordingly, and the cursor can be anywhere on one line.
(2). A command the command differs from a command, a command moves the cursor to the end of the line at which it is inserted, inserts new text from there, and automatically moves to the end of the line when you enter a command.
The A and a commands are the only way to insert text at the end of a line.
3. Open command
Whether it's an insert command or a append command, the inserted content starts at 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 offers two open commands: O and O.
(1). o command the command will open a new line below the line where the cursor is located, and place the cursor at the beginning of the line, waiting for the input text. Note that when you use delete characters, you can only delete characters that start at the beginning of the insertion mode, and do not work for previous characters.
(2). o command and O command instead, the O command inserts a row above the line where the cursor is positioned and places the cursor at the beginning of the line, waiting for the input text.
2.2.5 Text modification operation
In command mode, you can use the various commands provided by VI to modify text, including deletion, copying, substitution and substitution of text content.
1. Text deletion/Movement
When editing text, often need to delete some unwanted text, we can use the key to the wrong or unwanted text deletion, but at this time there is a limit is that when you delete the wardrobe, then want to delete the contents of the above line is not possible.
In command mode, VI provides a number of commands for removing commands. Most of them start with a d. Commonly used are:
(1). Delete a single character
x deletes the character at the cursor. If you add a number n before x, delete the n characters to the right starting at the position of the cursor.
x deletes the character at the front of the cursor, and if you add a number n before x, delete the n characters from the beginning of the cursor to the left.
Obviously these two commands are a quick way to remove a few characters.
(2). Delete multiple characters
DD deletes the entire line where the cursor is located. You can add a number n before DD to delete the contents of the current row and its n-1 row.
D or d$ Two command functionality, delete the content from the beginning of the cursor to the end of the line.
D0 deletes the content from the beginning of the cursor to the start of the first character.
DW Deletes a word. If the cursor is in the middle of a word, delete the suffix from the cursor position. As with the DD command, you can add a number n before the DW. Represents the deletion of n-specified words.
If the user accidentally deletes the operation, also does not matter VI, provides restores the error operation the command, and may move the restored content, places in the text anywhere. The RESTORE command uses "NP", where n is the number of times it needs to be recovered. For example, by using the DD command to delete a line of content and then using the "2p" command, the deleted content is reinserted two times.
With the combination of DD command and P command, it is convenient to implement the movement of text lines.
2. Text Copy command
yy copies the entire line where the cursor is located. You can add a number n before yy to indicate the contents of the copied current row and its n-1 row.
After text lines are copied, you can paste the lines of text anywhere by using the "P" command described above.
The DD, YY, and P commands work in VI, similar to the cut, copy, and paste commands for graphics editors in Windows.
3. Cancel the previous command (undo)
Canceling the previous command (Undo), also known as the Undo command, is a useful command that cancels the effect of the previous or improper operation on the file to revert to the state before the misoperation or improper operation was executed.
There are two forms of canceling the previous command, typing the characters u and u in command mode and their function is to cancel the command just entered and revert to the original situation. The lowercase u and the capital U differ in specific details, the difference is that the function of the capital U command is to revert to the situation before the Misoperation command, that is, if you use the U command after inserting the command, delete what you just inserted; if you use the U command after you delete the command, you are inserting what you just deleted at the cursor. Here all the commands that modify the text are treated as insert commands, which means that the U command can only cancel the previous operation. If you undo the previous action with the U command, when you press the U key again, you are not undoing the previous step, but you are undoing the operation you just performed, that is, returning to the state before you first used the U command, It turned out that nothing had been done. The function of the lowercase u command is to revert the current row to the state before it was edited, regardless of how many times the row was edited.
4. Repeat command (Redo)
Repeating a command is also a very common command. In text editing, you often encounter the need to mechanically repeat some operations, and then you need to use a repeat command. It makes it easy for the user to perform a complex command that was just completed earlier.
Duplicate commands work only in command mode, and the "." Key is available in this mode. When a repeating command is executed, the result is dependent on the current position of the cursor.
2.2.6 text Search and replace operations
When you are editing text, you can search for or locate a specific word or part of a word. The VI editor has the ability to search forward or backward for a specified pattern.
"/" is the VI Editor's Mode search command. When you type/, a line appears at the bottom of the screen, and a diagonal shoulder appears at the beginning of the bottom line, and the cursor is placed after the tilt. At this point, you can type the pattern you want to search at the cursor position. After you enter the completion and press ENTER, the editor will begin searching for the pattern you typed from the cursor's location to the end of the file. If the input pattern is found, the cursor will remain on the found pattern. After you find a matching pattern, use the command "n" If you need to search for the next matching pattern, or use the "n" command if you need to find the last matching pattern.
In addition, the "?" command is also a pattern search command. and "/" is the difference between/is from the cursor when
The previous location searches for the end of the file, and the "?" Command searches the file head for the current location of the cursor.
Text substitution operations require the use of a last-line pattern, which means that all text substitution operations commands start with ":".
The format of the text substitution command is:
: S/TEXT1/TEXT2 is used to replace the first "text1" That is searched for the paragraph where the cursor is located with the "Text2";
: s/text1/text2/g is used to replace all "Text1" in the paragraph where the cursor is positioned with "text2";
: m,ns/text1/text2/g is used to replace all "text1" searched to Text2 from the beginning of the M line to the end of n rows. You can use $ to represent the last line, which means "1,$" to replace all eligible characters in the document.