otto
otto

Reputation: 403

Copy between matches, not the lines, but things between match-points

:/From book:/,/$/ cmd_copy chapters_from_match_@From_book@_until_end_of_line.txt

I tried not to use words "write", "put" or "read" as VIM has special meaning for them. I try to copy (sorry not referring to VIM's copy-command) the thing between matches to a file. How do you do it, without copying the whole lines?

Dummy example

TEXT:

do not copy me dummy1 hello world please copy me dummy2 do not copy me

some enters, should work also with enters btw the matchpoints dummy1 not yet!

not yet!

copy will end soon! dummy2

COPIED:

hello world please copy me

or

dummy1 hello world please copy me dummy2

Upvotes: 1

Views: 181

Answers (2)

ib.
ib.

Reputation: 29014

To copy text between start and end, use the following sequence of Normal mode commands.

/start/e+1Entery/end/Enter

The first command searches for the next occurrence of the pattern matching start of a text fragment to copy, and positions the cursor to the first character after the match. The second one yanks everything until the next match of the ending pattern.

Depending on the context in which the commands will be used, they could be rewritten as an Ex command, a mapping or a macro.

  • Ex command

    :norm!/start/e+1^My/end/
    

    (Type ^M as Ctrl+V, Enter.)

  • Mapping

    :nnoremap <leader>y /start/e+1<cr>y/end/<cr>
    
  • Macro

    :let @y = "/start/e+1\ry/end/\r"
    

    (Or record a macro the usual way: type q, register to store the macro, say, y, then commands as it shown at the top of the answer followed by final q.)

Upvotes: 3

Benoit
Benoit

Reputation: 79233

so, why not use

:1s/^\_.\{-}dummy1//    "first line: delete everything until first occurrence of dummy1
:%s/dummy2\zs\_.\{-}\zedummy1/\n/
:1s/\_.*dummy2\zs\_.*// "delete from last occurrence of dummy2 till EOF

Upvotes: 0

Related Questions