Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a string as stdin to a system call from vim?

Tags:

vim

stdin

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?

like image 753
Chip Hogg Avatar asked Sep 20 '25 01:09

Chip Hogg


1 Answers

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
like image 160
Kent Avatar answered Sep 23 '25 07:09

Kent