How to use vi editor

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)

  controls the movement of the screen cursor, deletes characters, words or lines, moves and copies a section and enters Insert mode,
or goes to last line mode.

2) Insert mode (Insert mode)

  Only in Insert mode, text input can be done, press the "ESC" key to return to the command line mode.

3) The bottom line mode (last line mode)

  saves the file or exits vi, and can also set the editing environment, such as searching for strings, listing line numbers...etc.

However, we generally simplify vi into two modes when using it, that is, the last line mode is also counted as the
command line mode (command mode).

2. Basic operations
of vi a) Enter vi and enter vi and the file name

  at the system prompt, then enter the vi full-screen editing screen:

   $ vi myfile


  But there is one thing to pay attention to, that is, after you enter vi, you are in the "command line" 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 first
Use the up, down, left, and right keys to move the cursor, but the computer keeps beeping and beeping, which makes you half-dead, so after entering vi, don't
move around, switch to "Insert mode" and talk about it!

b) Switch to insert mode (Insert mode) to edit the file

  In the "command mode (command mode)", click 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", you can only enter text all the time, if you find that you have entered a wrong
word! 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 (c
ommand mode)" and then delete the word.

d) 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 convert the article to the specified file

: wq (input "wq", save and exit vi) :

q! (input q!, force quit vi without saving)


3. Command mode (command mode) function key 1).   Press "i"
in insert mode

” switch to enter the insert mode “insert mode”, press “i” to enter the insert mode,
start inputting the file from the current position of the cursor;

  press “a” to enter the insert mode, start to enter the text from the position next to 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 and

  press "ESC" key.

3). To move the cursor

  vi, you can directly use the cursor on the keyboard to move up, down, left and right, but the regular vi uses lowercase English letters "h", "
j", "k", "l" to control the cursor left, down, Move up and right by one space.

  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 character

  Press "#l": the cursor moves to the #th position of the line, such as: 5l, 56l.

4). Delete the character

  "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": 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 #line from the line where the cursor is located

5). Copy

  "yw": Copy the characters from the cursor 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 where the cursor is.

  "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 return 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

  "c#w" at the end of the word: for example, "c3w" means to change 3 words

9). Jump to the specified line

  "ctrl" + "g" column Displays 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", line numbers are 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,
such as entering the number 15, then press Enter, will jump to line 15 of the article.

C) Search for characters

 "/keyword": first press the "/" key, then enter the character you want to search for, if the keyword you are looking for for the first time is not what you
want , you can keep pressing "n" to find it later the keywords you want.

 "?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) Exit 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

l move the
cursor one character to the right

k
Move the cursor up one line

j Move the
cursor down one line

^ Move
the cursor to the first

0 of the line, the
number "0", move the cursor to the beginning of the article

G Move
the cursor to the end of the article

$ Move the
cursor to the end of the line

Ctrl+f Scroll
forward

Ctrl+b
Scroll back

Ctrl+d Scroll
forward half screen

Ctrl+u Scroll
back half screen

i
Insert character before the cursor position a Add the character

after
the cursor position

o
Insert a new line, type

ESC from the beginning of the line
Return from the input state to the 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 Delete a word from #line yw
of the line number where the cursor is located #yw





Copy # characters at the cursor position

yy
Copy a line at the cursor position

#yy
Copy from # lines at the cursor position

p
paste

u
Cancel operation

cw
Change one 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

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 ?
To search, enter the content you want to find after / to use with / or ?, if the content to be searched is not the keyword you want to find, press

n
or backward (combined with /) or forward (
combined with ?) Keep looking until you find it.


For the first time using vi, there are a few points to remind:
1. After opening a file with vi, it is in "command mode", you need to switch to "
Insert mode" to be able to input Word. Switching method : press a button in the "command mode (command mode)"
Press the letter "i" to enter "Insert mode", at which point 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 the "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=326554544&siteId=291194637