title: How to script in Vim to yank only lines from a visual selection (or fold) that match a certain pattern?
body: <p>I'd like to add a command to my <code>.vimrc</code> that allows to, within a visual selection or the range of the current fold level to</p>
  
  <ul>
  <li>yank all, but only those lines that match a certain pattern.</li>
  </ul>
  
  <p>and as a bonus to</p>
  
  <ul>
  <li>reverse their order</li>
  </ul>
  
  <p>and</p>
  
  <ul>
  <li>perform a small pattern substitution.</li>
  </ul>
  
  <hr>
  
  <p>Specifically the idea is to reduce the legwork in writing the common C idiom fail-goto-rollback, i.e. (can be found in lot of C projects most prominently the Linux kernel) if the body of a function (or a block) is this</p>
  
  <pre><code>    someErrorType errorcode;
      if(fail1) {
          errorcode = someError1;
          goto error_1;
      }
      prepare_a();
      if(fail2) { 
          errorcode = someError2;
          goto error_2;
      }
  </code></pre>
  
  <p>then the result of the desired transformation shall be this.</p>
  
  <pre><code>error_2:
      /* &lt;insert cleanup code operation that did not fail1 here&gt; */
  error_1:
  </code></pre>