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 19. Overwrite Existing Text with Replace Mode

Tip 19. Overwrite Existing Text with Replace Mode

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

Overwrite Existing Text with Replace Mode

• 35

Vim has a second variant of Replace mode. Virtual Replace mode is triggered

with gR and treats the tab character as though it consisted of spaces. Suppose

that we position the cursor on a tab stop spanning eight columns of screen

real estate. If we switch to Virtual Replace mode, we could type up to seven

characters, each of which would be inserted in front of the tab character.

Finally, if we typed an eighth character, it would replace the tab stop.

In Virtual Replace mode, we overwrite characters of screen real estate rather

than dealing with the actual characters that would eventually be saved in a

file. This tends to produce fewer surprises, so I would recommend using Virtual Replace mode whenever possible.

Vim also provides a single-shot version of Replace mode and Virtual Replace

mode. The r{char} and gr{char} commands allow us to overwrite a single

character before switching straight back to Normal mode (:h r ).

report erratum • discuss


Visual Mode

Vim’s Visual mode allows us to define a selection of text and then operate

upon it. This should feel pretty intuitive, since it is the model that most editing

software follows. But Vim’s take is characteristically different, so we’ll start

by making sure we grok Visual mode (Tip 20, on page 37).

Vim has three variants of Visual mode involving working with characters,

lines, or rectangular blocks of text. We’ll explore ways of switching between

these modes as well as some useful tricks for modifying the bounds of a

selection (Tip 21, on page 39).

We’ll see that the dot command can be used to repeat Visual mode commands,

but that it’s especially effective when operating on line-wise regions. When

working with character-wise selections, the dot command can sometimes fall

short of our expectations. We’ll see that in these scenarios, operator commands

may be preferable.

Visual-Block mode is rather special in that it allows us to operate on rectangular columns of text. You’ll find many uses for this feature, but we’ll focus

on three tips that demonstrate some of its capabilities.

Tip 20

Grok Visual Mode

Visual mode allows us to select a range of text and then operate upon it.

However intuitive this might seem, Vim’s perspective on selecting text is different

from other text editors.

Suppose for a minute that we’re not working with Vim but instead filling out

a text area on a web page. We’ve written the word “March,” but it should read

report erratum • discuss

Chapter 4. Visual Mode

• 38

“April,” so using the mouse, we double-click the word to select it. Having

highlighted the word, we could hit the backspace key to delete it and then

type out the correct month as a replacement.

You probably already know that there’s no need to hit the backspace key in

this example. With the word “March” selected, we would only have to type

the letter “A” and it would replace the selection, preparing the way so that we

could type out the rest of the word “April.” It’s not much, but a keystroke

saved is a keystroke earned.

If you expect this behavior to carry over to Vim’s Visual mode, you’re in for a

surprise. The clue is right there in the name: Visual mode is just another

mode, which means that each key performs a different function.

Many of the commands that you are familiar with from Normal mode work

just the same in Visual mode. We can still use h , j , k , and l as cursor keys.

We can use f{char} to jump to a character on the current line and then repeat

or reverse the jump with the ; and , commands, respectively. We can even

use the search command (and n / N ) to jump to pattern matches. Each time

we move our cursor in Visual mode, we change the bounds of the selection.

Some Visual mode commands perform the same basic function as in Normal

mode but with a slight twist. For example, the c command is consistent in

both modes in that it deletes the specified text and then switches to Insert

mode. The difference is in how we specify the range on which to act. From

Normal mode, we trigger the change command first and then specify the range

as a motion. This, if you’ll remember from Tip 12, on page 24, is called an

operator command. Whereas in Visual mode, we start off by making the

selection and then trigger the change command. This inversion of control can

