4

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?

Community
  • 1
  • 1
Chip Hogg
  • 1,570
  • 13
  • 14

3 Answers3

6

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
Kent
  • 189,393
  • 32
  • 233
  • 301
  • FWIW, I don't actually care about the return value, but otherwise I think you understand what I want. It looks like `system()` is the canonical vim way to do this. – Chip Hogg Aug 25 '13 at 19:17
  • @ChipHogg and you could build the command (the first arg of system() ) with the value (string) of your variable. – Kent Aug 25 '13 at 19:24
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.

Chip Hogg
  • 1,570
  • 13
  • 14
2

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)

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Peter Kay
  • 133
  • 7