Basic usage of vi/vim The
vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor. Here is just a brief introduction to its usage and a small number of commands. Since the vi editor is identical to any version of Unix and Linux systems, you can learn more about it wherever vi is introduced. Vi is also the most basic text editor in Linux, after learning it, you will be able to travel freely in the Linux world.
1. The basic concept of
vi 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 as follows:
1) command line mode command mode)
Control the movement of the screen cursor, delete characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode.
2) Insert mode (Insert mode)
Only in Insert mode, text input can be done, press "ESC" key to return to command line mode.
3) bottom line mode (last line mode)
Save the file or exit vi, you can also set the editing environment, such as finding strings, listing line numbers...etc.
However, we generally simplify vi into two modes when we use it, that is, the last line mode is also counted as the command line mode (command mode).
2. Basic operation of vi
a) Enter vi
After the system prompts you to enter vi and the file name, you will enter the vi full-screen editing screen:
$ vi myfile
But there is one thing to pay special attention to, that is, after you enter vi, you are in "command mode (command mode)", you have to switch to "insert mode (Insert mode)" to be able to enter text. People who use vi for the first time will want to use the up, down, left, and right keys to move the cursor first. As a result, the computer keeps beeping and beeps, which makes me half-dead. So after entering vi, don't move around, switch to "Insert mode" and then talk about it. !
b) Switch to Insert mode to edit the file
In the "command mode (command mode)" press the letter "i" to enter the "insert mode (Insert mode)", then you can start typing text.
c) Switch of Insert
You are currently in "Insert mode" and you can only keep typing text if you notice a typo! If you want to use the cursor keys to move back and delete the word, you must first press the "ESC" key to go to "command mode" and then delete the word.
d) Exit vi and save the file
In "command mode (command mode)" , press the ":" colon key to enter "Last line mode", for example:
: w filename (enter "w filename" to save the article with the specified filename filename)
: wq (type "wq", save and exit vi)
: q! (input q!, force quit vi without saving)
3. Command mode function key
1). Insert mode
Press "i" to switch to insert mode "insert mode", and press "i" to enter insert mode to start inputting the file from the current position of the cursor;
After pressing "a" to enter the insert mode, the text starts from the next position of the current cursor position;
After pressing "o" to enter the insert mode, a new line is inserted, and the text is entered 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 the regular vi uses the lowercase English letters "h", "j", "k", "l" to control the cursor to move left, down, up, and right by one grid.
Press "ctrl" + "b": the screen moves to the "back" one page.
Press "ctrl" + "f": the screen moves to the "forward" one page.
Press "ctrl" + "u": the screen moves to the "back" half page.
Press "ctrl" + "d": the screen moves to the "forward" half 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 line" of the line where the cursor is located.
Press "^": move to the "start of line" of the line where the cursor is located
Press "w": the cursor jumps to the beginning of the next word
Press "e": the cursor jumps to the end of the next word
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 press deletes one character "behind" the cursor position.
"#x": For example, "6x" means to delete 6 characters "behind" the cursor position.
"X": A capital X, each time you press it, deletes one character "before" the cursor position.
"#X": For example, "20X" means to delete 20 characters "before" the cursor position.
"dd": Delete the line where the cursor is located.
"#dd": delete # lines 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 located 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 cursor position. Note: All copy commands related to "y" must be combined with "p" to complete the copy and paste function.
6). Replace
"r": Replace the character at the cursor position.
"R": Replace the character at the cursor position until the "ESC" key is pressed.
7). Redo/Undo/Restore the last operation
"u": If you execute a command by mistake, you can press "u" immediately to undo the previous operation. Multiple undos can be performed by pressing "u" multiple times.
8). Change
"cw": Change the word where the cursor is located to the end of the word
"c#w": For example, "c3w" means change 3 words
9). Jump to the specified line
"ctrl" + "g" lists the line number of the line where the cursor is located.
"#G": For example, "15G", which means move the cursor to the beginning of the 15th line of the article.
4. Introduction to commands in Last line mode
Before using "last line mode", please remember to press the "ESC" key to confirm that you are already in "command mode", and then press the ":" colon to enter the "last line mode" ".
A) List line numbers
"set nu": After typing "set nu", the line number is listed before each line in the file.
B) Jump to a line in the file
"#": The "#" sign represents a number. Enter a number after the colon, and then press the Enter key to jump to the line. For example, if you enter the number 15 and press Enter, it will jump to the 15th line of the article. .
C) find characters
"/Keyword": Press the "/" key first, and then enter the character you want to find. If the keyword you are looking for the first time is not what you want, you can keep pressing "n" to find the key you want later. until the word.
"?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 find the key you are looking for. until the word.
D) save the file
"w": Enter the letter "w" in the colon to save the file.
E) leave vi
"q": Press "q" to exit. If you cannot leave vi, you can forcefully leave vi after "q" followed by a "!".
"qw": It is generally recommended to use it together with "w" when leaving, so that the file can be saved when exiting.
5. 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
lMove
the cursor one character to the right
kCursor
up one line
jThe
cursor moves down one line
^
move the cursor to the beginning of the line
0
The number "0", the cursor moves to the beginning of the article
G
cursor to the end of the article
$
cursor to end of line
Ctrl+f
scroll forward
Ctrl+b
scroll backwards
Ctrl+d
scroll forward half screen
Ctrl+u
scroll back half screen
i
insert character before cursor position
a
starts incrementing at the character after the cursor position
oInsert
a new line, starting from the beginning of the line
ESC returns
from the input state to the command state
x
deletes the character after the cursor
#x
deletes # characters after the cursor
X
(capital X), deletes the character before the cursor
#XDelete
# characters before the cursor
dd
deletes the line where the cursor is located
#dd
deletes # lines from the line where the cursor is located
yw
copies a word at the cursor position
#ywCopy #words at
the cursor position
yy
Copy the line at the cursor position
#yyCopy #line
from the line where the cursor is located
p
paste
uCancel
the operation
cw
change a word at the cursor position
#cwChange
the # characters at the cursor position
The following table lists some commands in line command mode
w filename
stores the file being edited as filename
wq filename
stores the file being edited as filename and exits vi
q!
Abandon all modifications and exit vi
set nu
display line number
/ or ?
find, enter what you want to find after /
Use n
and/or ? together. If the searched content is not the keyword you want to find, press n or backward (used with / or with ?) to continue searching until you find it.