Launch the visual editor from the shell
The VI filename instructs the shell to start the VI editor and pass the parameter filename to it. If the file is currently present, the VI Editor interprets it as the file to open, and if the file is not available, the VI compiler creates a new file
VI file1 file2 File3shell pass 3 parameters to Vi,vi to interpret them as files to open. You can use the: w command to save the file, using the: N command to access the next file
VI +# filename Opens the file and moves the cursor to the specified line. For example, command VI +100 records to edit a file starting from line 100th records
VI +/the filename Opens the file and moves the cursor to the line containing the target string. For example, command VI +/jason friends to start editing a file from the 1th line containing the string Jason friends
View filename Opens the file for editing, but refuses to save changes to the file unless you use the w! command
Cursor Move command
HJKL move the cursor one character to the left, down, up, and right, respectively
0 (0) moves the cursor to the beginning of the current line
^ (caret) the same as 01 moves the cursor to the beginning of the current line
$ moves the cursor to the end of the current line
# #G Move the cursor to the line specified by the number in front of G. For example, 42G moves the cursor to line 42nd of the file
G move the cursor to the last line of the file
W move the cursor forward to the first letter of the next word
E moves the cursor forward to the last letter of the next word
b move the cursor back to the first letter of the previous word
-Position the cursor at the beginning of the previous line
+ Position the cursor at the beginning of the next line
12| Position the cursor to the 12th column of the current row
L position the cursor to the bottom line of the screen
M position the cursor to a line in the middle of the screen
H position the cursor to the top row of the screen
"Two single quotes move the cursor to its previous position
Cursor position command (context)
FB moves the cursor forward to the next letter B (or any other specified character) on the current line
FB moves the cursor back to the previous letter B (or the specified character) on the current line
t# moves the cursor to the right of the 1th instance of the character # on the current line. For example, a command TM moves the cursor to the right of 1th m on the current line
t# moves the cursor to the left on the current line and moves it to the first character of the 1th instance of the character #
/word moves the cursor forward to the next instance of Word
Word moves the cursor back to the previous instance of Word
n Move the cursor to the previous command/word or? Specify the next instance of the pattern in Word
Show Adjustment commands
Ctrl+d display the lower half of the text in a file
Ctrl+u display the top half of text in a file
Ctrl+f Display the next screen of text in a file
Ctrl+b Display the previous screen text in a file
Setting Display Options
: Set number takes line numbers as part of the screen display, but the line numbers are not part of the file. Its abbreviated form is: Set Nu
: Set Nonumber clears the line number on the screen. You can also use the abbreviated form: Set Nonu
: Set Showmode displays append mode information in the lower-right corner of the screen
: The set list displays the dollar sign at the end of each row, and the Ctrl+i represents a tab
: Set Showmatch is entered) or], the cursor is moved to the match (or [
: Set Window=value defines the number of lines of text lines displayed on the screen
: Set autoindent Auto indent. You can also use the abbreviated form: Set AI
: Set Tabstop=value sets the number of space characters that display tabs. You can also use the abbreviated form Ts=value
: Set Wrapmargin=value Sets the right page edge of the display. The editor automatically returns a newline when the input enters the page edge you set
: Set ignorecase instructs the editor to search for strings and ignores the case of letters in the target
: Set display all options for settings
: Set all shows all options that can be set
Text Delete command
DD Delete all lines of text at the current cursor
#dd Delete # lines of text
DW removes a word from the text
#dw Remove the # words from the text
X remove one of the characters in the cursor
#x Remove the # characters from the text
D Delete the part after the cursor on the current line
: #, #d for example,: 12,37d will delete all text between lines 12th to 37th, including lines 12th and 37
Undo Command
U undo. Restores the last text modification operation, even if the cursor has been moved. In a Linux system, using the Undo command again restores the previous text modification operation. In BSD VI, the 2nd undo operation Will undo the first undo operation, restoring the text modified before the 1th revocation
: Redo in the Linux system, the undo action restores the text modification. In a standard UNIX system, the 2nd u command cancels the 1th u command, and the result is a "redo"
U You can undo all text modifications to the current line if you haven't moved the cursor out of the current line after the modification
Add text to Text
A (lowercase) inserts text from the right of the cursor
A (uppercase) to add text starting at the end of the current line
I (lowercase) inserts text from the left of the cursor
I (uppercase) inserts text from the beginning of the current line
O (lowercase) opens (or inserts) a new line under the cursor
O (uppercase) opens a new line above the cursor
: #r filename For example,: 8rreport.old reads the file Report.old and puts its contents after the 8th line of the current file
ESC can return to VI command mode by pressing ESC to leave append mode, regardless of the command used to enter append/insert mode
CTRL + V allows the input of control characters. Pressing the CTRL + V key and then pressing ENTER will insert the ctrl+m into the file
Modify text in a file
CW simply modifies the word at the cursor (deletes the word and then enters append mode to add text where the word is deleted)
s (lowercase) replaces a single character
S (uppercase) replaces entire line of text
CC Replace whole line text (same s)
R replaces the character at the current cursor with the next character entered and automatically returns to command mode
R puts the editor in overlay mode, replacing the character at the cursor one by one with the input characters
C (uppercase) modifies the text from the cursor to the end of the line on the line
ct# modifies the text from the cursor to the forward 1th target character on the line. For example, CTY will delete all text from the cursor to the 1th character y on the current line, and enter append mode to add text where the text is deleted
cf# modifies the text (including the target character) from the cursor to the forward 1th target character on the line. For example, CFY will delete all text from the cursor to the 1th character y (including y) on the current line, and enter append mode to add text where the text is deleted
ct# modifies the text from the cursor to the 1th target character on a line. For example, CTY will delete all text from the cursor to the 1th character y on the current line, and enter append mode to add text where the text is deleted
cf# modifies the text (including the target character) from the cursor to the 1th target character on a line. For example, CFY will delete all text from the cursor to the 1th character y (including y) on the current line, and enter append mode to add text where the text is deleted
Word that is attached and pasted line
YY copies or adds the current line to an intrinsic buffer. 20yy copies the current line and 19 rows (20 lines) behind it to memory. The destination row remains in the file, and you can use the P command to paste the in-memory text into the file
DD deletes the current line and places it in the same memory buffer used by the YY command. The destination row is removed from the file, but you can use the P command to paste it elsewhere in the file
YW the word where the current cursor is located or copies it into the inner buffer. The 6YW command copies the current Word and 5 (6) words behind it into memory
DW deletes the current word and places it in the same memory buffer used by the YW command. You can use the P command to paste the word elsewhere in the file
yt# the text from the cursor to the forward one character (excluding the character). For example, the YTB command will connect or copy text from the cursor to the next instance of character B (excluding character B) to memory
yf# the text from the cursor to the forward one character (including the character). For example, the YF: command copies the text from the cursor to the next instance of the character: (including the character:) to the memory
yt# (excluding the target character). For example, the YTN command will move from the cursor to the 1th instance of the character n
The text (excluding the character N) is either copied to the memory
yf# (including the target character) is followed up. For example, the YFJ command will take the text (including the character N) from the cursor to the backward 1th instance of the character n, or copy it to memory
P Paste the lines of text in memory below the line of the cursor in the file, or paste the words in memory to the right of the cursor in the file
P (uppercase) Pastes the line of text that is being received or deleted to the top of the line in the file where the cursor is located. Or paste the word you've removed or deleted to the left of the cursor in the file
File Move command
J merges the downstream text into a row with the current line
: #,# Move # Moves the specified line to the target location. : The 12,35 move58 command moves all text between line 12th to 35th to the back of line 58th. Abbreviated to MO
: 1,26 Co 82 copies all text between lines 1th to 26th to the back of line 82nd (line number can be selected)
Use the visual editor for global editing
: S/target/replacement/finds the 1th instance of the target string on the current line and deletes it, and then replaces it with a string replacement. Modify only the 1th target instance on the current line
: G/target/s//replacement/finds the 1th instance of the target string on all rows and deletes it, and then replaces it with a string replacement. Modify the 1th instance of a target on all lines
: #,# s/target/replacement/to be replaced on the specified line. For example, 7,37s/march/walk/will find all lines of text between lines 7th to 37th and replace the 1th target string in each line with a string walk March. 1th target string modification on all specified lines
: #,# s/target/replacement/g The global substitution on the specified line. For example, 1,$ s/fun/joyful/g will find all instances of the target string fun in line 1th to the last line of the file and delete it, and then replace it with the string joyful. All instances of the target string on the specified line are modified
: G/target/s/replacement finds the 1th instance of the target string on all rows and deletes it, and then replaces it with a string replacement. The 1th target on all lines has been modified.
: #,# target/s/replacement/to be replaced on the specified line, for example,: 7,37march
VI Command usage