search/replace from script (execute search command from string variable)

I am scripting search/replace function in vim and have a problem. It should work in visual mode for selected lines.

This works:

let b:CON="'<,'>s:[^t ]:a:e"
vnoremap r :<C-u>execute b:CON

But this does not (it should add ‘a’ after first letter in line):

let b:CON="'<,'>s:([^t ]):a:e"
vnoremap r :<C-u>execute b:CON

So here I just added a group in regex. But it does nothing now. What is wrong here? Same command works fine if I type it in or call it direct via map. Does execute un-escapes some characters in my string? I think it should not.
Extra question: is there some other good way to “execute” command on multiple lines, other than what I use here (C-u).


Source: regex

Leave a Reply