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 6. Meet the Dot Formula

Tip 6. Meet the Dot Formula

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

Chapter 1. The Vim Way



• 12



In Tip 3, on page 6, we wanted to pad each occurrence of the + symbol with

a space both in front and behind. We used the f+ command to jump to our

target and then the s command to substitute one character with three. That

set us up so that we could complete the task by pressing ;. a few times.

In Tip 5, on page 9, we wanted to substitute every occurrence of the word

“content” with the word “copy.” We used the * command to initiate a search

for the target word and then ran the cw command to change the first occurrence. This set us up so that the n key would take us to the next match and

the . key would apply the same change. We could complete the task simply

by pressing n. as many times as it took.



The Ideal: One Keystroke to Move, One Keystroke to Execute

In all of these examples, using the dot command repeats the last change. But

that’s not the only thing they share. A single keystroke is all that’s required

to move the cursor to its next target.

We’re using one keystroke to move and one keystroke to execute. It can’t

really get any better than that, can it? It’s an ideal solution. We’ll see this

editing strategy coming up again and again, so for the sake of convenience,

we’ll refer to this pattern as the Dot Formula.



www.it-ebooks.info



report erratum • discuss



Part I



Modes



Vim provides a modal user interface. This means

that the result of pressing any key on the keyboard

may differ depending on which mode is active at

the time. It’s vital to know which mode is active

and how to switch between Vim’s modes. In this

part of the book, we’ll learn how each mode works

and what it can be used for.



www.it-ebooks.info



CHAPTER 2



Normal Mode

Normal mode is Vim’s natural resting state. If this chapter seems surprisingly

short, then that’s because most of this book is about how to use Normal mode!

Here, however, is where we cover some core concepts and general tips.

Other text editors spend most of their time in something that resembles Insert

mode. So to the Vim newcomer, it can seem strange that Normal mode is the

default. In Tip 7, on page 15, we’ll begin explaining why this is by drawing

an analogy with the workspace of a painter.

Many Normal mode commands can be executed with a count, which causes

them to be run multiple times. In Tip 10, on page 20, we’ll meet a pair of

commands that increment and decrement numerical values and see how

these commands can be combined with a count to do simple arithmetic.

Just because you can save keystrokes by using a count doesn’t mean that

you should. We’ll look at some examples where it’s better simply to repeat a

command than take the time to count how many times you want to execute

it.

Much of the power of Normal mode stems from the way that operator

commands can be combined with motions. We’ll finish by looking at the

consequences of this.



Tip 7



Pause with Your Brush Off the Page

For those unused to Vim, Normal mode can seem like an odd default. But

experienced Vim users have difficulty imagining it any other way. This tip uses

an analogy to illustrate the Vim way.



www.it-ebooks.info



report erratum • discuss



Chapter 2. Normal Mode



• 16



How much time do you reckon artists spend with their paint brushes in

contact with the canvas? No doubt it would vary from artist to artist, but I’d

be surprised if it counted for as much as half of the time painters spend at

work.

Think of all of the things that painters do besides paint. They study their

subject, adjust the lighting, and mix paints into new hues. And when it comes

to applying paint to the canvas, who says they have to use brushes? A painter

might switch to a palette knife to achieve a different texture or use a cotton

swab to touch up the paint that’s already been applied.

The painter does not rest with a brush on the canvas. And so it is with Vim.

Normal mode is the natural resting state. The clue is in the name, really.

Just as painters spend a fraction of their time applying paint, programmers

spend a fraction of their time composing code. More time is spent thinking,

reading, and navigating from one part of a codebase to another. And when

we do want to make a change, who says we have to switch to Insert mode?

We can reformat existing code, duplicate it, move it around, or delete it. From

Normal mode, we have many tools at our disposal.



Tip 8



Chunk Your Undos

In other text editors, invoking the undo command after typing a few words

might revert the last typed word or character. However, in Vim we can control

the granularity of the undo command.

The u key triggers the undo command, which reverts the most recent change.

A change could be anything that modifies the text in the document. That

includes commands triggered from Normal, Visual, and Command-Line modes,

but a change could also encompass any text entered (or deleted) in Insert

mode. So we could also say that i {insert some text} constitutes a change.

In nonmodal text editors, triggering the undo command after typing a few

words could do one of two things. It could undo the last character that was

typed. Or, more helpfully, it could chunk a set of characters together so that

each undo operation removed a word instead of a character.

In Vim, we can control the granularity of the undo command. From the

moment we enter Insert mode until we return to Normal mode, everything we

type (or delete) counts as a single change. So we can make the undo command



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 6. Meet the Dot Formula

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

×