Re: Yanking multi-line pattern matches
On 9/6/06, Yakov Lerner <[EMAIL PROTECTED]> wrote: On 9/6/06, Druce, Richard <[EMAIL PROTECTED]> wrote: > I'm trying to select all the text matching a (multi-line) pattern so I > can put it into a separate document. I thought the global command would > work but it only copies the first matching line, does anyone know a > command to do this? :g/multi-line-pattern/normal v//e"Ay There was error in the command above. Working command is: let @a='' " clear the register :silent! g/multi-line-pattern/exe "normal v//e\n\"Ay" This accumulates multi-lines containing multiline patterns into register A. Explanation: v//e turn current pattern into visual selection "Ay append visual selection to register A For best results, make pattern match up to and including the trailing \n of the last line of the mathing line (append .*\n if needed). Otherwise, last substring of the previous match will merge with first substring of next match in "A. Yakov
Re: Yanking multi-line pattern matches
On 9/6/06, Druce, Richard <[EMAIL PROTECTED]> wrote: I'm trying to select all the text matching a (multi-line) pattern so I can put it into a separate document. I thought the global command would work but it only copies the first matching line, does anyone know a command to do this? :g/multi-line-pattern/normal v//e"Ay This accumulates multi-lines containing multiline patterns into register A. Explanation: v//e turn current pattern into visual selection "Ay append visual selection to register A For best results, make pattern match up to and including the trailing \n of the last line of the mathing line (append .*\n if needed). Otherwise, last substring of the previous match will merge with first substring of next match in "A. Yakov
Re: Yanking multi-line pattern matches
Yakov Lerner wrote: On 9/6/06, Elliot Shank <[EMAIL PROTECTED]> wrote: Hari Krishna Dara wrote: > On Wed, 6 Sep 2006 at 12:18pm, Druce, Richard wrote: >> I'm trying to select all the text matching a (multi-line) pattern so I >> can put it into a separate document. I thought the global command would >> work but it only copies the first matching line, does anyone know a >> command to do this? > > I don't see any Vim primitives to work with multi-line matches. I think > the searchpos() function should be extended to return the [line, col] of > the end of the match as well. May be I am missing something, but in the > absence of primitives, you can probably write a function which will > try to guess what the matching lines are, something like this (only > partly tested): None of this is necessary. The simple solution is to use the uppercase name of a register, which appends to the register, rather than the lower case version, which replaces. I think the point of the question was how to yank multiple lines corresponding to multi-line pattern. For single-line pattern, you're right, it's trivial. Yakov What about (untested) :s/foo\_.{-}bar/\=(submatch(0) . nr2char(min([0, setreg(@", submatch(0), "c")]))) - execute it with the cursor on the line where the match starts - all on one line - the part after the dot is supposed to append the empty string to the "replace by" text while setting the unnamed register to the matched text To put it into a separate document then: :new :0put " :wq Best regards, Tony.
Re: Yanking multi-line pattern matches
On Wed, 6 Sep 2006 at 7:01am, Yakov Lerner wrote: > On 9/6/06, Elliot Shank <[EMAIL PROTECTED]> wrote: > > Hari Krishna Dara wrote: > > > On Wed, 6 Sep 2006 at 12:18pm, Druce, Richard wrote: > > >> I'm trying to select all the text matching a (multi-line) pattern so I > > >> can put it into a separate document. I thought the global command would > > >> work but it only copies the first matching line, does anyone know a > > >> command to do this? > > > > > > I don't see any Vim primitives to work with multi-line matches. I think > > > the searchpos() function should be extended to return the [line, col] of > > > the end of the match as well. May be I am missing something, but in the > > > absence of primitives, you can probably write a function which will > > > try to guess what the matching lines are, something like this (only > > > partly tested): > > > > None of this is necessary. The simple solution is to use the uppercase name of a register, which appends to the register, rather than the lower case version, which replaces. > > I think the point of the question was how to yank > multiple lines corresponding to multi-line pattern. > For single-line pattern, you're right, it's trivial. > > Yakov Right, Thanks Yakov. For single-line patterns, there is also a trick you can use to write the matches directly to the file. I think it is: :g/pattern/.w! >> file -- Hari __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Re: Yanking multi-line pattern matches
On 9/6/06, Elliot Shank <[EMAIL PROTECTED]> wrote: Hari Krishna Dara wrote: > On Wed, 6 Sep 2006 at 12:18pm, Druce, Richard wrote: >> I'm trying to select all the text matching a (multi-line) pattern so I >> can put it into a separate document. I thought the global command would >> work but it only copies the first matching line, does anyone know a >> command to do this? > > I don't see any Vim primitives to work with multi-line matches. I think > the searchpos() function should be extended to return the [line, col] of > the end of the match as well. May be I am missing something, but in the > absence of primitives, you can probably write a function which will > try to guess what the matching lines are, something like this (only > partly tested): None of this is necessary. The simple solution is to use the uppercase name of a register, which appends to the register, rather than the lower case version, which replaces. I think the point of the question was how to yank multiple lines corresponding to multi-line pattern. For single-line pattern, you're right, it's trivial. Yakov
Re: Yanking multi-line pattern matches
Hari Krishna Dara wrote: On Wed, 6 Sep 2006 at 12:18pm, Druce, Richard wrote: I'm trying to select all the text matching a (multi-line) pattern so I can put it into a separate document. I thought the global command would work but it only copies the first matching line, does anyone know a command to do this? I don't see any Vim primitives to work with multi-line matches. I think the searchpos() function should be extended to return the [line, col] of the end of the match as well. May be I am missing something, but in the absence of primitives, you can probably write a function which will try to guess what the matching lines are, something like this (only partly tested): None of this is necessary. The simple solution is to use the uppercase name of a register, which appends to the register, rather than the lower case version, which replaces. For example, the "A" register is identical to the "a" register, but what you put into "A" adds to "a", instead of replacing it. So, you want: :g/foobie bletch/y A After this, register "a" will contain every line that has "foobie bletch" in it. However, it also contains whatever was in it previously, so make sure the register you use is empty first. The uppercase registers are really useful for moving arbitrary chunks of code to a combined destination. Find the first set of lines you want to move and delete them into a lower-case register, e.g. "r5dd. Then, go and find the other lines you want and use the upper-case register, e.g. "R32dd. Finally, go to your destination and paste either register, e.g. "rp.
Re: Yanking multi-line pattern matches
On Wed, 6 Sep 2006 at 12:18pm, Druce, Richard wrote: > I'm trying to select all the text matching a (multi-line) pattern so I > can put it into a separate document. I thought the global command would > work but it only copies the first matching line, does anyone know a > command to do this? > > Cheers, > Richard. I don't see any Vim primitives to work with multi-line matches. I think the searchpos() function should be extended to return the [line, col] of the end of the match as well. May be I am missing something, but in the absence of primitives, you can probably write a function which will try to guess what the matching lines are, something like this (only partly tested): "function! FindMatchingRange() function! FindMatchingLines() let start = line('.') let text = '' for end in range(start, line('$')) let text = text."\n".getline(end) if text =~ @/ "return start.','.end return text endif endfor return '' endfunction You can use this either use it with :g command, or another function :let matches=[] :g/pattern/call add(matches, FindMatchingLines()) you can paste the matches in a new buffer as: :call append('$', matches) Read the help on |string-match| for limitations. -- HTH, Hari __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Yanking multi-line pattern matches
I'm trying to select all the text matching a (multi-line) pattern so I can put it into a separate document. I thought the global command would work but it only copies the first matching line, does anyone know a command to do this? Cheers, Richard.