VSCode: How to run a command after each terminal open?

On Linux systems you should use:

"terminal.integrated.shellArgs.linux"

On Windows and OSX:

terminal.integrated.shellArgs.windows

and

terminal.integrated.shellArgs.osx

respectively.

If you want to apply shellArgs setting on a per-workspace basis – you can, despite the fact that documentation says:

The first time you open a workspace which defines any of these settings, VS Code will warn you and subsequently always ignore the values after that

At least version 1.42 of VSCode asks you something like:

“This workspace wants to set shellArgs, do you want to allow it?”

See issue 19758


On Linux, if you are using bash (default for shell in VSCode), there are some subtleties:

  1. "terminal.integrated.shellArgs.linux": ["your_init_script.sh"]
    

    will execute the script and close terminal right away. To prevent this you’ll have to end the script with $SHELL command.

    #!/bin/bash
    echo "init"
    export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
    $SHELL
    

    But that way you end up in a subshell. Sometimes it’s unacceptable (Read 1) (Read 2).

  2. "terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
    

    will leave you in the initial shell, but will not execute the .bashrc init file. So you may want to source ~/.bashrc inside your_init_script.sh

    #!/bin/bash
    source ~/.bashrc
    echo "init"
    export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
    
  3. And if you already have some_init_script.sh in a repository, and for some reason don’t feel like adding source ~/.bashrc into it, you can use this:
    "terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
    

    your_init_script.sh:

    #!/bin/bash
    source ~/.bashrc
    source some_init_script.sh
    

    some_init_script.sh:

    #!/bin/bash
    echo "init"
    export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
    

    Outside of VSCode you can do without creating extra file. Like this

    bash --init-file <(echo "source ~/.bashrc; source some_init_script.sh")
    

    But I could not pass this string into terminal.integrated.shellArgs.linux – it needs to be split into array somehow. And none of the combinations I’ve tried worked.


Also, you can open terminal at a specific folder:

terminal.integrated.cwd

Change env:

"terminal.integrated.env.linux"
"terminal.integrated.env.windows"
"terminal.integrated.env.osx"

And even change terminal to your liking with

terminal.integrated.shell.linux
terminal.integrated.shell.windows
terminal.integrated.shell.osx

Or

terminal.external.linuxExec
terminal.external.osxExec
terminal.external.windowsExec

Leave a Comment

tech