Vim without plugins/Widgets
Four modes of vim namely General mode,normal mode, Command line mode,Visual mode.
Four modes of vim
General mode: normal mode. You can move the cursor, delete characters or entire lines, and copy and paste file data. Opening vim is to enter this mode, and the switching between the three modes is also transferred here.
Edit mode: In general mode, press any one of
i``I``o`` O
a`` A
r`` R
s`` S
to enter this mode. You can edit the content of the file and press Esc to return to the normal mode.-
i`` I
is insert (in front of the character under the cursor and at the beginning of the line)
-o`` O
is open new line (start a new line below the line where the cursor is and start a new line above the line where the cursor is
-a`` A
is append (after the character under the cursor and at the end of the line where the cursor is)
-s`` S
is to delete (the character where the cursor is and start inserting and the line where the cursor is and start inserting), that is, substitute replacement.
-r`` R
is to replace the character under the cursor and become the replacement modeCommand line mode: Press
:
/
in normal mode?
Any one enters this mode (the meaning of these symbols will be described below). You can find data operations, read, save, replace a lot of characters, leave vim, display line number and other operations, press Esc to return to the general mode.Visual mode: In general mode, press
v`` V
ctr + v
to enter the visual mode, which is equivalent to the normal mode after highlighting the selected text, that is, in this mode, you can arbitrarily select a specific area and be The selected area is highlighted,v
selects the unit: one character;V
is also called the visible line mode, select the unit: line;ctr + v
is also called the visible block mode, select the unit: square ; All three are useful, see below for details.
Mobile
In normal mode:
w
→ to the beginning of the next worde
→ to the end of the next word (words are separated by spaces by default)W
→ to the beginning of the next stringE
→ to the end of the next string (a string refers to a string consisting of numbers, letters, and underscores)B
→ Go to the first character of the previous string. The b
→ “command moves the cursor to the first character of the previous word.
By default, a word is composed of letters, numbers and underscores
If you think words are separated by blank characters, then you need to use uppercase E and W (Chen Hao: Note)
0
→ number zero, to the beginning of the line^
→ Go to the first position of the line that is not a blank character (the so-called blank character is a space, tab, line feed, carriage return, etc.)$
→ Go to the end of the lineg_
→ to the last position of the line that is not a blank character%
→ Go to the other of the pair of brackets where the cursor isgg
→ first lineG
→ last lineh`` j
k`` l
(strongly recommended to use it to move the cursor, but it is not necessary) → you can also use the cursor keys (← ↓ ↑ →). Note: j extends downwards, k extends upward
- ** High frequency usage scenario 1 **: Change a variable name in the line first and move the cursor:
w
andb
,W
andB
(or if the line is too long, use the following Search function) to the target word - ** High frequency usage scenario 2 **: Modify the indent and jump to the beginning of the line
^
- ** High frequency usage scenario 3 **: View the completeness of the function or class or the variable scope
%
- ** High frequency usage scenario 4 **: After splitting the screen, jump to different windows:
ctrl + w + (h or j or k or l)
- ** High-frequency use scene 5 **: Move left (left, top, right)
(h, j, k, l)
- ** High frequency usage scenario 6 **: Delete to the end:
d $
Delete to the beginning:d ^
Tag
Note: ** mark is to find better **, in normal mode:
mx
meaning: mark x, x is the name of mark;'x
meaning: go to the position of x mark
- ** High-frequency usage scenario 1: ** You can see how to define other functions in the function. You want to see how to define them. After you read it, you need to come back. Then mark it first and then jump back.
Syntax related jumps
In normal mode:
gd
meaning: go to definition- Press
[
and thenctrl + d
to jump to #define - Press
[
and thenctrl + i
to jump to functions, variables and #define
** Note **: The language support is not very good, you can try the language used
Quick page turning
In normal mode:
Partner 1 | Partner 2 |
---|---|
ctr + d page down |
ctr + d page up |
ctr + f page forward |
ctr + b page back |
Action operation instruction
In normal mode:
Partner 1 | Partner 2 |
---|---|
d ** d ** elete a character and copy to clipboard |
D has been ** deleted ** from the cursor position to the end of the line |
y ** c ** opy to clipboard |
Y ** Copy ** one line (= yy ) |
s ** s ** ubstitue a character |
S ** Replace ** the line where the cursor is located |
r ** r ** eplace a character |
R * Not commonly used *, which means to enter replacement mode |
c ** c ** hange a character |
C * Not commonly used *, which means ** modify ** the cursor position until the end of the line, the same effect as S rendering |
p ** p ** aste after the cursor |
P ** Paste ** before the cursor position (if you paste a whole line, paste to the previous line) |
u ** u ** ndo a operation |
U one-time ** undo ** all operations on a whole line |
x cut a character |
X * not commonly used *, ** cut ** to the left, ie backspace: delete the character to the left of the cursor |
** `** Search the word under the current cursor ** downward, and jump to the next word when found | # ** Search the word under the current cursor ** upward, jump to the previous word when found |
/ word ** Search the word word ** down to the full text, jump to the first word that matches, if there are multiple, continue to search down and press the n key (in the direction of the original command), up to press the N key. |
? word ** Search the word word ** up the full text, jump to the first word that matches, and if there are multiple, continue to search upwards and press the n key (in the direction of the original command), down to press the N key. |
a ** a ** ppend after the cursor |
A is ** append ** at the end of the line where the cursor is located) |
i ** i ** nsert before the cursor |
I ** insert ** at the beginning of the line where the cursor is located |
o Start a new line below the line under the cursor, open the new world? |
O starts a new line above the line where the cursor is located |
v enters ** v ** isual mode, used to select areas (can cross lines), used to cooperate with other subsequent operations (addition, deletion, and modification) |
v enters visual line mode, used to select some lines To cooperate with other follow-up operations (addition, deletion and modification) |
f ** f ** ind a character after the cursor |
F ** find a character before the cursor position ** |
t ** t ** ill a character tx is the same as fx, the difference is to jump to the front of character x |
T Tx is the same as Fx, the difference is to jump to the character x |
Formed separately
.
Repeat the operation just now~
Convert case
- You can change the case of the first letter of the variable
- You can select a string (variable) in combination with the commands provided below, and then change the case of the entire string (variable). For example: macro definition
=
Auto format
- Use
==
for the current line (double press = twice), or usen ==
for multiple lines (n is a natural number) to automatically indent the next n lines from the current line - Or enter the visual line mode, select some lines and then
=
for formatting, which is equivalent to the code format in the general IDE. - Use
gg = G
to typeset the entire code.
Undo and Redo
u
undo undoes the operation in the previous step. Commands can be combined. For example,Nu
N is any integer, which means undoing the N-step operation. The following is the same.U
restore the current line (that is, undo all operations on the current line at once)ctr + r
control + redo restore the previous operation that was cancelledCTRL-R
back to the previous command
Text replacement
Enter the replacement command in normal mode: : [range] s / pattern / string / [flags]
-pattern is the string to be replaced, which can be expressed by regexp.
-string replaces pattern with string.
-[range] has the following values:
[range] | Meaning |
---|---|
None | The default is the line where the cursor is located |
. |
Current line of cursor |
N |
Line N |
$ |
Last line |
'a |
Mark the line where a (marked with ma before) |
. + 1 |
The line below the current cursor line |
$ -1 |
The penultimate line, you can add or subtract a value to determine the relative line |
22,33 |
Lines 22 to 33 |
1, $ |
Line 1 to last line |
1, . |
line 1 to current line |
., $ |
Current line to last line |
'a,' b |
The line from the mark a to the line from the mark b (Ma and mb have been used to mark before) |
% |
All rows (equivalent to 1, $) |
? str? |
Search up from the current position to find the line of the first str (where str can be any string or regular expression) |
/ str / |
Search down from the current position to find the line of the first str (where str can be any string or regular expression) |
** Note that all the above methods for range can be used to set the relative offset by + and-operations. ** |
-[flags] has the following values:
flags | meaning |
---|---|
g |
Replace all matches (global) within the specified range |
c |
Request user confirmation before replacement |
e |
Ignore errors during execution |
i |
ignore case-insensitive |
None | Only the first match in the specified range is replaced |
** Note: All the above flags can be used in combination. For example, gc means to replace all matching items in the specified range, and the user will be asked to confirm before each replacement. **
Examples
Replace some lines
: 10,20s / from / to / g
replaces lines 10 to 20.: 1, $ s / from / to / g
replaces the contents of the first line to the last line (that is, all text): 1, .s / from / to / g
replaces the content from the first line to the current line.:., $ S / from / to / g
replaces the content from the current line to the last line.: 'a,' bs / from / to / g
replaces the line between the marks a and b (including the line where a and b are located), where a and b are the marks previously made with the m command .
Replace all lines: :% s / from / to / g
Repeat the action
In normal mode, any action can be repeated
Note: N is a number
-Numbers: Nyy
copies N lines down from the current line, Ndd
deletes N lines down from the current line, Ngg
jumps to the Nth line, and dNw
deletes from the current cursor to the first Before N words (does not contain blanks, ie delete N-1 words), yNe
copies from the current cursor to the end of the Nth word (note: yy
= 1yy`` dd
= 1dd
) d $
delete to the end of the line
-Repeat the previous command: N.
(N indicates the number of repetitions)
Block selection
Note: The brackets are optional
In normal mode: [ctr +] v + (h or j or k or l)
- ** High-frequency use scenario 1 **:
[ctr +] v
Select some line headers and then press=
Effect: ** Code format is automatically adjusted ** - ** High frequency usage scenario 2 **:
[ctr +] v
After selecting the head of some lines, pressI
, then press the comment symbol (eg://
) and finally pressESC
: All the selected lines are commented ** Multi-line quick comment ** - ** High frequency usage scenario 3 **:
[ctr +] v
After selecting some line headers, then pressA
, then press the contents of the comment and finally pressESC
(for example:// This is a test Code
) Effect: All the lines at the end of the selected lines are commented on.//This is the test code` ** Multi-line quick comment ** - ** High-frequency use scenario 4 **:
[ctr +] v
select some line header comments (for example://
), then pressd
and finally pressESC
effect: selected All these lines are commented out. ** Multi-line quick delete comments ** - ** High-frequency use scenario 5 **: After selecting certain blocks, press [ctr +] v` and then press the above-mentioned action buttons to achieve regional operation
Powerful combination
Operate a word under the cursor
In normal mode:
Action + Move [+ Number of Repeats]
The combination has already been used a lot in the past, continue here:
Action Operation Command + Range | Effect |
---|---|
cw or c1 or c1w | change from current cursor to word end |
caw | change whole word including current cursor |
dw or d1 or d1w | delete from current cursor to word end |
daw | delete whole word including current cursor |
yw or y1 or y1w | copy from current cursor to word end |
yaw | copy whole word including current cursor |
d / word | delete forward until the former character of the next ‘word’ |
d? word | delete backward until the former character of the last ‘word’ |
Action Operation Command + Range | Effect |
---|---|
dtc | delete until before the next ‘c’ |
dfc | delete until after the next ‘c’ |
Scope + Action Operation Instructions | Effects |
——————— | ———- |
bve or BvE + c / d / y |
manipulate a variable or string |
The above table are high-frequency use scenarios
Autocomplete
Press directly in insert mode: the most commonly used completion
` ctrl + n ctrl + p
`
Smart completion
` ctrl + x // Enter completion mode
`
-Complete line completion CTRL-X`` CTRL-L
-** Complete based on keywords in the current file ** CTRL-X`` CTRL-N
-** Complete according to dictionary ** CTRL-X`` CTRL-K
-Completion of CTRL-X`` CTRL-T
according to thesaurus
-** Complete according to keywords in header file ** CTRL-X`` CTRL-I
-Completion of CTRL-X`` CTRL-]
according to tags
-** Complete file name ** CTRL-X`` CTRL-F
-Completion of macro definition CTRL-X`` CTRL-D
-Completion of vim command CTRL-X`` CTRL-V
-User-defined completion method CTRL-X`` CTRL-U
-** Spelling suggestions ** CTRL-X`` CTRL-S
// For example: an English word
Collapse
In normal mode:
` zo (fold + open) zi (fold + indent) zc (fold + close)
`
Split screen
** Split command **, in normal mode, enter
vs
(Description: Vertically split the screen vertically)sp
(Note: split horizontally splits the screen, which is the default splitting method)
** Screens jump to each other **
ctr + w
and then pressh
orj
ork
orl
- Explanation:
h
: left,j
: down,k
: up,l
: right
** Resize the split window **
ctrl + w
before pressing+
or-
or=
, of course press a number before pressing+
or-
or=
to change the window height,=
is equal The meaning of points. .- In normal mode, enter
: resize -N
or: resize + N
to explicitly specify that the window is reduced or increased by N lines ctrl + w
before pressing<
or>
or=
, of course press a number before pressing<
or>
or=
to change the window width,=
is equal The meaning of points.- Sometimes preview large files and feel that the split screen is too small,
ctrl + w
+T
moves the current window to a new tab.
tab window
Vim has added the function of multi-tab switching since vim7, which is equivalent to multiple windows. Although the previous version also has a multi-file editing function, it is not as convenient as this. Usage in normal mode:
-: tabnew
[++ opt option] [+ cmd] The file creates a new tab for the specified file
-: tabc
closes the current tab or: q
-: tabo
close ** other ** tab
-: tabs
view ** all open ** tabs
-: tabp
previous tab window
-: tabn
next tab window
gt
, gT
can switch directly between tabs. There are many other commands,: help table.
table of Contents
In normal mode:
: Te
displays the current directory in the form of a tab window, then you can switch directories and open a file:! Ls
This is how vim invokes shell commands:! Ls + shell_command
, but it does not display the current directory in the form of a tab window.
Content operation of paired symbols
The following commands can operate on the content within punctuation:
ci'`` ci "
ci (
ci [
ci {
ci <
respectively change the text content of these paired punctuation marksdi'``di"
di (or
dibdi [
di {or
diByi'`` yi "
yi (
yi [
yi {
yi <
separately copy the text content of these paired punctuation marksvi'`` vi "
vi (
vi [
vi {
vi <
respectively select the text content of these paired punctuation markscit`` dit
yit`` vit
separately operate the content between a pair of tags, editing html is very useful
** In addition, if you change the above i
to a
, you can operate the paired punctuation and the content of the paired punctuation at the same time **, for example:
For example, the text to be operated: 111 “222” 333, move the cursor to any character of “222” and enter the command
-di “, the text will become: 111” “333
-If you enter the command da “, the text will become: 111333
Clipboard
1. Simple copy and paste
vim provides 12 clipboards, their names are vim and there are 11 pasteboards, which are 0, 1, 2, …, 9, a, “. If you open the system clipboard, there will be two more + And *. Use the: reg command to view the contents of each pasteboard.
In vim, simply use y
and just copy it to the clipboard of "
, and the same thing as paste with p
is also the content of this clipboard.
2. Copy and paste to the specified clipboard
To copy the content of vim to a pasteboard, enter the normal mode, select the content to be copied, then press " Ny
to complete the copy, where N is the pasteboard number (note that you click the double quotes and then press the pasteboard number Finally, press y), for example, to copy the content to the pasteboard a, select the content and press “ay”.
To paste the contents of a vim pasteboard, you need to exit the editing mode, press " Np
in the normal mode, where N is the pasteboard number. For example, you can press " 5p
to paste the contents of pasteboard No. If you paste it in, you can also press " + p
to paste the content in the system global pasteboard.
3. System Clipboard
To view the clipboard supported by vim, enter : reg
in normal mode
How should it be used to interact with the system clipboard? When you encounter problems, the first thing you are looking for is the help document. The Clipboard is the Clipboard. View help via : h clipboard
The asterisk * and plus sign + pasteboard are system pasteboards. Under Windows, the * and + clipboards are the same. For X11 systems, * the clipboard stores the selected or highlighted content, + the clipboard stores the copied or cut content. Open the clipboard option, you can access + clipboard; open xterm_clipboard, you can access * clipboard. * One function of the clipboard is that the content selected in one window of vim can be taken out in another window of vim.
** Copy to system clipboard **
example:
" * y
" + y
" + Nyy
copy N lines to the system clipboard
Explanation:
Command | Meaning |
---|---|
{Visual} “+ y | copy the selected text into the system clipboard |
“+ y {motion} | copy the text specified by {motion} into the system clipboard |
: [range] yank + | copy the text specified by [range] into the system clipboard |
** Cut to system clipboard **
example:
“+ dd
** Paste from system clipboard to vim **
In normal mode:
" * p
" + p
: put +
Meaning: Ex command puts contents of system clipboard on a new line
In insert mode:
<Cr> +
Meaning: From insert mode (or commandline mode)
“+ p is better than the Ctrl-v command, it can handle the pasting of large blocks of text faster and more reliably, and can also avoid the accumulation of automatic indentation at the beginning of each line when pasting a large amount of text, because Ctrl-v The system caches stream processing, processing the pasted text line by line.
vim encoding
Vim can edit all kinds of character encoding files very well, which of course includes popular Unicode encoding methods such as UCS-2 and UTF-8.
Four character encoding options, encoding, fileencoding, fileencodings, termencoding (for possible values of these options, please refer to Vim online help: help encoding-names, their meanings are as follows:
-** encoding **: The character encoding used internally by Vim
Including Vim’s buffer (buffer), menu text, message text, etc. The default is to choose according to your locale. The user manual recommends changing its value only in .vimrc. In fact, it seems that it only makes sense to change its value in .vimrc. You can use another encoding to edit and save the file. For example, the encoding of your vim is utf-8, and the edited file uses cp936 encoding. Vim will automatically convert the read file into utf-8 (vim can read) Understand the way), and when you write a file, it will automatically switch back to cp936 (the file save encoding).