Read the book Practical Vim this week, the author said not to read all of them, because there are many operations you already know, it will waste time. But how can I know whether I know it or not without looking at it? So I still read all of it. I sorted out and recorded what I didn’t know

Manipulate the contained text

Operations on text generally have

  • y– copy
  • d– delete
  • c– Change to insert mode after deletion
  • v– Region selection

We call these operations op, and summarize the key combination for the text contained by (), {}, [], ‘/””, and HTML Tag, in any place containing the text

  • (a)
    • op + ib– operation(a)The text within
    • op + ab– Operations include(a)Inside the text
  • {}
    • op + iB– operation{}The text within
    • op + aB– Operations include{}Inside the text
  • []
    • op + i[i]– operation[]The text within
    • op + a[a]– Operations include[]Inside the text
  • ' '""
    • op + i'i"– operation'"The text within
    • op + a'a"– Operations include'"Inside the text
    • op + it– operationThe text within
    • op + at– Operations includeInside the text

How to operate a word quickly, a word here means a string of consecutive Chinese or English words, or multiple words connected by _. We can directly manipulate the entire word using op + iw anywhere in the word. In other words, if the symbol above includes a word, we can directly use op + iw to manipulate the text inside the symbol, or op + AW to manipulate the text inside the symbol

Can a don’t two

A lot of things can be done with two keystrokes, and other things can be done with one keystroke

As you probably know, you can use I/A if you jump to the front/back of A line and change to Insert mode, but stop if you’re still using ^ I / $A

If you want to start at the beginning of the cursor and delete to the end, you might use c$or D $, but you can go straight to C or D. Still using CC? Fast try S

use;.

; And. Are both used to help us repeat operations. Let’s get a feel for two examples

var foo = 1
var bar = a
var foobar = foo + bar
Copy the code

How to quickly put a semicolon at the end of each line, first assume that our cursor is on the first line. Then do the following

A; + Esc + j.j., of course visual block feels faster (Ctrl + v + jj$A; + Esc)

Let’s do another example

var hi = 'Hi! my name is '+name+', and I love '+hobby;
Copy the code

Are you a code freak? If I see a file where the code is out of order, I’ll gg=G. But that doesn’t change, so how can you quickly add a space to the + side of something like this? That’s a combination; And:, suppose we do the following with our cursor at the beginning of the line

F +s␣+␣ + Esc + .; .

The first group is to find the +, and then delete the input space plus the sign space, go back to normal mode, use; Repeat the search work with. Repeat the input operation

But if you want to get the best out of., you have to maximize the change. What do you mean

var obj = {
  name: 'jason',
  hobby: 'basketball'
};
Copy the code

Now, assuming the cursor is on the last L of the Backetball, we want to delete the Basketball, and we have a number of ways to do that

  • dbxdblDelete to the head of the word, and leavelwithxdelete
  • bdwbBack to the head of the word, withdwdelete
  • diw-Just at the beginning, just cut out a word

All three buttons have the same effect, but the. Function is different. For the first case. = x, for the second. = dw, and for the third. = diw. When we want to delete the name attribute value again, use the. Operation. The first method can delete only the letter where the cursor is located, the second method can delete only the letter where the cursor is located to the end of the word, meaning that you can delete the whole Jason at the beginning of the word, and the last method can delete the whole word no matter where the cursor is located. That’s why we’re maximizing change

(Of course we can change to Insert Mode, then Ctrl + W to delete a word forward)

Search and replace

We use this a lot

What if I just change the search words in lines 5 to 12

But in fact, there may be very few words to be replaced in line 5 ~ 12. After typing that command, MAYBE I can also use other methods to complete my needs. For example, we are on line 5, the word to be replaced, and then

*cw + replace + Esc + n.n.n.

* The word where the cursor is located is equivalent to using /search or? Search, then delete the word you want to replace, then return normal mode, n is to find the next,. Is a repeat input operation

Or combine commands with *

* CW + replace + Esc then enter the command

Will copy the current word of our cursor into the command

And I also just discovered, black technology ah! You can add and subtract numbers where your cursor is. If the cursor position is not a number, do something to the first number after the cursor and jump to the number. What are the actual usage scenarios? There are too many front-end application scenarios, and some length units are often manipulated when writing the CSS. Adding 3px would be convenient. 3 + Ctrl + A, of course, under Windows will be all, the solution is here

You can use Ctrl + R0 to paste in insert mode without exiting normal mode

Many window

Often used more window coding together, there are some things to control or reference, but the small screen can’t afford to hurt ah, you can use the Ctrl + w | to maximize the current window, use Ctrl + w = average all Windows

register

What was found? Do you find many similar operations that you seem to have done

Whatever you copy outside with Ctrl + C goes into the “* “register

“” is the nameless register, where any operation that does not specify a register is entered

“0 is called the extraction register, and when you copy something with Y, it goes into the extraction register, but when you copy something with C, D, S, x, it never goes into the extraction register. So you can use “0P “to make sure you paste what you copied from general y last time

The named register, that is, “A to” Z, is added before the operation to store the contents in the specified register. For example, “adiw is the word where the cursor is placed in register A, which can be copied by “aP”

With this in mind, a common scenario can be easily solved

var collection = getCollection();
process(somethingElse, target);
Copy the code

We want to replace somethingElse with Collection, and when we copy the collection with yiw, go to somethingElse, dw delete it, press P and find somethingElse is back, Because dW doesn’t just delete a word, it puts it in the nameless register, and simply using P takes something out of the nameless register.

And we know from what we just saw, that using “0P “will take out the positive solution, because only things that are copied in” y “will go into register “0. You can also delete the word without dw, select the word with ve, and then p, because the nameless register “” has not been replaced. Another way is to delete the word cw and change it to Insert mode, then Ctrl + R0, Ctrl + R can access the register in Insert mode, so with the register name, you can remove the item from the register

Registers record not only the results of operations, but also the steps taken. Use q plus the register name to start recording the steps, enter Q to stop recording, use @ plus the register name to repeat the steps. This is used when you have a lot of repetition

conclusion

This article is just a summary of some tips and tricks that I didn’t know about to solve the development pain points. Hopefully there are some tips that you don’t know about, and you can leave a comment below for more and better tips. Vim is a book that can’t be summarized, and its power lies in customization. Enjoy it for more