2

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?

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The whole idea of the plugin was interaction with user. So, I see, this is not the common way. Maybe I should better try to use vim menus somehow or to rewrite the entire script in Vimscript. Anyway, thanks!
3

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

2 Comments

Thanks! This makes sense, however, it didn't help completely. Now I really receive only stdout from the system() call, but stderr isn't displayed anywhere. Furthermore, the script (even the simplest like "read x; print x") does not request any input; it seems that stdin is blocked by vim somehow.
You may need to grab stdin again (exec 0</dev/tty); in general, this is hairy stuff and best avoided; can't you just pass in all information (as described in my other answer)?!
-1

Call the script within the other as,

. ./script.sh

I think this is what you meant.

1 Comment

Sorry, I don't get it. Where this line should be put? And what does the first dot mean in this context? BTW, the script is written on Perl but not bash, and this construction does not work as I suppose it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.