Vim Practical Tips (1)


1. Know.The command

.Command role

  • == Repeat the previous modification ==. (see:h .)

Application, for example,

  • xThe == command will delete the character == under the cursor.Will have == deduplicates the character == under the cursor.

Enter u several times to undo the changes.

  • If you run the dd command to delete a whole line, using the. Command == deletes the current line ==.

  • >G == Adds the indent level == from the current line to the end of the document, which is repeated with.

  • Every time I enters insert mode, Vim records every keystroke until it returns to normal mode, making such a change before using it. Command, which will re-execute all of these keystrokes.

.A command is a tiny macromacro


Don’t repeat yourself

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

Add a semicolon to the end of the line:

  • First,$Move to the end of the line, and thenaEdit, enter a semicolon,EscTo complete the change
  • The remaining two lines can be used twicej$.
  • .meansa; <Esc>We have three bonds on top of one bond

Is there room for improvement?

  • You can useAInstead of$a. = =A$aEncapsulated as a button operation. = =
  • Then enter the semicolon and pressEsc.
  • Then you can use itj.Let’s do it again.

Kill two birds with one stone

Compound command Equivalent long command role
C c$ Delete to the end of the line and enter insert mode
s cl Deletes the current character and enters insert mode
S ^C Deletes the current line and enters insert mode

3. Take a step back

Requirement: Add a space before and after the plus sign in JavaScript string concatenation.

var foo = "method("+argument1+","+argument2+")";
Copy the code

To change to:

var foo = "method(" + argument1 + "," + argument2 + ")";
Copy the code
  • f+Find a plus sign

Note: f{char} looks for the next position where the specified character will occur

  • S space + space <Esc>modified
  • ;Repeat search last timefCharacter to look for
  • .Repeat last modification
  • ; .
  • ; .

Note: Instead of entering f+ 4 times, use; Repeat the search.

; Repeat search + repeat modify


4. Perform, repeat, and rollback

Repetition is useful, but sometimes multiple presses. , prone to the error of pressing more.

  • If I press too much.You can useuUndo the last modification.
  • If I press too much;You can use.Jump back because.It will reverse look up last timef{char}The character to find
purpose operation repeat The fallback
Make a change {} modified . u
Finds the next specified character in a line F {} characters / T {} characters ; .
Finds the last specified character in a line F {} characters / T {} characters ; .
Look for the next match in the document / the pattern enter n N
Look for the previous match in the document ? The pattern enter n N
To replace :s/target/replacement & u
Perform a series of changes Qx {modify} q @x u

Note that t{character} stops before the searched character, while t{character} stops after the searched character.


5. Find and manually replace

The requirement replaces the content in the text with copy.

. We're waiting for content before the site can go live... . If you are content with this, let's go ahead with it... . We'll launch as soon as we have the content...Copy the code

The :substitute command is used to find replacement tasks.

Lazy way: no need to input can search for the cloud:. Command is my favorite Vim single key command, the second is the * command, this command == can find the current cursor under the word ==.

  1. /content: look for tocontentThe word

* : find the word 3. Cwcopy

: change content to copy 4. N: Find the next 5. If you do not want to change it, press n to skip to the next step

Moving the cursor over content and pressing * yields two results:

  • The cursor jumps to the next match
  • All the places where the word appears are highlighted

If it’s not highlighted, try running :set HLS


6. Know.paradigm

The ==. Paradigm is the ideal editing mode, with one key to move and another to execute. = =

  • You only need to press the button once to move the cursor to the next target
  • use.The command repeats the previous modification

Normal Mode Is the default Vim mode.

7. Remove the brush when you pause

A painter does not rest his brush on the canvas. Common mode is Vim’s natural state of relaxation, as its name implies.


8. Cut the undo unit into chunks

The U key triggers the undo command, which undoes the latest changes. In Vim, we can control the granularity of the undo command ourselves. Anything entered or deleted from the time you enter insert mode until you return to normal mode is treated as a modification. Thus, you can make undo work on a word, sentence, or paragraph by controlling the use of the

key.

How often do you leave insert mode? The authors like to make each “undo block” correspond to a thought process. No matter how short the pause, each pause is a natural break point that prompts the author to exit insertion mode.

When in insert mode, the fastest way to start another line is to press

. But sometimes the author prefers to press

o because the author has a premonition that perhaps the author wants to have finer granularity when undoing.

In general, if you pause long enough to ask “Should I exit insert mode?” This question, quit.

== If the cursor keys

,

,

, and

are used in insert mode, a new undo speed will be generated. You can think of this as switching back to normal mode and then using h, J, k, l to move the cursor, but not exiting insert mode. This affects the operation of.commands.


9. Construct repeatable modifications

In Vim, the most obvious indicator of which approach is best is efficiency, which approach requires the fewest keystrokes (aka VimGolf). VimGolf vimgolf.com/

But how do you choose the winner in a draw?

The end is nigh is placed at h, and now I want to delete The word nigh.

Reverse delete: Vim golf score 3

  • dbDelete from the beginning of the cursor to the beginning of the word
  • xdeleteh

Positive deletion: Vim golf score 3

  • bMove to the beginning of the word
  • dwDelete the entire word

Delete entire word: Vim Golf score 3 use AW text object

  • dawDelete a word (which can be read asdelete a word)

Decider: Which is the most repetitive? To see which way to maximize the power of the command. == x Forward delete. == dw Delete the entire word. == daw So DAw wins!