vim backward search and replace before the matched pattern
<p>I'm enjoying vim, and its powerful search and replace command. However, I need to perform a complex research and substitute, like this one:</p>

<p>1) find the first occurrence of "field.h" and delete the line
2) from the deleted line search backwards up to a line containing "foo.h" (this search has to be case insensitive), and put a new line just before it containing "include 'io.h'"</p>

<p>3) repeat this operation for every occurrance of "field.h" in the file</p>

<p>Now, I know how to delete all the lines in a file matching a pattern, and how to insert a new line before a pattern, but I don't knonw how to all of this combined.</p>

<p>For point 1 above, I would do <code>:g/field.h/d</code>,  while for point 2 something like <code>:g/"foo.h"/s/#include "io.h"\r&amp;/g</code> should work...</p>

<p>As an example,  the starting file could be:</p>

<pre><code>"foo.h"

lines of text, they can be also empty lines
#include "field.h"
other text
</code></pre>

<p>and the resulting file should be:</p>

<pre><code>#include "io.h"
"foo.h"

lines of text, they can be also empty lines
other text
</code></pre>

<p>Some can help me?
Thanks</p>