This is not a collection of Vim commands, nor is it a recommendation of Vim plug-ins. It is a look at what can be solved by using Vim’s ideas and skills on the premise of installing no plug-ins, and then think about what plug-ins I need to install to achieve things that cannot be done by relying solely on native ViM
thought
One point command
1. Click the command to repeat the last modification. The last modification can be x, DD, or a series of operations that enter the insert mode until returning to the normal mode
Two. Don’t repeat yourself
Suppose you want to add semicolons to the end of all 15 lines after the current one
- Method 1: Press
A; <Esc>
And then pressj
andA; <Esc>
Repeating over and over again might do the trick, but you get stuck repeating yourself, as you do with a lot of similar code - Method 2: Press
A; <Esc>
And then pressj
and.
As mentioned in the first point, clicking the command can repeat the last change, which is the last change hereA; <Esc>
So according to thej
and.
We can achieve the desired effect, and this operation is also the ideal editing mode, a key movej
, another keystroke operation.
3. Granularity of a modification
- A lot of times we need to undo what we’ve typed and will press
u
Key to cancel - If we have entered ten lines in insert mode this time, maybe you just want to undo the tenth line, then you press
<Esc>
andu
, you will find that all ten lines have been undone. This is a change with too much granularityvscode
It’s different every time<command-z>
It only undoes one line of content, so invim
We need to control the granularity of the last modification by ourselves, and we can press it every time we think the code has no input<Esc>
Falling back to normal mode allows for sharding of change granularity
Use operator commands instead of visual mode whenever possible
import React, { FC, useMemo, useEffect } from 'react'
Let’s say you want to delete the first word hereimport
Method one can be useddaw
Implementation, two ways to usevaw
andd
. In other editors’ mode we usually choose method two, which is to select the area and then do the operation, butvim
You can select the operation and then select the scope. The advantage of selecting method 1 is that it can be used.
Command, you can continue to delete the next word, but using visual mode 2, you will find that it only deletes 7 characters each time because of the firstvaw
andd
The operation isimport
Add a space
The niche model
Operator undetermined mode
- You probably know that there are four modes in Vim: normal mode, insert mode, visual mode, and command line mode, but there are also operator modes to be determined
vim
There is an additional rule that applies to the current line when an operator command is called twice in a row- What is operator undecided mode,
d
Is the delete operator,<
Is the operator to reduce indentation, but we pressdd
Delete a row,<<
I’m going to reduce the indentation by one line, actually we’re pressing the first oned
We enter operator undecided mode, but this mode is short-lived until we receive our next action command and complete the operation, for exampledd
, the firstd
The yes, yes operator, the secondd
It says delete the whole row, so same thingcc
Delete the entire row and insert,yy
I’m just copying the whole row,dap
Is to delete the entire paragraph,gUap
Capitalize the entire paragraph - Operators can be used in conjunction with lookup actions
d/staff<cr>
Can be deleted from the current cursor tostaff
So the previous part - Operator pending mode can be combined with a text object. In operator pending mode,
i
Instead of switching to insert mode and placing the cursor before the character,a
It does not place the cursor after a character, but as part of a text object. becauseaw
Deletes the current word with a space,iw
Delete only the features of the current word,c
Command fit andiw
Such as coordination,d
Commands are suitable for andaw
Such cooperation - Many commands are two or more keystrokes, for example
<C-w> s
Will split the window horizontally, but<C-w>
Just the prefix of the second key, which can be used as a namespace, but they do not activate operator pending mode, because only operators can activate operator pending mode
Ii. Insert-normal mode
1. Sometimes when you want to use a normal command in insert mode, you can switch to insert normal mode without pressing
, < c-o >. Use this command when you want to run a normal command in insert mode and then continue typing immediately. For example, if you want to scroll the screen so that the current row is in the middle of the window for better reading, and then continue to insert, press < C-O >zz. For example, after the current line is finished, you can press < c-O >10j if you want to move 10 lines below
Three. Selection mode
1. Visual mode, the election of a text, then press < C > – g can switch to the choice of mode to enter any visible characters, such as p, the selected text will be deleted, and displays the characters you type p, and switch to the insert mode, but in visual mode, will these characters as a command, p is considered paste
skills
1. Switch with shell
<C-z>
You can makevim
Suspend, enter intoshell
,fg
Available from theshell
Return to thevim
In the
Exit insertion mode, move through the document, and then quickly return to the exit point to continue editing, using the gi command
3. Location marking
{letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} {letter} This is the fastest way I can get back to where I left off the day before
Center the row where the next/last lookup is located
noremap <Leader>n nzz
noremap <Leader>N Nzz
Copy the code
5. Automatically fill in the directory address of the current file
-
cnoremap
%% getcmdtype( ) == ‘:’ ? Expand (‘%:h’).’/’ : ‘%%’ Add this to vIMRC to automatically fill the directory address of the current file with %%
-
Cnoremap represents mapping in command line mode
-
means that the argument on the right side of the mapping command is treated as an expression
-
When we type %% we will get the expression getcmdType () == ‘:’? Expand (‘%:h’).’/’ : ‘%%
-
This expression gets the type of the current command line mode with getcmdType (), where: means command line mode, and expand () expands the wildcard by passing in arguments % and :h, which gets the address of the current buffer and strips the filename
-
Cnoremap
%% expand(‘%:h’).’/’
%% expand(‘%:h’)
Command line search history command
- You can keep your finger on the home line (the HJKL line), but you can also look for entered commands based on entered characters as a filter.
<Up>
Screening,<C-p>
No, such as inputset high
It filters based on what has been entered
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
Copy the code
7. Cancel the function of up and down keys, forcing yourself not to use up and down keys
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
Copy the code
8. Actual and screen lines
- When many split screens are turned on, actual and screen lines appear, that is, one line in the file is displayed as several lines on the screen
j
andk
Will move down or up depending on the actual row, andgj
andgk
It moves down or up by screen row