Sean Mackesey
Sean Mackesey

Reputation: 10939

Passing a multiline string in Vimscript to an external script

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:

Upvotes: 1

Views: 604

Answers (2)

David Blewett
David Blewett

Reputation: 21

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.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172608

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 :cating that filename to the Ruby script. Then, clean up with delete().

Upvotes: 3

Related Questions