1

Is there a way to make a substitution only at the beginning and end of a pattern and NOT line by line? E.g., searching for the pattern /^\*\{2,}.* in

* Micro topic 1
** Microbes are small
** You can't see them!
*** Isn't that something?

and surrounding the entire pattern in quotes would yield:

* Micro topic 1
"** Microbes are small
** You can't see them!
*** Isn't that something?"
  • 2
    You can include the whole or parts of the match in the substitution. Using \0 will include the whole match, so "\0" will surround the match with quotes. For your input example :%s/^\*\*\_.\{-}\*\*\*.\{-}$/"\0" will generate the output. Is that what you are looking for? – jjaderberg Jan 29 '16 at 20:54
  • 1
    It works for the example I provided, but the subheading structures will be variable, e.g. pastebin.com/JYhhCaq2 I tried modifying it in various ways: g/^\*\{2,}\_.\{-}\*\{2,}.\{-}$ Strangely, applying this to the example in pastebin excludes all top level headings (as desired) except for * dangerous microbes Perhaps if I can visually select the pattern, then I can substitute with "\0" ? – truthling Jan 30 '16 at 5:12
  • Perhaps this could help: /\n\*[^*]\|\%$ to go to the line before the next top level heading, or if that fails, to the last line of the file from @PhilipFrank – truthling Jan 30 '16 at 5:17
  • This, or something like it, might be handy too g/^* /+,/^* /- from vimregex.com/#address It is used for "all lines between Section 1 and Section 2, non-inclusively" – truthling Jan 30 '16 at 5:32
  • 1
    By the way, the ultimate goal is to join the subheadings onto one line with their parent heading, so perhaps it would be easier to get them on one line first? So the final product will be: "* Micro topic 1", "** Microbes are small<br>** You can't see them!<br>*** Isn't that something?" All on one line. – truthling Jan 30 '16 at 6:17
1

You can do it with the following multi-line regex:

:%s/^\*\{2,}.*\(\n\*\{2,}.*\)\+/"&"/
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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