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 11. Don't Count If You Can Repeat

Tip 11. Don't Count If You Can Repeat

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

Don’t Count If You Can Repeat



• 23



If, on the other hand, we had used the command dw. in the first place, we would

have to repeat the dot command only one more time. Because our original

change was simply dw, the u and . commands have more granularity.

Each acts upon one word at a time.

Now suppose that we want to delete seven words. We could either run d7w ,

or dw. ..... (that is, dw followed by the dot command six times). Counting

keystrokes, we have a clear winner. But would you trust yourself to make the

right count?

Counting is tedious. I’d rather hit the dot command six times than spend

the same time looking ahead in order to reduce the number of keys that I

have to press. What if I hit the dot command one too many times? No matter, I just back up by hitting the u key once.

Remember our mantra (from Tip 4, on page 8): act, repeat, reverse. Here it

is in action.



Use a Count When It Matters

Suppose that we wanted to change the text “I have a couple of questions” to

instead read “I have some more questions.” We could do so as follows:

Keystrokes



Buffer Contents



{start}



I have a couple of questions.



c3w some more



I have some more questions.



In this scenario, it doesn’t make much sense to use the dot command. We

could delete one word and then another (with the dot command), but then

we’d have to switch gears and change to Insert mode (using i or cw , for

example). To me, that feels awkward enough that I’d rather go ahead and use

a count.

There’s another advantage to using a count: it gives us a clean and coherent

undo history. Having made this change, we could undo it with a single press

of the u key, which ties in with the discussion in Tip 8, on page 16.

That same argument also goes in favor of counting ( d5w ) over repeating ( dw.... ),

so my preferences may seem inconsistent here. You’ll develop your own

opinion on this, depending on how much you value keeping your undo history

clean and whether or not you find it tiresome to use counts.



www.it-ebooks.info



report erratum • discuss



Chapter 2. Normal Mode



• 24



Tip 12



Combine and Conquer

Much of Vim’s power stems from the way that operators and motions can be

combined. In this tip, we’ll look at how this works and consider the implications.



Operator + Motion = Action

The d{motion} command can operate on a single character ( dl ), a complete

word ( daw ), or an entire paragraph ( dap ). Its reach is defined by the motion.

The same goes for c{motion} , y{motion} , and a handful of others. Collectively,

these commands are called operators. You can find the complete list by

looking up :h operator , while Table 2, Vim's Operator Commands, on page 25,

summarizes some of the more common ones.

The g~ , gu , and gU commands are invoked by two keystrokes. In each case,

we can consider the g to be a prefix that modifies the behavior of the subsequent keystroke. See Meet Operator-Pending Mode, on page 26, for further

discussion.

The combination of operators with motions forms a kind of grammar. The

first rule is simple: an action is composed from an operator followed by a

motion. Learning new motions and operators is like learning the vocabulary

of Vim. If we follow the simple grammar rules, we can express more ideas as

our vocabulary grows.

Suppose that we already know how to delete a word using daw , and then we

learn about the gU command (see :h gU ). It’s an operator too, so we can

invoke gUaw to convert the current word to SHOUTY case. If we then expand

our vocabulary to include the ap motion, which acts upon a paragraph, then

we find two new operations at our disposal: dap to delete, or gUap to make the

whole paragraph shout.

Vim’s grammar has just one more rule: when an operator command is invoked

in duplicate, it acts upon the current line. So dd deletes the current line, while

>> indents it. The gU command is a special case. We can make it act upon

the current line by running either gUgU or the shorthand gUU .



www.it-ebooks.info



report erratum • discuss



Combine and Conquer



Trigger



Effect



c



Change



d



Delete



y



Yank into register



g~



Swap case



gu



Make lowercase



gU



Make uppercase



>



Shift right



<



Shift left



=



Autoindent



!



Filter {motion} lines through an external program



• 25



Table 2—Vim’s Operator Commands



Extending Vim’s Combinatorial Powers

The number of actions that we can perform using Vim’s default set of operators

and motions is vast. But we can expand these even further by rolling our own

custom motions and operators. Let’s consider the implications.



Custom Operators Work with Existing Motions

The standard set of operators that ships with Vim is relatively small, but it

is possible to define new ones. Tim Pope’s commentary.vim plugin provides

a good example.2 This adds a command for commenting and uncommenting

lines of code in all languages supported by Vim.

The commentary command is triggered by \\{motion} , which toggles commenting for the specified lines. It’s an operator command, so we can combine it

with all of the usual motions. \\ap will toggle commenting for the current

paragraph. \\G comments from the current line to the end of the file. \\\

comments the current line.

If you’re curious about how to create your own custom operators, start by

reading :h :map-operator .



Custom Motions Work with Existing Operators

Vim’s standard set of motions is fairly comprehensive, but we can augment

it further by defining new motions and text objects.



2.



https://github.com/tpope/vim-commentary



www.it-ebooks.info



report erratum • discuss



Chapter 2. Normal Mode



• 26



