How to Use Vim
Guide |
Vim is a text editor developed from vi. Code Completion, compilation, error jump, and other convenient programming functions are particularly rich and widely used by programmers. In parallel with Emacs, Emacs becomes the favorite text editor for Unix-like users. |
1. Introduction to vi and vim
(1) based on graphical interfaces, such as gedit and geany;
(2) Based on command lines, such as nano, vi, and joe;
The text editor of each command line interface has different shortcut keys. For example, save nano As CTRL + W and save vim as w. The text editor based on command line is necessary, linux tty1 ~ Tty6 is a command line interface that cannot open the text editor of the graphic interface. vi is a text editor installed by default for every Linux Distribution, and the default editor of many software is vi, therefore, it is necessary to learn vi. vim is an advanced version of vi, vi is a text editor, and vim should be said to be a program editor, because it can be like a general IDE, load different syntax highlighting Based on the file name, that is, color differentiation. For example, a java file will follow the java syntax highlighting;
Next we will compile a "hello world" java program to see the vim syntax highlighted;
We can see that vim has a good syntax check function, because the keywords are marked with different colors;
Vi has three modes:
(1) General mode: Enter the default mode of vi, which can be copied, pasted, or deleted; (2) edit mode: Press 'I' in general mode to enter; (3) command line mode: In general mode, press ":", '? ','/'To enter;
NOTE: If vim is installed, the vim editor is used after vi is input. After alias is input, "alias vi = 'vim '" is displayed '";
Ii. vi/vim buttons
Common buttons in "Normal Mode:
Common buttons in "Normal Mode" |
Button description |
H |
Left |
J |
Down |
K |
Up |
L |
Right |
20 h |
20 characters to the left |
20j |
20 rows down |
20 k |
20 rows above |
20l |
20 characters to the right |
Page Up |
Page up |
Page Down |
Flip down |
Home |
Move the cursor to the leftmost end of the current row |
End |
Move the cursor to the rightmost end of the current flight. |
N [space] |
Move the cursor n characters to the right |
N [Enter] |
Cursor down N rows |
G |
Move cursor to the last line |
NG |
Move cursor to line n |
Gg or 1G |
Move cursor to the first line |
/Word |
Search for word |
? Word |
Search for word |
N |
If it is used after/word, it means to continue searching; If yes? If the word is used later, the search continues up;
|
N |
If it is used after/word, it indicates to continue searching up; If yes? If the word is used later, the search continues; |
: N, m/old/new/g |
From row n to Row m, replace old with new |
: N, $ s/old/new/g |
From row n to row n, replace old with new |
U |
Similar to CTRL + Z in windows, the previous step |
X |
Delete one character from the backend |
X |
Delete one character forward |
3x |
Delete 3 characters from the backend |
3X |
Delete 3 characters forward |
Dd |
Delete current row |
3dd |
Delete the three rows starting from the current row |
Yy |
Copy current row |
3yy |
Copy the three rows starting from the current row |
P |
Paste to the next line of the current cursor |
P |
Paste to the previous line of the current cursor |
CTRL + r |
Redo the previous operation |
Common buttons for "command line mode:
Common buttons in "command line mode" |
Button description |
: W |
Save |
: Q |
Exit vim |
: Wq |
Save and exit |
: Wq! |
(If the permission can be changed) force save and exit |
: Q! |
Exit directly without saving |
: W filename |
Save as filename |
: N, m w filename |
Save row n to Row m as filename |
: Set nu |
Show row number |
: Set nonu |
Do not show row number |
:! Command |
Temporarily exit vim and execute command. After the command is executed, enter vim |
: R filename |
Reads data from the filename file into the current file. |
: Set all |
Displays the current vim environment Configuration |
Iii. vim recovery mechanism
When vim is editing a file,. file. swp exists at the same time. This file is used for temporary storage to help restore the file content. When vim closes the file normally, the. filename. swp file will disappear.
The figure shows two reasons for this interface:
(1) When multiple users edit this file simultaneously: Response. swp, the interface will appear; (2) the file is not properly closed;
The last line in the figure shows the actions we can take at this time:
(1) o: Open it in read-only mode; (2) R: Restore, that is, it is restored from the swp file. However, the swp file will not be deleted as vim is closed and needs to be deleted manually; (3) d: Delete the swp file; (4) q: Exit vim;
Iv. vim record and default configuration file
The/etc/vimrc file is the global vim configuration file.
1. if we use the xiazdong account to use vim, it will appear in/home/xiazdong. viminfo file, which is used as the vim log to record the files opened and operations performed by users using vim. 2. if the xiazdong account wants to configure the default vim environment, you can create it in/home/xiazdong. vimrc, and configure some features, such as displaying row numbers and syntax checks;
The following configurations are generally used:
set hlsearchset backspace=2set autoindentset nuset rulerset showmodeset bg=darksyntax on
V. Visual Block Functions
The Visual Block function can be used for Block replication.
(1) CTRL + v: Start copying blocks; (3) y: Copy blocks; (4) p: paste blocks; (5) d: delete Blocks
6. Editing Multiple files at the same time
The advantage of Multi-file editing is that some content of a file can be yy and p to another file.
Vim file1 file2 can be used to edit two files at the same time, but only one file content will appear on the screen at the same time, you need to switch.
(1): n: Switch down; (2): N: switch up; (3): files: list all files currently edited;
7. Multi-Window editing
For example, multiple files can be displayed on one interface.
(1): sp: Open the current file; (2): sp filename: Open the filename file; (3) CTRL + w + fill: Switch the cursor to the next window; (4) CTRL + w + ←: Switch the cursor to the previous window; (5): q: Close the window where the cursor is located;
8. coding problems
Garbled characters may occur. Solution: ensure that the character encoding of the terminal is consistent with that of the file! Tty1 ~ Tty6 does not support displaying Chinese characters. Therefore ~ Garbled characters are required to display Chinese Characters in tty6, and character encoding of the terminal is set.
Encoding and conversion command: The new encoding file filename-o newfilename of the original encoding-t file of the iconviconv-f file. For example, there is a big5 encoded file: 1. big5, the file to be converted to utf8: 2. utf8, then iconv-f big5-t utf8 1. big5-o 2. utf8