Quantcast

how to copy non-continuous matched lines into clipboard?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to copy non-continuous matched lines into clipboard?

Zhaojun WU

Hi, Vimmers,

Just come across this problem when I need to copy some non-continuous
lines matched by specified search pattern. For example, in the
following lines:

FOO1
BAR BAR
BAR
FOO2
BAR
FOO3
...

Say, I want to copy all the lines with leading "FOO" out and paste
them into a new window.

Currently, I can use "record" to remember my keystrokes  and play it a
number of times. In addition, it is also a easy task by using utility
like "sed". But I am still wondering if there is any shortcut in VIM?

Thanks in advance.

--
Best,
Zhaojun (Joseph)

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to copy non-continuous matched lines into clipboard?

Jeenu V

You can make use of both the registers and the :global command here:

:let @a=""
:g/^FOO/normal "AY

Now the Register "a" would have all the lines that begins with FOO

"aPdd would give you what you want.

First clear the register "a". What the "g" command does is that it
goes to each line and search for the pattern; in this case "^FOO". If
it finds one, then the ex mode command "normal "AY" is executed. This
command actually does the "AY in normal mode (See :help :normal). Now
the "AY part: "A stands for the register "a", using the Uppercase, not
just copies to the register, but appends (See :help quotea). Doing it
repeatedly gets you all the line appended.

Thanks
Jeenu

On Aug 11, 4:50 pm, "Zhaojun WU" <[hidden email]> wrote:

> Hi, Vimmers,
>
> Just come across this problem when I need to copy some non-continuous
> lines matched by specified search pattern. For example, in the
> following lines:
>
> FOO1
> BAR BAR
> BAR
> FOO2
> BAR
> FOO3
> ...
>
> Say, I want to copy all the lines with leading "FOO" out and paste
> them into a new window.
>
> Currently, I can use "record" to remember my keystrokes  and play it a
> number of times. In addition, it is also a easy task by using utility
> like "sed". But I am still wondering if there is any shortcut in VIM?
>
> Thanks in advance.
>
> --
> Best,
> Zhaojun (Joseph)


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to copy non-continuous matched lines into clipboard?

Noah Spurrier
In reply to this post by Zhaojun WU

On 2007-08-11 19:50+0800, Zhaojun WU wrote:

>
>FOO1
>BAR BAR
>BAR
>FOO2
>BAR
>FOO3
>...
>
>Say, I want to copy all the lines with leading "FOO" out and paste
>them into a new window.

Maybe this is cheating:
     %!egrep ^FOO

--
  Noah Spurrier | email: [hidden email] | http://www.noah.org/wiki/engineering 
-------------------------------------------------------------------------------

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to copy non-continuous matched lines into clipboard?

Jeenu V
In reply to this post by Jeenu V

Oops! I forgot the Clipboard part:

You can then copy the contents of register "a" to the clipboard by
doing

:let @+=@a

@+ is the clipboard register, just in case.

Thanks
Jeenu

On Aug 11, 6:00 pm, Jeenu <[hidden email]> wrote:

> You can make use of both the registers and the :global command here:
>
> :let @a=""
> :g/^FOO/normal "AY
>
> Now the Register "a" would have all the lines that begins with FOO
>
> "aPdd would give you what you want.
>
> First clear the register "a". What the "g" command does is that it
> goes to each line and search for the pattern; in this case "^FOO". If
> it finds one, then the ex mode command "normal "AY" is executed. This
> command actually does the "AY in normal mode (See :help :normal). Now
> the "AY part: "A stands for the register "a", using the Uppercase, not
> just copies to the register, but appends (See :help quotea). Doing it
> repeatedly gets you all the line appended.
>
> Thanks
> Jeenu
>
> On Aug 11, 4:50 pm, "Zhaojun WU" <[hidden email]> wrote:
>
> > Hi, Vimmers,
>
> > Just come across this problem when I need to copy some non-continuous
> > lines matched by specified search pattern. For example, in the
> > following lines:
>
> > FOO1
> > BAR BAR
> > BAR
> > FOO2
> > BAR
> > FOO3
> > ...
>
> > Say, I want to copy all the lines with leading "FOO" out and paste
> > them into a new window.
>
> > Currently, I can use "record" to remember my keystrokes  and play it a
> > number of times. In addition, it is also a easy task by using utility
> > like "sed". But I am still wondering if there is any shortcut in VIM?
>
> > Thanks in advance.
>
> > --
> > Best,
> > Zhaojun (Joseph)


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to copy non-continuous matched lines into clipboard?

Zhaojun WU

Hi, Jeenu,

On 8/12/07, Jeenu <[hidden email]> wrote:
>
> Oops! I forgot the Clipboard part:
>
> You can then copy the contents of register "a" to the clipboard by
> doing
>
> :let @+=@a
>
> @+ is the clipboard register, just in case.

That's amazing. I mean, from your reply, I learned a lot of new
secrets behind VIM. Your answer is exactly what I want. Especially,
for the "A"--append to register "a". :)

Thank you very much for your detailed explanation.

Best,
Zhaojun

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to copy non-continuous matched lines into clipboard?

Kev-13
In reply to this post by Zhaojun WU

Zhaojun WU wrote:
 > Just come across this problem when I need to copy some non-continuous
 > lines matched by specified search pattern. For example, in the
 > following lines:
 >
 > FOO1
 > BAR BAR
 > BAR
 > FOO2
 > BAR
 > FOO3
 > ...
 >
 > Say, I want to copy all the lines with leading "FOO" out and paste
 > them into a new window.
 >
 > Currently, I can use "record" to remember my keystrokes  and play it a
 > number of times. In addition, it is also a easy task by using utility
 > like "sed". But I am still wondering if there is any shortcut in VIM?
 >

Registers can be used to copy to or append to.  The following will do
the trick:

:let @a='' |g/pattern/norm "Ayy

1) :let @a=''  #clears register a
2) |  # allows a second command on the same command line
3) g/pattern/norm "Ayy   # uses the global command and the norm command
    to append multiple lines to register a.
    At this point you can put register 'a' any place you would like.

Kevin

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Loading...