user364902
user364902

Reputation: 3344

Vim global command with two search ranges to yank multiple sections

I wanted to create a global command to yank multiple sections of text:

:g/BEGIN/.,/END/yank A

The idea is that I would assemble, in register A, the (multiple) sections in a file which occur between the markers "BEGIN" and "END:"

BEGIN
Section 1
END

BEGIN
Section 2
END

Unfortunately this seems to act somewhat chaotically on VIM. Sometimes the global seems to select multiple sections (i.e. it doesn't stop the selection at the first "END" it finds, but instead selects up to a farther one, sometimes a single section. The number of lines selected by the same global seems to change for the same file somewhat randomly based on unrelated edits.

Obviously I'm doing something wrong. Is there a correct syntax for this - Selecting and yanking multiple sections delineated by markers? I haven't found much online to validate this syntax - although there is plenty of docs for ex ranges, and for search ranges, and for ranges with globals, I've never seen all three combined like this.

Upvotes: 1

Views: 348

Answers (1)

user364902
user364902

Reputation: 3344

After Lieven verified that my syntax was correct (it was) I discovered the problem had something to do with the use of folds in my text. When I unfolded all the text, I got the expected behavior.

When I had folded text, the global command didn't always find the first "END" marker - sometimes it seemed to yank the whole document.

This seems like a bug to me - why would the first search term of the global command work despite the presence of folds, but the second term not work? But maybe this is expected behavior.

Here is some sample text which shows the issue:

 {{{
 BEGIN
 111
 END
 }}}
 {{{
 BEGIN
 222
 END
 }}}

When unfolded, the command g/BEGIN/.,/END/yank A finds the correct text and when folded, the global returns an error.

In my scenario, "unfold the text" is a viable solution, so I'm posting here for the sake of posterity.

Upvotes: 2

Related Questions