be generalized for all operator commands (see Table 2, Vim's Operator Commands, on page 25). For most people, the Visual mode approach feels more


Let’s revisit the simple example where we wanted to change the word “March”

to “April.” This time, suppose that we have left the confines of the text area

on a web page and we’re comfortably back inside Vim. We place our cursor

somewhere on the word “March” and run viw to visually select the word. Now,

we can’t just type the word “April” because that would trigger the A command

and append the text “pril”! Instead, we’ll use the c command to change the

selection, deleting the word and dropping us into Insert mode, where we can

type out the word “April” in full. This pattern of usage is similar to our original

example, except that we use the c key instead of backspace.

report erratum • discuss

Define a Visual Selection

• 39

Meet Select Mode

In a typical text editing environment, selected text is deleted when we type any

printable character. Vim’s Visual mode doesn’t follow this convention—but Select

mode does. According to Vim’s built-in documentation, it “resembles the selection

mode in Microsoft Windows” (see :h Select-mode ). Printable characters cause the

selection to be deleted, Vim enters Insert mode, and the typed character is inserted.

We can toggle between Visual and Select modes by pressing . The only visible

difference is the message at the bottom of screen, which switches between -- VISUAL -and -- SELECT --. But if we type any printable character in Select mode, it will replace

the selection and switch to Insert mode. Of course, from Visual mode you could just

as well use the c key to change the selection.

If you are happy to embrace the modal nature of Vim, then you should find little use

for Select mode, which holds the hand of users who want to make Vim behave more

like other text editors. I can think of only one place where I consistently use Select

mode: when using a plugin that emulates TextMate’s snippet functionality, Select

mode highlights the active placeholder.

Tip 21

Define a Visual Selection

Visual mode’s three submodes deal with different kinds of text. In this tip, we’ll

look at the ways of enabling each visual submode, as well as how to switch

between them.

Vim has three kinds of Visual mode. In character-wise Visual mode, we can

select anything from a single character up to a range of characters within a

line or spanning multiple lines. This is suitable for working at the level of

individual words or phrases. If we want to operate on entire lines, we can use

line-wise Visual mode instead. Finally, block-wise Visual mode allows us to

work with columnar regions of the document. Block-wise Visual mode is quite

special, so we’ll discuss it at greater length in Tip 24, on page 45, Tip 25, on

page 47, and Tip 26, on page 48.

Enabling Visual Modes

The v key is our gateway into Visual mode. From Normal mode, we can press

v by itself to enable character-wise Visual mode. Line-wise Visual mode is

enabled by pressing V (with the Shift key), and block-wise Visual mode by

report erratum • discuss

Chapter 4. Visual Mode

• 40

pressing (with the Control key). These commands are summarized in

the following table:




Enable character-wise Visual mode


Enable line-wise Visual mode

Enable block-wise Visual mode


Reselect the last visual selection

The gv command is a useful little shortcut. It reselects the range of text that

was last selected in Visual mode. No matter whether the previous selection

was character-wise, line-wise, or block-wise, the gv command should do the

right thing. The only case where it might get confused is if the last selection

has since been deleted.

Switching Between Visual Modes

We can switch between the different flavors of Visual mode in the same way

that we enable them from Normal mode. If we’re in character-wise Visual

mode, we can switch to the line-wise variant by pressing V , or to block-wise

Visual mode with . But if we were to press v from character-wise Visual

mode, it would switch us back into Normal mode. So you can think of the v

key as a toggle between Normal mode and character-wise Visual mode. The

V and keys also toggle between Normal mode and their respective flavors

of Visual mode. Of course, you can always switch back to Normal mode by

pressing or (just like getting out of Insert mode). This table summarizes the commands for switching between Visual modes:




Switch to Normal mode

v / V /

Switch to Normal mode (when used from character-, line- or

block-wise Visual mode, respectively)


Switch to character-wise Visual mode


Switch to line-wise Visual mode

Switch to block-wise Visual mode


Go to other end of highlighted text

report erratum • discuss

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

Tip 19. Overwrite Existing Text with Replace Mode

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