Note: Please use the other solution I posted instead of this kind of broken one.
Original post:
I have this mapping in my .vimrc
to do precisely what you ask:
:vmap <f9> y:exec substitute(@", '\n\s*\\', ' ', 'g')<cr>
So, visually select some text, press F9, and it will be vim-compiled.
It has one important shortcoming: it fails if the vimscript to compile contains comments. Still, I find it extremely useful and have been using it regularly for years.
Explanation:
So how does this work?:
:vmap <f9> y:exec substitute(@", '\n\s*\\', ' ', 'g')<cr>
In visual mode, pressing F9 will yank (y) the highlighted
text, which will go into the unnamed register ("), and then execute
(:exec … <cr>
) the code resulting from the 'substitute' expression,
which will operate on the contents of the unnamed register (@"),
replacing all ('g') instances of text matching the regex \n\s*\\
with a single space (' '). The regex matches a newline followed by
zero or more instances of whitespace, followed by a backslash.
Now, what is that replacement about? The idea was that I wanted to be
able to highlight for execution something like these three lines for
example:
echo "foo " .
\\ "bar " .
\\ "baz."
So I wanted the result of the substitution to be:
echo "foo " . "bar " . "baz."
The problem is, that regex is wrong: it should have been \n\s*\\\\
,
to match two consecutive backslashes.
Why did the mapping work? Well, it didn't really, but it appears that
I never really used it with continuation lines, so it was fine.
And now, I'm not sure why simply fixing the regex isn't sufficient
instead of that ExecHighlighted() function in my other post. I think I
had found another problem, which I wanted to document here (which is
why I hadn't filled in the explanation yet), but now I forgot.