Chapter 1 Vim's way of solving problems
-
.
The command will repeat the last modification- From entering insert mode to returning to normal mode, the operation in between is considered to be a repeatable modification
-
f{char}
: Find the position where the next specified character appears and jump;
: Repeat search backward,
: Repeat search forward
-
*
: Find the word under the cursor and jumpn
Forward repeat searchN
Reverse repeat search
-
How to replace a word in the document?
① It can be used
%s/old/new/g
, just press enter② Move the cursor to an old and press
*
, then presscw
to modify the word, and then press itn.n.
(compared to ①, it can deal with some positions that may not need to be modified)
The first part of the pattern
Chapter 2 Normal Mode
-
u
Command will undo the latest modification- Entering insert mode to returning to normal mode is considered a modification, so the granularity of undo can be controlled by controlling the use of <Esc>.
- It is best
<Esc>o
to start a new line to better control the granularity of undo - If you press the arrow keys while inserting, a new undo block will be generated!
- ⭐️
<C-r>
redo
-
n<C-a>
Increase the next number by n;n<C-x>
Decrease the next number by n- Vim interprets numbers starting with 0 as octal, such as 007 + 001 = 010
- You can be set
set nrformats=
so that all the figures are as Vim decimal
-
zz
Command to redraw the screen and display the current line in the center of the screen -
ga
Display the character code (decimal, hexadecimal and octal) of the current character at the bottom of the screen
-
Operator + motion command (motion) = operation
-
gg=G
Files in the corresponding language can be automatically indented -
Operator pending mode : the state between calling the operator and inputting the action command, press <Esc> to return to the normal mode
-
R
Enter replacement mode from normal mode- The tab character is represented by a single character in the file, but it occupies the width of several columns on the screen, which is determined by the
tabstop
setting - In the replacement mode, the tab character is treated as a single character (Vscode's Vim will not). To deal with this situation, you can use the
gR
command to enter the virtual replacement mode r
And thegr
command allows to replace a character and immediately return to normal mode
- The tab character is represented by a single character in the file, but it occupies the width of several columns on the screen, which is determined by the
Chapter 3 Insert Mode
-
⭐️ Shortcut delete method in insert mode (also available in bash shell)
<C-h> or Backspace
: Delete the previous character<C-w>
Delete the previous word<C-u>
Delete to the beginning of the line
-
Insert-normal mode
- Press to
<C-o>
enter in insert mode , in this mode, you can execute a normal mode command, after executing it, immediately return to insert mode - ⭐️You
<C-o>zz
can display the current line in the middle of the window in insert mode
- Press to
-
In insert mode,
<C-r>{register}
you can easily paste a few words without returning to normal mode by pressing p- There may be unnecessary line breaks/indents when there are multiple lines of text, you can use it at this time
<C-r><C-p>{register}
, or return to normal mode and press p - For the part of the register, see Part 4
- There may be unnecessary line breaks/indents when there are multiple lines of text, you can use it at this time
-
Expression register : allows us to do some operations and insert the result of the operation directly into the document
- By
=
indicating the use of the expression register - For example
<C-r>=1+1<CR>
,, 2 is displayed at the cursor position
- By
-
<C-v>{code}
Insert any character with character encoding (3 bits)- For example
<C-v>065
, enter A - Use
ga
commands in normal mode to query character encoding - Hexadecimal and digraphs can be queried if they are used
- For example
Chapter 4 Visual Mode
- 3 different visual modes
- Operation character text
v
- Line text
V
- Block text
C-v
- ⭐️You
gv
can repeat the last highlight selection
- Operation character text
- The different modes can be switched by the above trigger key, which can achieve very special effects, such as selecting only the first half of each line
- Press in visual mode to
o
switch the active end of the highlighted selection - The selection area can be used with operators, such as selecting and pressing
c
to cut the selection area and enter the insert mode .
Commands can be used to repeatedly execute commands in visual mode, but they are only useful when running text- For example, to increase the indentation of
V
two lines of code twice, you can select these two lines first , then use>
, and then press.
repeat
- For example, to increase the indentation of
- How to quickly select an area in visual mode:
- Use various commands to move the cursor to change the selection, such as
e
,G
etc. f{char}
Well/
and?
repeat queries and the corresponding may also be used- Can be quickly selected with a text object, such as
vaw
- Use various commands to move the cursor to change the selection, such as
- ⭐️ Clever use of visual mode
- Pressing
r
to replace in the visual mode is amazing! Can quickly replace horizontally and vertically - To modify the words in the same column at the same time, just select and press
c
. When modifying, only the first line will be updated in real time, and the other lines will display the modified results after returning to normal mode - Add the same text at the beginning or end of the selected line, just use the
I
orA
command- ⚠️ in visual mode
i
anda
is no longer acting in the normal mode, and will be treated as part of the text objects
- ⚠️ in visual mode
- Pressing
- Selection mode (some superfluous, basically not used)
- Use the <Cg> switch to delete the selected text in visual mode and replace it with the entered text, and switch to insert mode at the same time, which is equivalent to pressing one
c
(then why don’t I just press c?)
- Use the <Cg> switch to delete the selected text in visual mode and replace it with the entered text, and switch to insert mode at the same time, which is equivalent to pressing one
Chapter 5 Command Line Mode
-
Editor family history: ed -> ex ->vi ->vim, Ex commands (commands executed in command line mode) are inherited from the ex line editor, which can easily handle line editing tasks and
:h ex-cmd-index
view the complete Ex command list -
:
Enter command line mode -
Advantages of Ex commands: Normal mode commands generally operate on the current character or current line, while Ex commands can be executed at any position, and the ability to execute on multiple lines at the same time (wide range of influence and long distance)
-
In the command line mode, you can use it
<C-h> <C-w> <C-u>
to delete quickly,<C-v> <C-k>
insert unusual characters, or<C-r>{register}
paste some text -
⭐️Specify the
[range]
range, you can use line number, position mark or search mode to specify the start and end of the rangerange defaults to this line
- Line number as address:
:index
- If it
:1
will jump to the first line, it:3d
will jump to and delete the third line = 3Gdd
- If it
- Use address as a range:
:{start},{end}
- Address can be line number, location mark or search mode
- Such as
:2,5p
printing lines 2 to 5
- Specify range with highlight selection
- Press in the visual mode
:
, the command line will be filled:'<,'>
, which'<
means the first line of the highlighted area,'>
means the last line - Input outside the visual mode, the
:'<,'>p
last selected content will also be echoed
- Press in the visual mode
- Specify range with pattern
- Such as
:/<html>/,/<\/html>/p
- Such as
- Correct the address with offset
:{address}+n
n
Can be omitted, the default is 1- If
:/<html>/+1,/<\/html>/-1p
the row where the first tag of html is located - Another example
:.,.+3
is the current line and the following 3 lines
- Line number as address:
command | Shorthand | use |
---|---|---|
:[range]delete [x] | d | Delete the line in the specified range [to register x] |
:[range]yank [x] | and | Copy lines in the specified range [to register x] |
:[range]put [x] | Paste the contents of register x after the specified line | |
:[range]copy {address} | t | Copy the lines in the specified range to the line specified by {address} |
:[range]move {address} | m | Move the line in the specified range to the line specified by {address} |
:[range]join | Concatenate rows in the specified range | |
:[range]normal {commands} | Execute normal mode commands {commands} for each line in the specified range | |
:[range]substitute/{pattern}/{string}/{flags} | s | Replace {pattern} in the specified range with {string} |
:[range]global/{pattern}/[cmd] | Execute Ex command on all lines matching {pattern} in the specified range |
- Some commonly used symbols:
symbol | address |
---|---|
1 | The first line of the file |
$ | The last line of the file |
0 | Dummy line, located above the first line of the file |
. | Cursor line |
’m | Line containing position marker m |
’< | Highlight the starting line of the selection |
'> | Highlight the end line of the selection |
% | The entire file (:1, short form of $) |
-
:t
Andyyp
distinction,:t
without the use of registers, the default does not overwrite the contents of the register! -
@:
Repeat the last Ex command -
⭐️How to perform an operation for a large number of rows at the same time? (And countless times. Say byebye)
For example, add a semicolon at the end of each line
- You can execute it on the first line first, select the line that also needs a semicolon in visual mode, press
:
and enter:'<,'>normal .
to achieve a lot of repetition - Can also be executed directly
:%normal A;
For example, comment out the entire file
:%normal i//
- You can execute it on the first line first, select the line that also needs a semicolon in visual mode, press
-
The Ex command can be used for
Tab
automatic completion, and can be used to<C-d>
display the available completion list. Each time you press Tab, it will be replaced with the next item in the list, and if you press<Shift-Tab>
it , it will be replaced in the reverse direction (this one is not very useful after a try, under Windows (The vim press Tab is equivalent to the effect of <Cd> here)- There is also a magical effect. Press <Cd> to also display the parameter list, but there must be a space between the command
-
⭐️The
<C-r><C-w>
word under the cursor will be copied and inserted into the command line- For example: when replacing globally, use search to find old, then modify an old to new, call on new
:%s//<C-r><C-w>/g
to replace globally - To insert a string, use
<C-r><C-a>
- For example: when replacing globally, use search to find old, then modify an old to new, call on new
-
⭐️ Backtracking history command
- Press <Up><Down> to view historical commands, and at the same time support filtering based on current input, which is also suitable for search mode
/
! - You can also use <Cp><Cn> to replace the up and down keys to avoid moving your hand to the small keyboard area, but it does not support filtering. You can customize the mapping item to completely replace <Cp> with <Up>
cnoremap <C-p><Up>
set history=
You can set the number of saved history records
- Press <Up><Down> to view historical commands, and at the same time support filtering based on current input, which is also suitable for search mode
-
Use
|
the command combination Ex::write|!ruby %
, after which you can find this combination of command history -
⭐️Command line window
- enter
- In normal mode: Enter
q:
(I often enter by mistake, I don’t know what it is before...) - In command line mode:
<C-f>
switch to enter, the current half of the input command will be retained q/
Open the command line window of the command history of the search mode, the search prompt is just another form of the command line mode
- In normal mode: Enter
- Use hjkl to move, carriage return represents the execution of this historical Ex command
- Almost all operations of vim can be performed on the content in the command line window, including the Ex command itself
:q
drop out
- enter
-
⭐️ Run Shell commands: add a
!
prefix in command line mode:!{cmd}
-
E.g
:!ls
-
%
Represents the current file name, for example:!ruby %
-
You can use vim to
:shell
start an interactive shell session, and then you can use this shell freely until you enter exit to return to Vim- A more convenient way is to press
<C-z>
to suspend vim first, then you canjobs
view it and use thefg
command to resume foreground operations
- A more convenient way is to press
-
:read !{cmd}
Will redirect the output of cmd to the buffer (the area currently being edited):[range]write !{cmd}
The content specified by range will be used as the standard input of cmd, the default is the entire file-
Such as:
:write !sh
the role of the command is to execute each line of the command in the current buffer in the shell -
Note
!
the position:write ! {cmd}
=:write !{cmd}
, but ⚠️:write! sh
will force the contents of the buffer to be overwritten in the sh file!
-
-
-
Filter :
:[range]!{cmd}
Use the content of range as the standard input of cmd, and then replace the range specified by range with the output of cmd-that is, the text specified by range will be filtered by cmd- We can
!{motion}
switch to the command line mode with operators, and preset the range covered by the specified {motion} on the command line- For example
!G
, if you press , Vim will open the command line and fill in:.,$!
, so you don’t have to worry about the entire range
- For example
- We can