问题
I want to run a system command from vim (:help :!
), and I want to pass the contents of a string variable as stdin for that command.
I already know I can pass lines from the current buffer as stdin (see :help :w_c
). But I don't want to modify the current buffer, and I don't want to make a new buffer.
I know I can write the string to a temporary file, then pipe that file as input and delete it. Right now, that's the best solution I know for my application. But it would be cleaner to avoid creating the file in the first place.
Can it be done?
回答1:
so basically, you have a variable, say, a string, and you want to pass this variable to an external command, and get returned value in a variable instead of buffers.
If above is right, you could do in this way, see the following example:
let s="vim is good"
let r=system("sed 's/good/the best/'",s)
echo r
"display vim is the best
echo s
"display vim is good
回答2:
For posterity, here's another answer I figured out while hacking around.
@Kent's answer looks like the canonical vim way to pass stdin to a command. It frees you from managing the temporary file. But what if (for whatever reason) you want to avoid the overhead of creating a file at all? Here's a way to do that.
The basic form is
echo your_string | your_command
Of course, getting the details right is highly error prone, so I recommend wrapping it in a function:
function! RunCommandWithStdin(cmd, stdin_lines)
let l:stdin = shellescape(join(a:stdin_lines, "\\n"), 1)
execute '!echo -e' l:stdin '|' a:cmd
endfunction
Quick sanity check, using patch
:
:!echo foo > bar
:!cat bar
foo
:call RunCommandWithStdin('patch bar', ['@@ -1 +1 @@', '-foo', '+~!@#$%^&*()'])
:!cat bar
~!@#$%^&*()
Note that this appears to be handling special characters correctly.
The downside is that it uses echo
, so it's probably restricted to *nix-alikes. Oh well.
回答3:
You can also pass a list to system() to give your command more than one line of STDIN:
So:
let s=["perl is good", "perl is complicated"]
let r=system("sed 's/perl/vim/'", s)
echo r
"vim is good
"vim is complicated
Note also that in this case, you can use systemlist() to get your results as a list (as of Jan 2018, trailing blank lines are cut off, if this is important to you)
来源:https://stackoverflow.com/questions/18432409/can-i-pass-a-string-as-stdin-to-a-system-call-from-vim