0

I create some useful vim command aliases like:

command GccAndRun !gcc main.c && ./a.out 

However, it's better to run a.out and check source code in same window.

I here about vim internel terminal, so I write the follwing sentence:

command GccAndRun terminal gcc main.c && ./a.out 

But given error message: gcc: error: &&: No such file or directory in opened terminal.

I don't know how to fix it. Please help me!

1 Answer 1

2

Let's start by fixing your initial command, which can't work as-is:

command GccAndRun !gcc main.c && ./a.out 

Now, :terminal unfortunately doesn't accept constructs like cmd1 && cmd2 or cmd1 | cmd2, so you will need a workaround:

  • create a shell script, say run.sh, that does gcc main.c && ./a.out for you and do:

    command GccAndRun terminal run.sh 

    Pros:

    • you can put whatever you want in that script

    Cons:

    • may need non-trivial scripting if you want to pass filenames, etc.
    • pollutes the project
    • is project-dependent
    • doesn't leave you in terminal mode
  • use the ++shell option, which tells Vim to run the given command in a non-interactive shell:

    command GccAndRun terminal ++shell gcc main.c && ./a.out 

    Pros:

    • you can input whatever command you want
    • doesn't pollute the project
    • is project-agnostic

    Cons:

    • doesn't leave you in terminal mode

See :help :terminal.

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

2 Comments

Thanks, typo fixed. ++shell meets my need, and what's the meaning of doesn't leave you in terminal mode? I run in this way, and everything is similar to without ++shell. I can't find a clear explanation in vim help doc.
Sorry, I meant to put that in both workarounds.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.