vi basic commands

First, 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. www.2cto.com  
 
2) Insert mode
 
  Only in Insert mode, text input can be done, press "ESC" key to return to command line mode.
 
3) Last line mode
 
  Save the file or exit vi, you can also set the editing environment, such as finding strings, listing line numbers...etc.
 
    But generally we 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).

 Second, the basic operation of vi

 

a) Entry 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)

 

Third, the command line mode (command mode) function keys

1). Insert mode
 
       Press "i" to switch to insert mode "insert mode", after pressing "i" to enter insert mode, the input file starts from the current position of the cursor; www.2cto.com  
 
  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 is to use 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 "back" one page.
 
  Press "ctrl" + "f": the screen moves to "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 time it is pressed, one character "behind" the cursor position is deleted.
 
  "#x": For example, "6x" means to delete 6 characters "behind" the cursor position.
 
  "X": uppercase X, each time you press it, delete the "preceding" character at 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 # characters to the buffer www.2cto.com  
 
  "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). Reply to the last operation
 
  "u": If you execute a command by mistake, you can press "u" immediately to go back to the previous operation. Multiple replies 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 the "last line mode", please remember to press the "ESC" key to make sure you are in the "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. . www.2cto.com   
 
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.

 

 

Five, vi 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 a line
 
j
Move the cursor down one line
 
^
Move the cursor to the beginning of the line
  www.2cto.com  
0
The number "0", the cursor moves to the beginning of the article
 
G
Move the cursor to the end of the article
 
$
Move cursor to end of line
 
Ctrl+f
scroll forward
 
Ctrl+b
scroll back
 
Ctrl+d
Flip forward half screen
 
Ctrl+u
Flip back half screen
 
i
Insert character before cursor position
 
a
starts incrementing at the character after the cursor position
 
O
Insert a new line, starting from the beginning of the line
 
ESC
Return from input state to command state
 
x
delete the character after the cursor
 
#x
Delete the # characters after the cursor
 
X
(capital X), delete the character before the cursor
 
#X
Delete the # characters before the cursor
 
dd
delete the line where the cursor is located
 
#dd
Deletes # lines from the line where the cursor is located
  www.2cto.com  
is
Copy a word at the cursor position
 
#yw
Copy # words at the cursor position
 
yy
Copy the line at the cursor position
 
#yy
Copy the # line from the line where the cursor is located
 
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 commands in line command mode
w filename
Save the file being edited as filename
  www.2cto.com  
wq filename
Save the file being edited as filename and exit vi
 
q!
Abandon all changes and exit vi
 
set no
show line number
 
/or?
Find, enter what you want to find after /
 
n
Used together with / or ?, if the searched content is not the keyword you want to find, press n or backward (with / with) or forward (with ?) to continue searching until you find it.
 
For the first time using vi, there are a few points to remind:
1. After opening the file with vi, it is in "command mode", you need to switch to "Insert mode" to be able to enter text. Switching method: Press the letter "i" in "command mode" to enter "Insert mode", then you can start typing text.
2. After editing, you need to switch from insert mode to command line mode to save the file. Switching method: press "ESC" key.
3. Save and exit the file: enter: wq in command mode! (don't forget the : in front of wq)

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326916367&siteId=291194637