The vi command is used to enter the vi editor. The vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor. This article will take you through the vi command under Linux and briefly introduce its usage.
vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor.
Vi’s working mode
Basically vi can be divided into three states, namely command mode (command mode), insert mode ( Insert mode) and bottom line mode (last line mode), the functions of each mode are distinguished as follows:
1. Command line mode command mode)
Control the movement of the screen cursor, characters, words or Line deletion, moving and copying a section and entering Insert mode, or to last line mode.
2. Insert mode
Only in Insert mode, text input can be done. Press the "ESC" key to return to the command line mode.
3. Last line mode
Save the file or exit vi. You can also set the editing environment, such as searching for strings, listing line numbers, etc.
Some basic operations of vi
1. Enter vi
and enter vi and file name at the system prompt. Enter the vi full-screen editing screen:
For example:
$ vi myfile
But one thing to pay special attention to is that after you enter vi, you are in "command mode (command mode)". You need to Switch to "Insert mode" to enter text.
2. Switch to Insert mode to edit the file
In "Command mode (command mode)", click the letter "i" to enter "Insert mode" )", then you can start entering text.
3. Insert switching
You are currently in "Insert mode", you can only keep inputting text. If you find that you have entered the wrong word, you want to use the cursor keys to go to To move back and delete the word, you must first click the "ESC" key to go to "command mode (command mode)" and then delete the word.
4. Exit vi and save the file
In "command mode (command mode)", click the ":" colon key to enter "Last line mode", for example:
: w filename (Enter "w filename" to save the article with the specified file name filename)
: wq (Enter "wq", save and exit vi)
: q! (Enter q! to force exit vi without saving)
Command mode function key
1), Insert mode
Press "i" to switch to the insert mode "insert mode". After pressing "i" to enter the insert mode, the input file starts from the current position of the cursor;
After pressing "a" to enter the insert mode, the input file starts from the current position. Start inputting text at the position next to the cursor;
After pressing "o" to enter insert mode, a new line is inserted, and text is entered starting from the beginning of the line.
2), switch from insert mode to command line mode
Press the "ESC" key.
3), move the cursor
vi can directly use the cursor on the keyboard to move up, down, left, and right, but regular vi uses lowercase English letters "h", "j", "k", and "l" to control the cursor left, down, up, and respectively. Move one space to the right.
Press "ctrl" "b": the screen moves "back" one page.
Press "ctrl" "f": the screen moves "forward" one page.
Press "ctrl" "u": the screen moves half a page "back".
Press "ctrl" "d": the screen moves "forward" by half a page.
Press the number "0": move to the beginning of the article.
Press "G": move to the end of the article.
Press "$": Move to the "end" of the line where the cursor is located.
Press "^": move to the "beginning" of the line where the cursor is
Press "w": the cursor jumps to the beginning of the next word
Press "e": the cursor jumps to the beginning of the next word End
Press "b": the cursor returns to the beginning of the previous word
Press "#l": the cursor moves to the #th position of the line, such as: 5l, 56l.
4) Delete text
「x」: Each time you press it, delete the character "behind" the cursor position.
"#x": For example, "6x" means to delete the 6 characters "behind" the cursor position.
"X": Capital X, each time you press it, delete the character "before" the cursor position.
"#X": For example, "20X" means to delete the "first" 20 characters at the cursor position.
『dd』: Delete the line where the cursor is.
"#dd": Delete # lines starting from the line where the cursor is located
5), copy
"yw": Copy the characters from the cursor position to the end of the word into the buffer.
「#yw」:Copy # words to the buffer
「yy」:Copy the line where the cursor is to the buffer.
"#yy": For example, "6yy" means to copy 6 lines of text "counting down" from the line where the cursor is located.
「p」: Paste the characters in the buffer to the position of the cursor. Note: All copy commands related to "y" must be combined with "p" to complete the copy and paste functions.
6), Replacement
「r」: Replace the character where the cursor is.
『R』: Replace the character where the cursor is until the "ESC" key is pressed.
7), return to the previous operation
"u": If you execute a command by mistake, you can press "u" immediately to return to the previous operation. Press "u" multiple times to perform multiple replies.
8), change
"cw": Change the word where the cursor is to the end of the word
"c#w": For example, "c3w" means changing 3 characters
9), jump to the specified line
"ctrl" "g" lists the line number of the line where the cursor is located.
"#G": For example, "15G" means moving the cursor to the beginning of the 15th line of the article.
Introduction to commands in Last line mode
Before using "last line mode", please remember to press the "ESC" key to confirm your After you are already in "command mode", press the ":" colon again to enter "last line mode".
A) List the line numbers
"set nu": After entering "set nu", the line number will be listed in front of each line in the file.
B) Jump to a certain line in the file
"#": The "#" sign represents a number. Enter a number after the colon and press the Enter key to jump to that line. Okay, if you enter the number 15 and press Enter, it will jump to the 15th line of the article.
C) Search for characters
"/Keyword": Press the "/" key first, and then enter the character you want to find. If the keyword you are looking for for the first time is not what you want , you can keep pressing "n" to search further until you find the keyword you want.
"?Keyword": Press the "?" key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing "n" to move forward. Find the keyword you want.
D) Save the file
"w": Enter the letter "w" in the colon to save the file.
E) Exit vi
"q": Press "q" to exit. If you cannot leave vi, you can force "!" to leave vi after "q".
『qw』: It is generally recommended to use it with "w" when exiting, so that the file can be saved when exiting.
vi command list
1. The following table lists the functions of some keys in command mode:
h: Move the cursor one character to the left
l: Move the cursor one character to the right
k: Move the cursor up one line
j: Move the cursor down one line
^: The cursor moves to the beginning of the line
0: The number "0", the cursor moves to the beginning of the article
G: The cursor moves to the end of the article
$: Move the cursor to the end of the line
Ctrl f: Scroll forward
Ctrl b: Turn the screen backward
Ctrl d: Turn half the screen forward
Ctrl u: Turn half the screen backward
i: In front of the cursor position Insert characters
a: Begin to add the character after the cursor position
o: Insert a new line and input from the beginning of the line
ESC: Exit from the input state To command state
x: Delete the characters after the cursor
#x: Delete # characters after the cursor
X: (Capital X), delete the characters before the cursor
#X: Delete # characters before the cursor
dd: Delete the line where the cursor is
#dd: Delete # lines from the line where the cursor is
yw: Copy a word at the cursor position
#yw: Copy # words at the cursor position
yy: Copy a line at the cursor position
# yy: Copy # lines from the line where the cursor is
p: Paste
u: Cancel the operation
cw: Change a word at the cursor position
#cw: Change the # characters at the cursor position
2. The following table lists some instructions in the line command mode
w filename: Save the file being edited The file is filename
wq filename: Save the file being edited as filename, and exit vi
q!: Abandon all modifications, exit vi
set nu: Display the line number
/or?: Search, enter what you want to find after /
n: Used together with/or?, if the search content is not the keyword you want to find, press n or Continue searching backward (used with /) or forward (used with ?) until found.
For the first time using vi, there are a few things to note:
1. After opening a file with vi, it is in "command mode" )", you need to switch to "Insert mode" to enter text. Switching method: Click the letter "i" in "command mode" to enter "Insert mode". At this time, you can start inputting text.
2. After editing, you need to switch from insert mode to command line mode to save the file. Switching method: press the "ESC" key.
3. Save and exit the file: Enter: wq in command mode! (Don’t forget the one in front of wq)
(Recommended learning: linux tutorial)
The above is the detailed content of Detailed explanation of vi command under linux. For more information, please follow other related articles on the PHP Chinese website!