Three modes of the VI editor

Three modes of the vi editor

Generally speaking, the vi editor has three modes, namely the general command mode, the edit mode and the command line command mode. The functions of these three modes are:

command mode< /span>

Open a file with vi and directly enter the general command mode (this is the default mode , Also referred to as the general mode). In this mode, you can use the up, down, left, and right buttons to move the cursor, you can use delete characters or delete entire lines to process the content of the file, and you can also use copy and paste to process your file data.

insert mode

The editing mode can edit the content, press any one of i, I, o, O, a, A, r, R, etc. After the letter, you can enter the edit mode. If you want to return to the general command mode, you must press the Esc button to exit the editing mode.

command-line mode

In the normal mode, enter any of the three buttons:, /,? to move the cursor to The bottom line. In this mode, you can provide you with the actions of searching for data, and the actions of reading, saving, replacing a large number of characters, leaving vi, displaying line numbers, etc. are achieved in this mode! In short, the relationship between the three can be summarized with the following figure:

share picture

Shortcut keys in each mode h2>

General command mode

< tr class="md-end-block">

< td>

How to move the cursor
h or the left arrow key (←) Move the cursor one character to the left
j or down arrow key (↓) < span class="td-span">Move the cursor down one character
k or up arrow key (↑) Move the cursor up one character
< span class="md-plain">l or right arrow key (→) Move the cursor one character to the right
hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, move down 30 lines, you can use the combination of “30j” or “30↓”, that is, add the desired number of times (number), and then press the action!
[Ctrl] + [f] The screen moves down one page, which is equivalent to the [Page Down] key
[Ctrl] + [b] The screen moves up one page, which is equivalent to the [Page Up] button
[Ctrl] + [d] Move the screen down half a page
[Ctrl] + [u] Move the screen up half a page
n n represents a number, such as 20. After pressing the number and then pressing the space bar, the cursor will move to the right by n characters in this line. For example, if 20, the cursor will move back 20 characters.
0 or function key [Home] Move to the top of this line Character place
$ or function key [End] moved to this line At the last character
H Move the cursor to the top of this screen The first character of a line
M The cursor moves to the center of this screen The first character of that line
L The cursor moves to The first character on the bottom line of this screen
G Mobile Go to the last line of this file
nG n is a number. Move to the nth line of this file. For example, 20G will move to line 20 of this file
gg mobile Go to the first line of this file
n n is a number. Move the cursor down n lines
Search and replace
/word < span class="td-span"> look for a string named word under the cursor
?word < span class="td-span"> look for a string with the string name word above the cursor
n Repeat the previous search action
N is the opposite of n, which is the reverse of the previous search action. For example, after /word, pressing N means?word
:n1,n2s/word1/word2/g n1 and n2 are numbers. Search for the string word1 between lines n1 and n2, and replace the string with word2, for example: search for null between lines 100 to 200 and replace it with NULL, then :100,200s/null /NULL/g
:1,$s/word1/word2/g < span class="md-plain">Find the word1 string from the first line to the last line, and replace the string with word2
:1,$s/word1/word2/gc< /td>

Find the word1 string from the first line to the last line, and replace the string with word2. And before the replacement, a prompt character is displayed to confirm to the user whether it needs to be replaced
Delete, copy and paste
x, X In a line of characters, x is to delete one character backward, and X is to forward Delete a character
nx n is a number, delete n consecutive backwards Characters
dd delete the entire row where the cursor is located
ndd n is a number. Delete the n rows down where the cursor is. For example, 20dd is to delete 20 rows
d1G Delete all data from the cursor to the first line
dG Delete all data from the cursor to the last line
d$ Delete where the cursor is
d0 Delete where the cursor is , To the first character of the line
yy Copy the line where the cursor is located
nyy n is a number . Copy the cursor down n lines, for example, 20yy is to copy 20 lines
y1G copy all data from the line where the cursor is to the first line
yG Copy all data from the line where the cursor is to the last line
y0 copy cursor All data from the character to the beginning of the line
y$ Copy all data from the character where the cursor is to the end of the line
p, P p is to paste the copied data to the next line of the cursor, and P is to paste to the previous line
J Combine the line where the cursor is and the data of the next line into the same line
u Back, similar to Ctrl+z
[Ctrl]+r Forward, similar to Ctrl+y
. Repeat the previous action. If you want to repeat delete, repeat paste and other actions, just press the decimal point.

Available shortcut keys for switching from general command mode to edit mode

Enter INSERT or REPLACE mode span>
< span class="md-plain">i, I Enter insert mode (Insert mode): i means insert from the current cursor position, I means insert at the first non-space character of the current line
a, A < span class="td-span"> enter Insert mode: a is to insert from the next character where the cursor is currently located, A is to start from the last of the line where the cursor is Insert at the beginning
o, O Enter insert mode (Insert mode): o is to insert a new line in the next line where the cursor is currently located; O is to insert a new line in the previous line where the current cursor is located
r, R Enter Replace mode: r will only replace the character at the cursor once; R will always replace the cursor at Text until ESC is pressed
[Esc] Exit editing mode, Back to the normal command mode

The shortcut keys available for switching from general command mode to command line mode

span>

< /tbody>

Command-line mode storage, leave and other instructions
:w write the edited data to the hard disk file (save)< /span>
:w! If the file attribute is read-only, write the document. However, whether you can write or not depends on your file permissions for the file.
< span class="td-span">:q Leave vi
< span class="md-plain">:q! if modified I did not want to save the file, use it! Do not save the file for forcibly leaving
:wq leave after storage, if it is: wq!, leave after compulsory storage
< span class="td-span">ZZ If the file has not been changed, it will not be saved and left. If the file has been changed, it will be saved and left!
:w filename Save the edited data as another file
: r filename In the edited data, read data from another file . That is, the content of the file filename is added after the line where the cursor is located
:n1,n2 w filename Save the contents of n1 to n2 as filename
:! command Leave vi temporarily to display the result of executing command in command line mode. For example, :! ls /homeyou can view the file information output by ls under /home in vi< /span>
Vim environment changes
:set nu Display the line number, after setting, the line number of the line will be displayed in the prefix of each line
:set nonu In contrast to :set nu, it cancels the line number

“Bird Brother’s Linux Private Kitchen”reading notes

Leave a Comment

Your email address will not be published.