Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 3. Take One Step Back, Then Three Forward

Tip 3. Take One Step Back, Then Three Forward

Tải bản đầy đủ - 0trang

Take One Step Back, Then Three Forward



•7



Make the Change Repeatable

This idiomatic approach solves the problem:

Keystrokes



Buffer Contents



{start}



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



f+



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



s␣+␣



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



;



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



.



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



;.



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



;.



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



The s command compounds two steps into one: it deletes the character under

the cursor and then enters Insert mode. Having deleted the + sign, we then

type ␣+␣ and leave Insert mode.

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; all we need to do is position our

cursor on the next + symbol, and the dot command will repeat that little

dance.



Make the Motion Repeatable

There’s another trick in this example. The f{char} command 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 (see :h f ). So when we type f+ , our

cursor goes straight to the next + symbol. We’ll learn more about the f{char}

command in Tip 49, on page 114.

Having made our first change, we could jump to the next occurrence by

repeating the f+ command, but there’s a better way. The ; command will

repeat the last search that the f command performed. So instead of typing

f+ four times, we can use that command once and then follow up by using

the ; command three times.



All Together Now

The ; command takes us to our next target, and the . command repeats the

last change, so we can complete the changes by typing ;. three times. Does

that look familiar?



www.it-ebooks.info



report erratum • discuss



Chapter 1. The Vim Way



•8



Instead of fighting Vim’s modal input model, we’re working with it, and look

how much easier it makes this particular task.



Tip 4



Act, Repeat, Reverse

When facing a repetitive task, we can achieve an optimal editing strategy by

making both the motion and the change repeatable. Vim has a knack for this.

It remembers our actions and keeps the most common ones within close reach

so that we can easily replay them. In this tip, we’ll introduce each of the actions

that Vim can repeat and learn how to reverse them.

We’ve seen that the dot command repeats the last change. Since lots of

operations count as a change, the dot command proves to be versatile. But

some commands can be repeated by other means. For example, @: can be

used to repeat any Ex command (as discussed in Tip 31, on page 63). Or we

can repeat the last :substitute command (which itself happens to be an Ex

command as well) by pressing & (see Tip 92, on page 224).

If we know how to repeat our actions without having to spell them out every

single time, then we can be more efficient. First we act; then we repeat.

But when so much can be achieved with so few keystrokes, we have to watch

our step. It’s easy to get trigger-happy. Rapping out j.j.j. again and again

feels a bit like doing a drum roll. What happens if we accidentally hit the j

key twice in a row? Or worse, the . key?

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 many 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 (see Tip 49, on page 114).

It always helps to know where the reverse gear is in case you accidentally go

a step too far. Table 1, Repeatable Actions and How to Reverse Them, on page

9, summarizes Vim’s repeatable commands along with their corresponding

reverse action. In most cases, the undo command is the one that we reach

for. No wonder the u key on my keyboard is so worn out!



www.it-ebooks.info



report erratum • discuss



Find and Replace by Hand



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



n



N



Scan document for previous match ?pattern



n



N



Perform substitution



:s/target/replacement



&



u



Execute a sequence of changes



qx{changes}q



@x



u



•9



Table 1—Repeatable Actions and How to Reverse Them



Tip 5



Find and Replace by Hand

Vim has a :substitute command for find-and-replace tasks, but with this alternative

technique, we’ll change the first occurrence by hand and then find and replace

every other match one by one. The dot command will save us from labor, but

we’ll meet another nifty one-key command that makes jumping between

matches a snap.

In this excerpt, the word “content” appears on every line:

the_vim_way/1_copy_content.txt

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



Suppose that we want to use the word “copy” (as in “copywriting”) instead of

“content.” Easy enough, you might think; we can just use the substitute

command, like this:







:%s/content/copy/g



But wait a minute! If we run this command, then we’re going to create the

phrase “If you are ‘copy’ with this,” which is nonsense!

We’ve run into this problem because the word “content” has two meanings.

One is synonymous with “copy” (and pronounced content), the other with

“happy” (pronounced content). Technically, we’re dealing with heteronyms

(words that are spelled the same but differ in both meaning and pronunciation), but that doesn’t really matter. The point is, we have to watch our step.



www.it-ebooks.info



report erratum • discuss



Chapter 1. The Vim Way



• 10



We can’t just blindly replace every occurrence of “content” with “copy.” We

have to eyeball each one and answer “yay” or “nay” to the question, should

this occurrence be changed? The substitute command is up to the task, and

we’ll find out how in Tip 89, on page 219. But right now, we’ll explore an

alternative solution that fits with the theme of this chapter.



Be Lazy: Search Without Typing

You might have guessed by now that the dot command is my favorite singlekey Vim trigger. In second place is the * command. This executes a search

for the word under the cursor at that moment (see :h * ).

We could search for the word “content” by pulling up the search prompt and

spelling out the word in full:







/content



Or we could simply place our cursor on the word and hit the * key. Consider

the following workflow:

Keystrokes



Buffer Contents



{start}



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



*



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



cwcopy



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



n



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



.



...We're waiting for copy 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 copy...



We begin with our cursor positioned on the word “content” and then use the

* command to search for it. Try it for yourself. Two things should happen:

the cursor will jump forward to the next match, and all occurrences will be

highlighted. If you don’t see any highlighting, try running :set hls and then

refer to Tip 80, on page 201, for more details.

Having executed a search for the word “content,” we can now advance to the

next occurrence just by hitting the n key. In this case, pressing *nn would

cycle through all matches, taking us back to where we started.



www.it-ebooks.info



report erratum • discuss



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 3. Take One Step Back, Then Three Forward

Tải bản đầy đủ ngay(0 tr)

×