An alternative that circumvents all the escaping problems (and potential size limitations of the shell command-line) would be to write the text to a temporary file (using tempname()
and writefile()
), and just passing or :cat
ing that filename to the Ruby script. Then, clean up with delete()
.
Passing a multiline string in Vimscript to an external script
-
12-07-2023 - |
Question
I am passing visually selected vim text to a Ruby script using:
system("echo -n " . shellescape(var_holding_selected_text) . " | my/ruby/script")
The script isa able to read the text from STDIN, but I find that all the newlines are preceded by a backslash. I gather that this is because the shellescape
function escapes newlines. I have two questions:
- why does
shellescape
escape the newlines and/or null bytes if it also quotes the string? Apparently the quoting of the string is enough, because I am receiving literal backslashes as a result of the escaping in my script. - is there a better way to pass the text to my ruby script?
Solution
OTHER TIPS
Just found a way with Claytron's help:
silent execute '!printf "\%s" '. shellescape(a:text, 1) .' | nc localhost 2224'
printf should pass through the entire contents of shellescape without interpretation.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow