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 58. Snap Between Files Using Global Marks

Tip 58. Snap Between Files Using Global Marks

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

Chapter 9. Navigate Between Files with Jumps



• 138



Let’s say that our codebase contains dozens of matches for the pattern fooBar.

For each match :vimgrep finds, it creates a record in the quickfix list. Now

suppose that we spend a minute or two traversing that list until eventually

we find what we are looking for. Now we want to get back to where we were

before we ran the :vimgrep command. How do we do it?

We could get there using the command to reverse through the jump

list, but it might take a while. This is a scenario where a global mark would

come in handy. If we had run the mM command before invoking :vimgrep, then

we could snap back in one move with the `M command.

Advice is rarely welcome when it goes “You should have started by doing X.”

Global marks are only useful if we have the forethought to set them up correctly in advance. With practice, you’ll learn to recognize the scenarios where

it would be useful to set a global mark.

Try to get into a habit of setting a global mark before using any commands

that interact with the quickfix list, such as :grep, :vimgrep, and :make. The same

goes for the commands that interact with the buffer and argument lists, such

as :args {arglist} and :argdo (see Tip 37, on page 80).

Remember, you can set up to twenty-six global marks, which is more than

you’ll ever need. Use them liberally; set a global mark any time you see

something that you might want to snap back to later.



www.it-ebooks.info



report erratum • discuss



Part IV



Registers



Vim’s registers are simply containers that hold text.

They can be used in the manner of a clipboard for

cutting, copying, and pasting text, or they can be

used to record a macro by saving a sequence of

keystrokes. In this part of the book, we’ll master

this core feature.



www.it-ebooks.info



CHAPTER 10



Copy and Paste

Vim’s cut, copy, and paste functionality differs from what you may be used

to. For a start, the terminology is different, as we’ll see in Vim's Terminology

Versus the World, on page 145. In Tip 59, on page 141, we’ll learn how to use

Vim’s delete, yank, and put commands for a handful of common cases.

Instead of dealing with a single system-wide clipboard, Vim provides a couple

of dozen registers where we can store regions of text. We’ll learn more about

Vim’s registers in Tip 60, on page 144. Vim’s put command is smart about how

it treats line-wise and character-wise text, as we’ll see in Tip 62, on page 151.

The put command has some interesting quirks when used from Visual mode,

as we’ll discover in Tip 61, on page 149.

Finally, in Tip 63, on page 154, we’ll learn about how to use the system paste

command inside Vim without producing strange effects.



Tip 59



Delete, Yank, and Put with Vim’s Unnamed Register

Vim’s delete, yank, and put commands are designed to make common tasks

easy by default. We’ll study a few problems that can easily be solved using

Vim’s unnamed register, and then we’ll finish by looking at a task that requires

a better understanding of how Vim’s registers work.

Normally when we discuss cut, copy, and paste, we talk about putting text

on a clipboard. In Vim’s terminology, we don’t deal with a clipboard but instead

with registers. In Tip 60, on page 144, we’ll see that Vim has multiple registers,

and we can specify which ones we want to use. But let’s start off by looking

at what can be done using the unnamed register.



www.it-ebooks.info



report erratum • discuss



Chapter 10. Copy and Paste



• 142



Transposing Characters

I consistently misspell some words. Over time, I may notice that I habitually

mistype a certain word, and then I can train myself out of it. But some spelling

mistakes are more haphazard. The most common typing error that I make is

to get two characters in the wrong order. Vim makes it easy to fix such mistakes.

Suppose that we’re typing out the title of this book when we make such a

transposition error:

Keystrokes



Buffer Contents



{start}



Practica lvim



F␣



Practica lvim



x



Practicalvim



p



Practical vim



Here we’ve typed the space too soon, but it’s easily mended. The F␣ command

places our cursor on the first of the two characters that we want to swap (see

Tip 49, on page 114). The x command cuts the character under the cursor,

placing a copy of it in the unnamed register. Then the p command pastes the

contents of the unnamed register after the cursor position.

Taken together, the xp commands can be considered as “Transpose the next

two characters.”



Transposing Lines

We can just as easily transpose the order of two lines of text. Instead of using

the x command to cut the current character, we can use the dd command,

which cuts the current line, placing it into the unnamed register:

Keystrokes



Buffer Contents



{start}



2) line two

1) line one

3) line three



dd



1) line one

3) line three



p



1) line one

2) line two

3) line three



The p command knows that this time we’re dealing with a line-wise chunk

of text, so it does the thing we would expect: it pastes the contents of the



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 58. Snap Between Files Using Global Marks

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

×