0

Is it possible to run a blocking input statement in your code when it is run from within the vim buffer? For example:

#include <iostream>
int main(void) {
    int x;
    std::cin >> x;
    std::cout << x;
}

I am using a simple make command:

:setlocal makeprg=g++\ %\ &&\ ./a.out

But of course when I run the program with :make, the cin >> x statement doesn't block and the variable takes whatever garbage value it was initialized with. What could be a good way of going about this?

2
  • 2
    I'd not run the program with :make but use :terminal a.out instead. Maybe define a mapping that runs both commands?
    – Friedrich
    Feb 2, 2023 at 7:39
  • 1
    Funny thing is I cannot reproduce this issue. It blocks until I input something which is also printed. I'm currently on a Debian machine with (g)vim 8.2.
    – Friedrich
    Feb 2, 2023 at 11:01

1 Answer 1

2

When the make command trigger the program (here g++) it doesn't link std:in with an interactive prompt like a terminal program would do.

I believe you have two solutions:

  • Run your program within a terminal
  • Prepare the input in a file (prepared_input.in) and redirect it to your program (yourprogram < prepared_input.in > a.out)

The second is not interactive. It is the only way I know we can pass an input to a program started by the :make command.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.