4

I see how to copy text in vim using visual mode, arrow keys, y to yank, and p to paste. I also see how to find a given starting string using back slash.

However, I'd appreciate knowing how to copy all the text from a given start phrase (such as To list all available commands enter "/?") to a given stop phrase (such as Enter chat message or link here).

I'd normally just use arrow keys, but for my application, the number of lines in-between the start and stop phrases may vary, so I'd like one script to work in all cases.

Thanks a lot in advance!

P.S. Does the solution also work in vimium? I asked the question on this site because practically everything that works for vim also works for vimium, but my actual application uses vimium.

2 Answers 2

4

I think the idea of using marks is indeed a good approach. Here is a function to add to your .vimrc to ease the process:

function! SelectDelimitations(start, end)
    " Set start mark
    call search(a:start)
    normal! ma
    " Set end mark at the end of the match
    call search(a:end, "e")
    normal! mb

    " Go to start mark and select the text
    normal! `a
    normal! v`b
endfunction

You can then use the function this way:

:call SelectDelimitation("Start pattern", "end pattern")

This will select the text between the patterns.

An interesting point in this function is that the text is selected character wise thanks to the backtick used in the normal command and the search flag e used to go to the end of the second pattern.

1

After some googling (mainly thanks to this post), I seem to have come up with a solution. The crucial idea is to use marks.

First, search for the starting phrase using the back slash command. Call this mark a by typing ma.

Next, search for the closing phrase using the back slash command again. Finally, once the cursor is at this position, type y'a (to yank to mark a).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.