简体   繁体   English

WSL2:使用 *.code-workspace 文件从命令行启动 VSCode

[英]WSL2: Launching VSCode from the command line using a *.code-workspace file

I have Windows 10 + WSL2 with the latest version of VSCode.我有 Windows 10 + WSL2 和最新版本的 VSCode。 I also have a simple.code-workspace file which I can double-click (from within the windows file-explorer) and launch VSCode in a way that it pops-up already attached to a specific docker-container that is up-and-running.我还有一个 simple.code-workspace 文件,我可以双击它(从 windows 文件资源管理器中)并以弹出的方式启动 VSCode,它已经附加到一个特定的 docker-container,它是 up-and-跑步。

This works great with double-clicking on the.code-workspace file:双击 .code-workspace 文件效果很好:

{
    "folders": [
        {
            "uri": "vscode-remote://attached-container+7b2...27d/workspace/foobar"
        }
    ],
    "remoteAuthority": "attached-container+7b2...27d",
    "settings": {}
}

Apart from double-clicking I can also invoke VSCode from Windows command-line (dos) and this works ok too:除了双击,我还可以从 Windows 命令行 (dos) 调用 VSCode,这也可以正常工作:

  "C:\Program Files\Microsoft VS Code\Code.exe" "C:\path\to\foobar.code-workspace"

However when I try to open the workspace from within WSL2:但是,当我尝试从 WSL2 中打开工作区时:

  code ./foobar.code-workspace

Even though VSCode pops-up the workspace doesn't open properly.即使 VSCode 弹出,工作区也无法正常打开。 What should I do to make the WSL2 (bash) command-line work the same way as in Windows?我应该怎么做才能使 WSL2 (bash) 命令行以与 Windows 中相同的方式工作?

And to answer my own question:并回答我自己的问题:

  "/mnt/c/Program Files/Microsoft VS Code/Code.exe"   ./foobar.code-workspace

Strangely enough this does the trick.奇怪的是,这可以解决问题。

To make the script a bit more dynamic I guess one could write something like:为了使脚本更具动态性,我想可以编写如下内容:

  "$(which code | xargs -0 dirname | xargs -0 dirname)/Code.exe"  ./foobar.code-workspace

Hope this helps some folks out there save a few hours worth of time.希望这可以帮助那里的一些人节省几个小时的时间。

At the end of the day wsl is installed on top of a windows environment you could also a.bat or.ps1 file and adds the commands they require without the need for the workspace.最后,wsl 安装在 windows 环境之上,您还可以使用 .bat 或 .ps1 文件并添加它们所需的命令,而无需工作空间。 For example I have my script like this: wsl (cd ~/Projects; code./test; command3; command4) exit inside the parentheses put all the commands you need and exit closes the terminal例如我有这样的脚本:wsl (cd ~/Projects; code./test; command3; command4) exit inside the parentheses put all the commands you need and exit closes the terminal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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