The Vim Way
Vim is optimized for repetition. We can always replay the last change with a single keystroke.
The dot command(.
) is our starting point. We’ll work through a handful of simple editing tasks that can be rapidly completed with the dot command. We’ll identify an ideal editing formula, which requires only one keystroke to move and another to execute.
Tip 1 — Meet the Dot Command
The dot command lets us repeat the last change. It is the most powerful and versatile( 通用的;万能的;多才多艺的)command in Vim
:hlep .
repeats the last change
What is the ‘last change’ ?
A change could act at the level of individual characters, entire lines, or even the whole file.
for example:x
, dd
,>G
x
: deletes the character under the cursor.
dd
: deletes the current line as a whole.
>G
: increases the indentation from the current line until the end of the file.
The x
, dd
,and >
command are all executed from Normal mode.
The Dot Command Is a Micro Macro
Using Macro, vim can record any arbitrary number of keystrokes to be played back later. This allows us to capture out most repetitive workflows and replay them at a keystroke.
Tip 2 — Don’t Repeat Yourself
For such a common use case as appending a semicolon at the end of a series of lines, Vim provides a dedicated(anj. 专用的) command that combines two steps into one.
Suppose that we have a snippet of JaveScript code like this:
var foo = 1
var bar = 'a'
var foobar = foo + bar
We need to append a semicolon at the end of each line.
$
: moves the cursor to the end of the line
a
: appends text after the cursor
j
: [count] lines downward
$a;<esc>
+ j$.
Is there any room for improvement?
Reduce Extraneous(adj. 外来的;没有关联的) Movement
While the a
command appends after the current cursor position, the A
command appends at the end of the current line. We can squash(v. 把什么压扁) $a
into a single keystroke — A
Then the command above becomes
A;<esc>
+ j.
One keystroke to move, another to execute.
Tip 3 — Take One Step Back, Then Three Forward
We can pad(v. 填补) a single character with two spaces(one in front, the other behind) by using an idiomatioc(adj. 惯用的) Vim solution. At first it might look slightly odd, but the solution has the benefit of being repeatable, whick allows us to complete the task effortlessly
Suppose that we have a line of code that looks like this:
var foo = "method("+argument1+","+argument2+")";
We will make this a little easier on the eye by padding each + sign with spaces to make it look like this:
var foo = "method(" + argument1 + "," + argument2 + ")";
相关的命令:
s
: compounds(v. 混合) two steps into one: it deletes the chatacter under the cursor and then enters Insert mode.
f{char}
: tells Vim to look ahead for the next occurrence of the specified character and then move the cursor directly to it if a match is found.
;
: repeats the last search that the f
command performed.
.
so the complete commands are:
f+
+ s + <Esc>
+ ;.
+ ;.
+ ;.
One step back and then three steps forward. It’s a strange little dance that might seem unintuitive, but we get a big win by doing it this way: we can repeat the change with the dot command.
Tip 4 — Act, Repeat, Reverse
When facing a repetitive task, we can achieve an optimal (adj. 最佳的,最理想的)editing strategy by making both the motion(n. 移动) and the change repeatable. Vim has a knack(n. 诀窍,本领; k不发音) for this. It remembers our actions and keeps the most common ones within close reach so that we easily replay them. In this tip, we’ll introduce each of the actions that Vim can repeat and learn how to reverse(vt. 颠倒;倒转) them
Whenever Vim makes it easy to repeat an action or a motion, it always provides some way of backing out in case we accidentally go too far. In the case of the dot command, we can always hit the u
key to undo the last change. If we hit the ;
key too much times after using the f{char}
command, we’ll miss our mark. But we can back up again by pressing the ,
key, which repeats the last f{char}
search in the reverse direction.
Intent | Act | Repeat | Reverse |
---|---|---|---|
Make a change | {edit} | . | u |
Scan line for next character | f{char}/t{char} | ; | , |
Scan line for previous character | F{char}/T{char} | ; | , |
Scan document for next match | /pattern<CR> | n | N |
Scan document for previous match | ?pattern<CR> | n | N |
Perform substitution | :s/target/replacement | & | u |
Execute a sequence of changes | qx{changes}q | @x | u |
Tip 5 — Find and Replace by Hand
Vim has a :substitute
command for find-and-replace task, but with this alternative technique, we’ll change the first occurrence by hand and then find and replace every match one by one. The dot command will save us from labor, bur we’ll meet another nifty(adj. 俏皮的,漂亮的 ) one-key command that makes jumping between matches a snap.
In this excerpt(n. 摘录,引用), the word “content” appears on every line:
...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...
replace ‘content’ with ‘copy’ one by one (not all at once)
search with out typing
*
: search forward for the word under the cursor at that moment.
n
: repeat the latest “/” or “?” [count] times.
c{motion}
: deletes {motion} text and start insert.
w
: [count] words forward. | exclusive | motion.
.
so you can do like below:
*
+ cw
copy<CR>
+ n
+ .
Tip 6 — Meet the Dot Formula
We’ve considered three simple editting tasks so far. Even though each problem was different, we found a solution using the dot command for each one. In this tip, we’ll compare each solution and identify a common pattern — an optimal editting strategy that I call the Dot Formula.
The Ideal: One Keystroke to Move, One Keystroke to Execute