Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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