about VI
- VI is a kind of text editing program that exists widely in various UNIX and Linux systems.
- VI is not a typesetting program, but a purely text-editing program.
- VI is a full screen text editor, it has no menus, only commands.
- VI is not window-based, so this multi-purpose editing program can be used to edit a wide variety of files on any type of terminal.
- VI function is very powerful, but command a wide range of proficiency in a certain degree of difficulty.
- The inventor of VI: Bill Joy
VI Start-up
$ VI filename or $ VI
If filename already exists, VI will open the existing file
If it is a new file, VI will create it
Vi. exit of VI
Enter in command-line mode: q,:q! ,: Wq or: X, you can exit VI
: W Save
: w filename saved as filename
: wq! Save and exit
: wq! FileName is saved with filename and then exited
: q! Do not save exit
: x Save and exit, function and: wq! same
VI mode of Operation
VI has three modes of operation, namely: Command mode, insert mode (edit mode) and last line mode , three modes perform different operations respectively, they can be switched between the two.
- Command mode : After entering VI, the first entry is the command mode, waiting for the user to enter the editing command, the letter entered at this time as an edit command to explain.
- Insert mode : Enter insert command in command mode I, additional command A, open command O and other commands can enter the insertion mode, in the Insert mode, the user input can edit the text, any input characters are saved as the contents of the file. The "ESC" key returns to the command mode.
- Last line mode : In command mode, press ":" Key to enter the last line mode, the VI cursor will be displayed in the final line of the window, ":" As a prompt for the last row mode, waiting for the user to enter the command. After the last command executes, VI automatically returns to the command mode.
VI Command Daquan
1. Move the cursor
H or LEFT arrow: the cursor moves one character to the left
L or RIGHT ARROW key: The cursor moves one character to the right
K or UP ARROW: The cursor moves up one character
J or DOWN ARROW: the cursor moves down one character
"Ctrl" +f: Screen "down" to move a page, equivalent to "PageDown"
"Ctrl" +b: Screen "Up" to move a page, equivalent to "PageUp"
0: Number 0, move to the front of the line at the top of the character
$: Move to the last character in this line
G: Move to the last line of this file
GG: Move to the first line of this file
N "Enter": N is a number and the cursor moves down n rows
2. Search and replace
/STR: Starting with the cursor, look down for a string named Str.
? STR: Starting with the cursor, look up for a string named Str.
: N1,n2s/str1/str2/g:n1 and N2 are numbers, look for str1 between line N1 and N2 line, and replace the string with STR2
: 1, $s/str1/str2/g: look for the str1 string from the first line to the last line and replace the string with STR2
: 1, $s/str1/str2/gc: look for the str1 string from the first line to the last line, replace the string with str2, and confirm with the user before the replace prompt whether a replacement is required
3. Delete, copy and paste
X,x:x Delete one character for backward, X to delete one character forward
Nx:n is a number, and the n characters are removed for successive backwards
DD: Delete the entire line where the cursor is located
Ndd:n is a number, starting at the cursor position, removing the down n columns, such as 20DD, to delete the 20 columns.
YY: The line where the cursor is copied
Nyy:n is a number, copy the down n rows where the cursor is located, such as 20yy to copy 20 rows
P,p:p Pastes the copied data to the next line of the cursor, and P is glued to the previous line of the cursor.
U: Restore previous action
"Ctrl" +r: Redo the previous action
.: decimal point, repeat the previous action, commonly used in repeated deletion, repeated paste.
4. Insert mode
I,i: Insert: Inserts the input text at the current cursor, the existing text is backward, where I is inserted from before the current cursor position, I move the cursor to the beginning of the current line, and then start inserting.
A,a:a is inserted from the next character at the current cursor, a is inserted from the last character of the line where the cursor is located
O,o: This is the case of the English letter O, O to insert a new line at the next line where the current cursor is located, O to insert a new row on the previous line where the current cursor is located.
R,r: Replace: R is the character that the replacement cursor is in, and R will always replace the text of the cursor until the ESC key is located.
5, file operation related
: w: Save File
: w filename: Save as FileName
: N1,n2 w filename!: Saves the contents of the N1 line to the N2 row in filename
: n w FileName: Save nth row in filename
: L,. W FileName: Saves the content from the first line to the current cursor position in filename
:., $ w FileName: Saves content from the cursor's current position to the end of the file in filename
: R filename: Open another file filename
: E FileName: Edit new file filename instead of original content
: F filename: Rename the current file to filename Vi
VI Editor commands