Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

On Windows I have to run the command start-ssh-agent.cmd on each new terminal session I open. My development environment is VSCode, and I open a dozen new terminals each day. After each terminal open, I have to manually run this command.

Is there is a way to run this command on the terminal each time I open one ?

enter image description here

This may take the form of a VSCode extension, VSCode configuration (settings) or a Windows environment configuration.

Any idea?

like image 507
Sébastien Avatar asked Aug 11 '17 12:08

Sébastien


People also ask

How do I run a command in VS Code terminal?

Launching from the command line# You can also run VS Code from the terminal by typing 'code' after adding it to the path: Launch VS Code. Open the Command Palette (Cmd+Shift+P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.

How do I switch between two terminals in VS Code?

Once the cursor is in the terminal section you can use ctrl+x ctrl+up or ctrl+x ctrl+down to cycle through the active terminals (note that moving between on-screen split terminals is done with ctrl+x left or ctrl+x right ). cmd-J is still used to hide/show the terminal pane.

How do I run a partial code in VS Code?

You can: open a terminal at Terminal>New Terminal. Highlight the code you want to run. Hit Terminal>Run Selected Text.

How do I open vs code from the command line?

You can launch VS Code from the command line to quickly open a file, folder, or project. Typically, you open VS Code within the context of a folder. To do this, from an open terminal or command prompt, navigate to your project folder and type code .: Note: Users on macOS must first run a command ( Shell Command: Install 'code' command in PATH) ...

How do I run a VSCode project from the command line?

Now restart VS Code, and the next time you want to open any project from your command line, open your terminal and do the following: Type code . and hit Enter. Now your VSCode will launch an instance running your project.

How to open the current directory in a VS Code terminal?

To automatically open the current directory in a VS code terminal: 1. Launch the VS code app, then press “Ctrl+`” to open a terminal. 2. From the menu bar, select “View” > “Command Palette.” 3. Start typing “Shell” or “Shell Command” into the search box. 4. Select “Shell Command: install “code: command in PATH.”

How do I open a new terminal in Visual Studio Code?

Use the View > Terminal menu command. From the Command Palette ( Ctrl+Shift+P ), use the View: Toggle Terminal command. You can create a new terminal via the Terminal menu with Terminal > New Terminal. Note: Open an external terminal with the Ctrl+Shift+C keyboard shortcut if you prefer to work outside VS Code.


3 Answers

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
like image 58
x00 Avatar answered Sep 29 '22 21:09

x00


You can do the following:

"terminal.integrated.shellArgs.windows": ["start-ssh-agent.cmd"]

Modified from: https://code.visualstudio.com/docs/editor/integrated-terminal#_shell-arguments

like image 18
Jacob Bolda Avatar answered Sep 25 '22 21:09

Jacob Bolda


I actually found a pretty neat Linux solution for this. It should also work on Windows if you use a shell like Bash. I'm not sure if it's possible using vanilla CMD.

Add something like this to your .bashrc or .zshrc:

#
# Allow parent to initialize shell
#
# This is awesome for opening terminals in VSCode.
#
if [[ -n $ZSH_INIT_COMMAND ]]; then
    echo "Running: $ZSH_INIT_COMMAND"
    eval "$ZSH_INIT_COMMAND"
fi

Now, in your VSCode workspace setting, you can set an environment variable like this:

"terminal.integrated.env.linux": {
    "ZSH_INIT_COMMAND": "source dev-environment-setup.sh"
}

Now the script "dev-environment-setup.sh" will be automatically sourced in all new VSCode terminal windows.

like image 18
Hubro Avatar answered Sep 29 '22 21:09

Hubro