How to copy X text

0 views
Skip to first unread message

Shade

unread,
Dec 2, 2008, 5:35:25 AM12/2/08
to vim_use
Hello everyone. I'm gonna paste an example of what i want to do. Let's
imagine that I have this code:

<p id="already_registered">

And i wanna copy id="already_registered" in a register, for example
the register "P". How can i do that, and how can i paste it back
later?

Thanks in advance.

A. S. Budden

unread,
Dec 2, 2008, 6:02:09 AM12/2/08
to vim...@googlegroups.com
2008/12/2 Shade <cele...@gmail.com>:

In what situation? A script? In normal mode?

I'm sure there are better ways of doing this, but one that springs to
mind for scripts is:

let ml = matchlist(getline('.'), '<p \(id="[^"]*"\)>')
let @P = ml[1]

Of course, you may need to bulk it out to check that the regexp
actually matches etc

For pasting, either use setline() or :put

:help matchlist()
:help match()
:help let-@
:help setline()
:help :put

Hope that helps,

Al

Charles Campbell

unread,
Dec 2, 2008, 9:35:35 AM12/2/08
to vim...@googlegroups.com
I'm sure that there are a lot of ways. Using normal mode (ie. manually):

First method:

put cursor on the "i" in "id=..." . Press v.
move cursor to the end of what you want.
Press "py

Put cursor elsewhere, press
"pp

Second method:

Again, put the cursor on the "i" in "id=...". Press mp .
Move cursor to the end of what you want, plus one more.
Press: "py`p

Again, put cursor elsewhere, press
"pp

Regards,
Chip Campbell

Agathoklis D. Hatzimanikas

unread,
Dec 2, 2008, 10:01:39 AM12/2/08
to vim...@googlegroups.com

Yet another one (while the cursor is on the "i" in "id=..."):

"pyt>

It turned out that "t,T" and "f,F" are quite useful motions and I have to
admit that I underestimated the usefulness of them when I first started
to learn vim in 2006, but now I use them all the time.

Regards,
Ag.

Shade -

unread,
Dec 2, 2008, 10:22:17 AM12/2/08
to vim...@googlegroups.com
Thanks a lot Charles and Agathoklis, both methods worked perfectly. I forgot to enter the visual method to select a zone.

Chris Suter

unread,
Dec 2, 2008, 1:51:01 PM12/2/08
to vim...@googlegroups.com
you could also, from anywhere inside the double quotes, type "pyi" (that's double-quote, followed by 'p', 'y', 'i', and another double quote).

this says, into the p register, (y)ank (a) double-quoted text object.

this works for all sorts of things: you could get all the text in the tag (from after < to before >) by typing yi<

see :help objects   for more on text objects -- there are about a million of them and they're a wonderful creation.
--
Christopher Suter

Chris Suter

unread,
Dec 2, 2008, 1:51:35 PM12/2/08
to vim...@googlegroups.com
"this says, into the p register, (y)ank (a) double-quoted text object."

should have said "this says, into the p register, (y)ank (i)nside double-quoted text object."
--
Christopher Suter
Reply all
Reply to author
Forward
0 new messages