Guide |
Vim is a text editor developed from VI. Code completion, compilation and error jumps and other convenient programming features are particularly rich, is widely used in programmers, and Emacs parallel to become UNIX-like system users favorite text editor. |
I. VI, VIM INTRODUCTION
(1) Based on graphical interface, such as Gedit, geany, etc.;
(2) based on the command line, such as Nano, VI, Joe, etc.;
Each text editor of the command line interface has its own different shortcut keys, for example, nano is saved as CTRL + W, vim is saved as: w;
A text editor based on the command line is necessary. Linux's tty1 ~ tty6 are all command line interfaces, and they cannot open the text editor of the graphical interface;
vi is a text editor installed by default in every Linux Distribution, and the default editor of many software is vi, so 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 load different syntax highlighting according to the file name like a general IDE, that is, color distinction, such as java files will follow java syntax highlighting;
Below we will write a "hello world" java program to see the syntax highlighting of vim;
It can be seen 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 perform operations such as copying, pasting, and deleting;
(2) Edit mode: press ‘i’ from the general mode to enter;
(3) Command line mode: press “:”, ‘?’, ‘/’ From the general mode to enter;
Note: If vim is installed and the vim editor is used after entering vi, we see "alias vi =‘ vim ’” after entering alias;
Second, Vi/vim button
commonly used keys for "General mode":
Common keys for "general mode" |
key meaning |
H |
Left |
J |
Down |
K |
Up |
L |
Right |
10K |
20 characters to the left |
20j |
20 Rows Down |
20k |
Up to 20 rows |
20l |
20 characters to the right |
Page up |
Page UP |
Page down |
Page Down |
Home |
Cursor moves to the leftmost end of the current line |
End |
Cursor moves to the far right of the current navigation |
n [Space] |
The cursor moves n characters to the right |
N[enter] |
Cursor down n rows |
G |
Move the cursor to the last line |
NG |
Move cursor to nth row |
GG or 1G |
Move cursor to First line |
/word |
Find word down |
? word |
Find Word Up |
N |
If it is used after/word, it means to continue searching downward; If it is used after word, it means to continue looking up; |
N |
If it is used after/word, it means to continue looking up; If it is used after word, it means to continue looking down; |
: n,m/old/new/g |
From line N to line M, replace old with new |
: N, $s/old/new/g |
From Nth to last row, replace old with new |
U |
Similar to CTRL + Z in Windows, the previous step |
X |
Remove one character backwards |
X |
Delete a character forward |
3x |
Remove 3 characters backwards |
3X |
Delete 3 characters in a forward direction |
Dd |
Delete When moving forward |
3dd |
Delete 3 rows from the beginning of the current line |
Yy |
Copy when moving forward |
3yy |
Copy 3 rows from the beginning of the current line |
P |
Paste to the next line of the current cursor |
P |
Paste to the previous line of the current cursor |
Ctrl+r |
Redo Previous Action |
common keys for command line mode:
Common keys for "command line mode" |
key meaning |
: W |
Save |
: Q |
Exit vim |
: Wq |
Save and exit |
: wq! |
Force save and exit (in case you can convert permissions) |
: q! |
Exit directly without saving |
: w filename |
Save As FileName |
: n,m w filename |
Save nth Row to line m as filename |
: Set Nu |
Show line Numbers |
: Set Nonu |
Do not display line numbers |
:! Command |
Leave vim temporarily, execute command, and then enter VIM |
: R filename |
Reads the data from the filename file into the current file |
: Set All |
Displays the current VIM environment configuration |
Iii. The recovery mechanism of vim
When VIM is editing a file, it will exist simultaneously. FILE.SWP, this file is used for staging, to help recover the contents of the file, and when vim closes the file normally, the. filename.swp file disappears.
We see two reasons why this interface appears in the diagram:
(1) Multiple people edit this file at the same time: Because Linux is a multi-user operating system, two people may log in and edit this file at the same time. If A enters the system and starts editing the 1.txt file, .1.txt.swp will appear. , When A has n’t finished editing but B wants to edit it, because .1.txt.swp exists in the directory of this file, the interface will appear;
(2) Abnormally close the file;
The last line in the figure illustrates several actions we can take at this time:
(1) o: Open in read-only mode;
(2) R: restore, that is, restore from the swp file, but the swp file will not be deleted when vim is closed, you need to delete it manually;
(3) d: delete the swp file;
(4) q: exit vim;
Four, vim records and default configuration files
The / etc / vimrc file is a global vim configuration file.
1. If we use vim with a xiazdong account, the .viminfo file will appear in / home / xiazdong. This file is used as a vim log to record what files the user opened and used vim to do;
2. If the xiazdong account wants to configure the default environment of vim, you can create .vimrc in / home / xiazdong and configure some features, such as display line number, syntax check, etc .;
Generally use the following configuration:
set hlsearch
set backspace = 2
set autoindent
set nu
set ruler
set showmode
set bg = dark
syntax on
Five, Visual Block function
The Visual Block function can be used for block copying.
(1) CTRL + v: start copying blocks;
(3) y: copy block;
(4) p: paste the block;
(5) d: delete the block
Six, edit multiple files at the same time
The advantage of multi-file editing is to be able to transfer certain contents of one file to another file.
vim file1 file2 can 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;
Seven, multi-window editing
For example, an interface can display the contents of multiple files.
(1): sp: open the current file;
(2): sp filename: open the filename file;
(3) CTRL + w + ↓: The cursor switches to the next window;
(4) CTRL + w + ↑: The cursor switches to the previous window;
(5): q: close the window where the cursor is located;
Eight, coding problems
There may be garbled characters, the solution: keep the character encoding of the terminal consistent with the character encoding of the file! tty1 ~ tty6 does not support displaying Chinese, so garbled characters will appear when displaying Chinese in tty1 ~ tty6, set the character encoding of the terminal.
Code conversion command: iconv
iconv -f file's original encoding -t file's new encoding filename -o newfilename
For example, there is a big5 encoded file: 1.big5, to be converted to utf8 file: 2.utf8, then iconv -f big5 -t utf8 1.big5 -o 2.utf8
How to use Vim