How to use :s with regex and filename in vim
<p>Wanting to create a function which will add me python logger at each function define I came up on macro:</p>

<pre><code>nnoremap &lt;leader&gt;L :%s/\(\s*\)def \(\w\S*\)\(self.*\):/\0\r\1\tlogging.info(\'\2)\')/g&lt;cr&gt;
</code></pre>

<p>It works, but I need eddited filename too, so I added to macro: <code>in \=expand("%p")</code>. Now macro looks like that:</p>

<pre><code>nnoremap &lt;leader&gt;L :%s/\(\s*\)def \(\w\S*\)\(self.*\):/\0\r\1\tlogging.info(\' \=expand("%p") \2)\')/g&lt;cr&gt;
</code></pre>

<p>Which doesn't work, because it prints whole <code>=expand("%p")</code> as a text, not as a file name.</p>

<p>It seems to me that in general in text substitution I can use expand if I do not use regex.</p>

<p>I would be terribly grateful for correct macro and explanation why didn't it work.</p>