VI copy and paste command
The VI editor has three modes: Command mode, input mode, and last line mode. It is very important to master these three modes:
Command mode: After VI is started, the command mode is entered by default. From this mode, you can switch to the other two modes by using commands, in any mode, you can press the [ESC] key to return to the command mode. Enter the subtitle "I" in command mode to enter the VI input mode to edit the file.
Input mode: In this mode, you can edit, modify, and input files. In the last line of the editor, "-- insert --" is displayed, indicating that vi enters the input mode. When modifying input and other operations, we need to save the file. At this time, we need to first return to the command mode and save it in the last line mode.
Last line mode: Enter ":" In command mode to enter this mode. There are many useful commands in last line mode.
4. Edit operations
Enter input mode command
I insert command a additional command o open command C modify command
R replace command s replace command ESC exit command
Operation in input mode
Home cursor to beginning of Line
End cursor to end of line
Page up and page down
Delect deletes the character at the cursor position
Delete operation (used in command mode)
X delete a single character at the cursor
Dd deletes the row where the cursor is located.
DW deletes all characters from the current character to the end of a word including Spaces
# X for example, 3x Delete the three characters to the right of the cursor
# Dd For example, 3DD deletes three lines of text from the current row
Cancel operation
U command to cancel the last operation, you can use multiple times to restore the original operation
U cancel all operations
CTRL + R can be used to restore operations using the U command
Copy operation
Run the YY command to copy the entire line to the VI buffer.
YW copies the content from the current cursor position to the end of the word to the VI cache, which is equivalent to copying a word.
Y $ copy the cursor to the cache area at the end of the row
Y ^ copy the cursor to the cache area from the beginning of the line
# Yy. For example, 5yy means copying 5 rows.
# YW: for example, 2yw means copying two words.
If you want to copy the content between Row M and row N, you can enter m in the last row mode. For example, NY: 3, 5y copies the content from row 3 to row 5 to the cache area.
5. search and replace
The search and replacement functions of VI are mainly completed in the last line mode:
Top-down search
/The character seek to be searched./indicates that the search starts from the position where the cursor is located, for example:/work
Bottom-up search
? For example,/work
Replace
: S/old/new replace the old
: S/old/new/g replace all existing old values in the row with new.
: #,# S/old/new/g replace the old value from row # To row # With new
: % S/old/new/g replace the old
If the replacement range is large, add a C command at the end of all commands to force each replacement to be confirmed by the user. For example: s/old/new/C or S/old/new/GC
6. Restore Files
When editing a file, VI will generate a temporary file that starts with. And ends with. SWP. Normally, the file is automatically deleted. If the file is unexpectedly exited, such as a sudden power failure, the file will not be deleted. we can select the command to process it the next time we edit it:
O read-only access without changing the File Content
E. Continue to edit the file and do not restore the content saved in the. SWP file.
R restores the content of the file not saved after the last Edit
Q: Exit vi
D. Delete the. SWP file.
Or use the Vi-r file name to restore unsaved content.
In the GUI:
(1) You can press I to enter the insert mode.
(2) drag the cursor to select the content to be pasted and press the left mouse button to copy the selected block to the buffer zone.
(3) move the cursor to the position to be pasted, and press the middle mouse button (you can press both the left and right buttons) to paste the buffer content.
In a plain text terminal:
(1) Select the text block and use V to enter the visual mode. move the cursor key to select the content.
(2) copy the selected block to the buffer, use y; copy the entire row, and use YY
(3) Cut the selected block to the buffer, use d; cut the entire row with dd
(4) paste the content in the buffer, and use P
Open the second file in the same editing window and use: SP [filename]
Switch between multiple editing files and press Ctrl + W.
Add a number to the front of the command to indicate the number of repetitions, and add a letter to indicate the name of the buffer used.
For help, use: Help [content or command].
VI copy and paste command