简体   繁体   中英

VSCode: Execute command automatically on WSL terminal opening

I'd like to execute a command automatically every time I open the WSL terminal on Visual Studio Code (Windows 10).

I'm not using the Remote WSL extension , just the WSL terminal with this setting: "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe"

I tried unsuccessfully using terminal.integrated.shellArgs.windows setting.

Is there a way to do it?

You can put in settings.json:

{
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
    "terminal.integrated.shellArgs.windows": [
        "-e",
        "bash",
        "--rcfile",
        "/path/to/vscode.bashrc"
    ]
}

In vscode.bashrc

source $HOME/.bashrc
your-command

What's in settings.json asks vscode to run following command:

"C:\WINDOWS\System32\wsl.exe" -e bash --rcfile /path/to/vscode.bashrc

-e option of wsl runs "bash --rcfile /path/to/vscode.bashrc"

--rcfile for bash runs /path/to/vscode.bashrc instead of $HOME/.bashrc

For more details, see https://www.gnu.org/software/bash/manual/bash.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM