Search code examples
vim

vim internal terminal run multiple commands at a time


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!


Solution

  • 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.