Table of contents
vi searches for characters in text
The vi editor quickly jumps to the first line and the best line
Powerful functions, simple interface
Various suffix files can be modified (now it feels like common suffix files can be used, py, sh, txt, csv, sql)
How to use vi
#vi filename
Simply add vi to the file name that needs to be edited.
Three command modes of vi
Command (command) mode, used to enter commands;
Insert (insert) mode, used to insert text;
Visual mode for highlighting and selecting text for visualization
File saving and exiting
Command mode is the default mode of vi or vim. If we are in other command modes, we need to switch over by ESC key.
When we press the ESC key and then enter the : number, vi will wait for us to enter the command at the bottom of the screen;
:w save;
:w filename save as filename;
:wq! Save and exit;
:wq! filename Note: Exit after saving with filename as the file name;
:q! Exit without saving;
:x should save and exit, the function is the same as :wq!
cursor movement
When we press ESC to enter Command mode, we can use the following keys to move the cursor;
j move down one line;
k moves up one line;
h move one character to the left;
l move one character to the right;
ctrl+b move up one screen;
ctrl+f move down one screen;
up arrow move up;
down arrow move down;
Left Arrow Move left;
right arrow move right;
When we edit a file, for the j, k, l, and h keys, we can also add numbers in front of these action commands, such as 3j , which means moving down 3 lines.
Jump to the specified line, colon + line number, press enter, for example, jumping to line 240 is: 240 press enter. Another method is line number + G, for example, 230G jumps to line 230.
Ctrl + e Scroll down one line
Ctrl + y Scroll up one line
Ctrl + d Scroll down half a screen
Ctrl + u Scroll up half a screen
Ctrl + f Scroll down one screen
Ctrl + b Scroll up one screen
Multi-line deletion and copying in VI
method one:
Single row deletion, : 1 (row to be deleted) d
Multiline delete, :1,10d
Method Two:
The line where the cursor is located, dd
N lines below the line where the cursor is located, Ndd
method 1:
Put the cursor on line 6,
Input: 2yy
Put the cursor on line 9,
Enter: p
This method is suitable for copying a small number of rows of text, copy the data of the 2 rows below the 6th row (including), and put it under the 9th row.
Method 2:
Enter in command line mode
6,9 co 12
Copy the content between lines 6 to 9 to the end of line 12.
Method 3:
Sometimes when you don't want to bother to see how many lines or copy a large number of lines, you can use labels instead
Move the cursor to the start line, enter ma
Move the cursor to the end line, enter mb
Move the cursor to the paste line, enter mc
Then: 'a,'b co 'c change co to m to cut
To delete multiple lines, you can use: 5, 9 de
Display line numbers or cancel line number display
show line number
:set number
:set no
Cancel line number display
:set no!
vi searches for characters in text
1. Enter "/string" in command mode, such as "/Section 3".
2. To find the next one, press "n".
To search upwards from the current cursor position, use the following command:
/pattern Enter
Among them, pattern represents the specific sequence of characters to search for.
To search down from the current cursor position, use the following command:
?pattern Enter
After pressing the Enter key, vi will search for the specified pattern and position the cursor at the first character of the pattern.
vi to find and replace
You can use the :s command in vi/vim to replace strings.
:s/vivian/sky/ Replace the first vivian in the current line with sky
:s/vivian/sky/g Replace all vivian in the current line with sky
The vi editor quickly jumps to the first line and the best line
1. Jump to the first line of the file in the vi editor:
a Enter: 0 or: 1 and press Enter
b keyboard press lowercase gg
2. The vi editor jumps to the last line of the file:
a Input: $ Enter
b keyboard press uppercase G
c keyboard press shift + g
vi undoes the last operation
u Undo the operation of the previous step
Ctrl+r Restore the operation of the previous step that was undone