Ivan Smirnov
Ivan Smirnov

Reputation: 4435

How to interact with an external command in vimscript?

I have a script which interacts with user (prints some questions to stderr and gets input from stdin) and then prints some data to stdout. I want to put the output of the script in a variable in vimscript. It probably should look like this:

let a = system("./script")

The supposed behaviour is that script runs, interacts with user, and after all a is assigned with its output to stdout. But instead a is assigned both with outputs to stdout and stderr, so user seed no prompts.

Could you help me fixing it?

Upvotes: 2

Views: 705

Answers (3)

Riyas Siddikk
Riyas Siddikk

Reputation: 186

Call the script within the other as,

. ./script.sh

I think this is what you meant.

Upvotes: -1

Ingo Karkat
Ingo Karkat

Reputation: 172510

Interactive commands are best avoided from within Vim; especially with GVIM (on Windows), a new console window pops up; you may not have a fully functional terminal, ...

Better query any needed arguments in Vimscript itself (with input(); or pass them on from a custom Vim :command), and just use the external script non-interactively, feeding it everything it needs.

Upvotes: 3

Ingo Karkat
Ingo Karkat

Reputation: 172510

What gets captured by system() (as well as :!) is controlled by the 'shellredir' option. Its usual value, >%s 2>&1 captures stdout as well as stderr. Your script needs to choose one (e.g. stdout) for its output, and the other for user interaction, and the Vimscript wrapper that invokes it must (temporarily) change the option.

:let save_shellredir = &shellredir
:set shellredir=>
:let a = system('./script') " The script should interact via stderr.
:let &shellredir = save_shellredir

Upvotes: 3

Related Questions