Recently, you need to develop a shell script on Linux to summarize how the Linux VI editor is used.
1. Start VI
VI filename
If the file exists, open and display the content, or if the file does not exist, create a new file the first time you save it.
2. Three Modes of VI:
1) Command mode, move cursor, modify, delete, copy, search
2) Insert mode, through AIO entry, can modify the file content
3) search content, by: Enter, you can exit, save, find and replace
3. Move the cursor
1) H,j,k,l left and right, or use the arrow keys
2) Word move W next word b previous Word
3) Sentence movement (previous sentence) next sentence
4) Paragraph move {Previous paragraph} next paragraph
4. Modify, delete, copy in command mode
|
Change |
Delete |
Yank (copy) |
Line |
Cc |
Dd |
Yy |
Word |
cw |
Dw |
yw |
Letter |
Cl |
Dl |
Yl |
5. Undo Operation
U undo the last modification
Ctrl+r Cancel the last Undo
U Undo All the changes
6. Find operations
1)/text Backward Search
2) Text forward search
3) n Search for the next same content
7. Paste operation
1) Paste the copied, modified or deleted data with P
2) for line, p paste on row, p paste under row
3) for characters, p is pasted to the right of the character, p is pasted to the left of the character
8. Some tricks in command mode
1) DTC removes all characters from cursor to C
2) RC replaces the character under the cursor with the C
3) DW Delete the word that is located at the cursor
4) 5DD Delete 5 rows of data
5) 5yy copy 5 rows of data
6) 5x Delete 5 characters
7) R enters the replacement state, ESC exits
9. Save and exit in extended mode
: W Save
: Q exit
: Wq Save
: w! Force Save
: q! Force exit
10, the document inside the location
1) G jump to the last line of the file
2) 1G jump to the first line of the file
3) Ctrl d scroll down half screen
4) CTRL u scroll up half screen
11. Screen Positioning
1) H jump to the first line of the screen
2) M jump to the middle line of the screen
3) L jump to the last line of the screen
4) Z+enter make the current line the first line of the screen display
5) Z makes the current line the last line on the screen
12. Find and Bulk replace
1) Search by sed: s/i/-/g
2) Find the current line by default
3): 1,10 1-10 rows to find: 1,10s/i/-/g
4): 1,$ to the entire file look up: 1, $s/i/-/g
13. Read and save
1): R file reads the contents of another file into the current document
2): 1,20 W File writes the contents of 1-20 rows to a new file
3): 1,$ W file writes the entire document to a new files
4): 1,20 w >>file append 1-20 rows of data to file
14. Setting
1): Set displays common configuration options
2): Set all Show all configuration options
3): Set number setting line numbers
Use of the Linux VI Editor