Table of contents
1.Basic introduction to Vi and Vim
2. Switching between Vi and Vim modes
1.Basic introduction to Vi and Vim
Vi is the most versatile text editor in Unix and Unix-like operating systems .
The Vim editor is a more powerful text editor developed from Vi. You can automatically identify the correctness of the grammar by font color , which facilitates program design . Vim is fully compatible with the Vi editor.
2. Switching between Vi and Vim modes
3.General mode
Use # vi <path> or # vim <path> on the command line to enter the normal mode. In this mode, you can delete, copy, and paste the contents of the file.
Common grammar
grammar | Function description |
yy | Copy the line currently under the cursor |
y “number” y eg:8yy or y8y | Copy a section from the current line to the nth line |
p eg: 8p means paste 8 times | Paste in arrow target row |
u | Undo previous step |
dd | Delete the line currently under cursor |
d "number" d | Delete how many lines after the cursor (inclusive) |
x | Cut a letter, equivalent to del |
X | Cut a letter, equivalent to backspace |
yw (with a space after the word) | Copy a word (the cursor needs to be positioned at the beginning of the word) |
dw (spaces after words are also deleted) | Delete a word (the cursor needs to be positioned at the beginning of the word) |
shift + 6(^) | Move to head of line |
shift + 4($) | move to end of line |
1 + shift + g or gg or H | Move to top of page |
shift + g or G or L | Move to end of page |
Number + shift + g (use set nu to set the line number first) | Move to target row |
r or R | replace |
4.Edit mode
In normal mode, operations such as copying, pasting, and deletion can be performed, but editing functions cannot be performed. You need to type [i, I, o, O, a, A] to enter the edit mode.
When typing these keys, the words [INSERT] or [REPLACE] will appear below the command line, and then you will enter the editing mode. Type [ESC] to exit the editing mode.
Common grammar
button | Function |
i | before current cursor |
I | The line where the cursor is located is at the front |
o | The line next to the current cursor line |
O | The previous line of the current cursor line |
a | after current cursor |
A | The end of the line where the cursor is located |
5. Command mode
In normal mode, press any key [: / ?] to move the cursor to the bottom line.
This mode can provide the action of [searching for data] . Operations such as obtaining, saving, replacing a large number of characters, leaving vi, displaying line numbers, etc. are completed in this mode.
Common grammar
Order | Function |
:w | keep |
:q | quit |
:! | enforce |
/ word to search for | Type n to search down, N to search up |
:noh | Unhighlight |
:set no | Show line number |
: set to nine | Close line number |
:% s / old / new / g | Replace all old matches in the document with new |
:% s / old / new | Replace the first matching old in each line in the document with new |
:s / old / new | Replace the first old matched in the current line with new |
Combination operation:
:wq Save and exit
:q! force quit
:wq! Force save and exit