Kana Natsuno’s textobj-entire plugin is a good example.3 It adds two new text

objects to Vim: ie and ae , which act upon the entire file.

If we wanted to autoindent the entire file using the = command, we could run

gg=G (that is, gg to jump to the top of the file and then =G to autoindent

everything from the cursor position to the end of the file). But if we had the

textobj-entire plugin installed, we could simply run =ae . It wouldn’t matter

where our cursor was when we ran this command; it would always act upon

the entire file.

Note that if we had both the commentary and textobj-entire plugins installed,

we could use them together. Running \\ae would toggle commenting

throughout the current file.

If you’re curious about how to create your own custom motions, start by

reading :h omap-info .



Meet Operator-Pending Mode

Normal, Insert, and Visual modes are readily identified, but Vim has other modes

that are easy to overlook. Operator-Pending mode is a case in point. We use it dozens

of times daily, but it usually lasts for just a fraction of a second. For example, we

invoke it when we run the command dw . It lasts during the brief interval between

pressing d and w keys. Blink and you’ll miss it!

If we think of Vim as a finite-state machine, then Operator-Pending mode is a state

that accepts only motion commands. It is activated when we invoke an operator

command, and then nothing happens until we provide a motion, which completes

the operation. While Operator-Pending mode is active, we can return to Normal mode

in the usual manner by pressing escape, which aborts the operation.

Many commands are invoked by two or more keystrokes (for examples, look up :h g ,

:h z , :h ctrl-w , or :h [ ), but in most cases, the first keystroke merely acts as a

prefix for the second. These commands don’t initiate Operator-Pending mode. Instead,

we can think of them as namespaces that expand the number of available command

mappings. Only the operator commands initiate Operator-Pending mode.

Why, you might be wondering, is an entire mode dedicated to those brief moments

between invoking operator and motion commands, whereas the namespaced commands are merely an extension of Normal mode? Good question! Because we can

create custom mappings that initiate or target Operator-Pending mode. In other

words, it allows us to create custom operators and motions, which in turn allows us

to expand Vim’s vocabulary.



3.



https://github.com/kana/vim-textobj-entire



www.it-ebooks.info



report erratum • discuss



CHAPTER 3



Insert Mode

Most of Vim’s commands are triggered from other modes, but some functionality is within easy reach from Insert mode. In this chapter, we’ll explore these

commands. Although delete, yank, and put commands are all triggered from

Normal mode, we’ll see that there is a convenient shortcut for pasting text

from a register without leaving Insert mode. We’ll learn that Vim provides two

easy ways for inserting unusual characters that are not represented on the

keyboard.

Replace mode is a special case of Insert mode, which overwrites existing

characters in the document. We’ll learn how to invoke this and consider some

scenarios where it proves useful. We’ll also meet Insert Normal mode, a submode that lets us fire a single Normal mode command before dropping us

back into Insert mode.

Autocompletion is the most advanced functionality available to us from Insert

mode. We’ll cover it in depth in Chapter 19, Dial X for Autocompletion, on page

275.



Tip 13



Make Corrections Instantly from Insert Mode

If we make a mistake while composing text in Insert mode, we can fix it immediately. There’s no need to change modes. Besides the backspace key, we can

use a couple of other Insert mode commands to make quick corrections.

Touch typing is more than just not looking at the keyboard; it means doing

it by feel. When touch typists make an error, they know it even before their



www.it-ebooks.info



report erratum • discuss



Chapter 3. Insert Mode



• 28



eyes process the information on the screen in front of them. They feel it in

their fingers, like a misplaced step.

When we make a typing error, we can use the backspace key to erase the

mistake and then make a correction. As long as the error appears near the

end of the word, this may be the quickest strategy for making amends. But

what if the mistake was at the start of the word?

Expert typists recommend drastic measures: delete the entire word; then type

it out again. If you can type at a rate above sixty words per minute, retyping

a word from scratch will only take a second. If you can’t type that fast, consider this to be good practice! There are particular words that I consistently

mistype. Since I started following this advice, I’ve become more aware of which

words trip me up. As a result, I now make fewer mistakes.

Alternatively, you could switch to Normal mode, navigate to the start of the

word, fix the error, then hit A to return to where you left off in Insert mode.

That little dance could take longer than a second, and it would do nothing to

improve your touch-typing skills. Just because we can switch modes doesn’t

mean that we should.

In Insert mode, the backspace key works just as you would expect: it deletes

the character in front of the cursor. The following chords are also available

to us:

Keystrokes



Effect







Delete back one character (backspace)







Delete back one word







Delete back to start of line



These commands are not unique to Insert mode or even to Vim. We can also

use them in Vim’s command line as well as in the bash shell.



Tip 14



Get Back to Normal Mode

Insert mode is specialized for one task—entering text—whereas Normal mode

is where we spend most of our time (as the name suggests). So it’s important

to be able to switch quickly between them. This tip demonstrates a couple of

tricks that reduce the friction of mode switching.



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 11. Don't Count If You Can Repeat

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

×