Monday, February 21, 2011

Re: Copy/delete/paste strategy?

On 02/21/2011 10:41 AM, David Kahn wrote:
> Relatively new to vim. I know there are other ways to do this specific task
> such as search/replace but I want to understand how to repeatedly paste some
> text but also overwrite a certain character, in the case I want to do it
> manually. This has brought me to need to understand if there is a sort of
> clipboard/yank history.
>
> For example, I have yanked this text:
>
> .truncate(
>
> I want to replace the comma ',' in the lines below with my yanked text in
> each line:
>
> word,12)
> word2,12)
> word3,12)
>
> So what I do is move in regular mode to the first comma and press 'x' to
> remove the comma, and then press 'p'... however instead of getting my yanked
> text, I get the text now under the cursor, in this case '1'. So how do I
> handle a yank> delete> put operation, and multiple times?

There are a couple options that occur to me. The first two
involve overriding the register you're using. The first is to
specify which register to use when pasting. All yanks also get
put into register 0 (zero), so you can use

"0p

to paste the contents of the yanked stuff instead of the
more-recently-deleted stuff. Alternatively, you can specify that
your deletion goes to the black-hole register:

"_x

which leaves your scratch/default register unmolested for future
pasting.

Another option: if you do your replacement in one step/action
instead of two (delete + paste), you can repeat it with the "."
operator, so you could put your cursor on the comma, press "s" to
substitute (or "c<motion>") and then use control+R followed by
zero (to insert the contents of register 0), then press escape,
you can then move to the next target and press the period to
perform the same action.

If you find yourself reaching for the register-overrides
frequently, you can map them if you want...something like

:nnoremap <f4> "0p
:nnoremap <f5> "0P

to give you "paste the last yanked thing {before/after} the
cursor" at a single key-press.

Lastly, in this case, if you can target your replacements with a
regexp, you can do all the substitutions in one swoop...something
like

:%s/word\d*\zs,\ze12)/^R0

where "^R" is control+R as above to insert the contents of the
register 0 that you yanked into the command-line, and if you've
not done any deletions, you can use the double-quote (default)
register. You may have to adjust the flags if it can appear
multiple times on the same line, or if you want to confirm yes/no
on replacements.

You can read more at

:help quote0
:h quote_
:h .
:h i_CTRL-R
:h c_CTRL-R

Hope that helps,

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: