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: PressA; <Esc>And then pressjandA; <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: PressA; <Esc>And then pressjand.As mentioned in the first point, clicking the command can repeat the last change, which is the last change hereA; <Esc>So according to thejand.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

  1. A lot of times we need to undo what we’ve typed and will pressuKey to cancel
  2. 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 granularityvscodeIt’s different every time<command-z>It only undoes one line of content, so invimWe 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

  1. import React, { FC, useMemo, useEffect } from 'react'Let’s say you want to delete the first word hereimportMethod one can be useddawImplementation, two ways to usevawandd. In other editors’ mode we usually choose method two, which is to select the area and then do the operation, butvimYou 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 firstvawanddThe operation isimportAdd a space
The niche model

Operator undetermined mode

  1. 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
  2. vimThere is an additional rule that applies to the current line when an operator command is called twice in a row
  3. What is operator undecided mode,dIs the delete operator,<Is the operator to reduce indentation, but we pressddDelete a row,<<I’m going to reduce the indentation by one line, actually we’re pressing the first onedWe enter operator undecided mode, but this mode is short-lived until we receive our next action command and complete the operation, for exampledd, the firstdThe yes, yes operator, the seconddIt says delete the whole row, so same thingccDelete the entire row and insert,yyI’m just copying the whole row,dapIs to delete the entire paragraph,gUapCapitalize the entire paragraph
  4. Operators can be used in conjunction with lookup actionsd/staff<cr>Can be deleted from the current cursor tostaffSo the previous part
  5. Operator pending mode can be combined with a text object. In operator pending mode,iInstead of switching to insert mode and placing the cursor before the character,aIt does not place the cursor after a character, but as part of a text object. becauseawDeletes the current word with a space,iwDelete only the features of the current word,cCommand fit andiwSuch as coordination,dCommands are suitable for andawSuch cooperation
  6. Many commands are two or more keystrokes, for example<C-w> sWill 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

  1. <C-z>You can makevimSuspend, enter intoshell,fgAvailable from theshellReturn to thevimIn 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

  1. cnoremap

    %% getcmdtype( ) == ‘:’ ? Expand (‘%:h’).’/’ : ‘%%’ Add this to vIMRC to automatically fill the directory address of the current file with %%

  2. Cnoremap represents mapping in command line mode


  3. means that the argument on the right side of the mapping command is treated as an expression

  4. When we type %% we will get the expression getcmdType () == ‘:’? Expand (‘%:h’).’/’ : ‘%%

  5. 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

  6. Cnoremap

    %% expand(‘%:h’).’/’

    %% expand(‘%:h’)

Command line search history command

  1. 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 highIt 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

  1. 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
  2. jandkWill move down or up depending on the actual row, andgjandgkIt moves down or up by screen row