【问题标题】:VS Code - Cygwin as Integrated TerminalVS Code - Cygwin 作为集成终端
【发布时间】:2018-02-14 03:47:47
【问题描述】:

我想在我的 Windows 笔记本电脑上使用 Cygwin 作为 Visual Studio Code 上的集成终端(因为这可以让我使用 Linux 终端命令 git 和 G++ 等)但是当我为 "terminal.integrated.shell.windows": 设置值时到 Cygwin 应用程序的地址 (.exe) 然后它会打开一个新的 Cygwin 终端,而不是留在 VS Code 中。

所以我的问题是:我可以使用集成到 VS Code 终端中的 Cygwin 并使用它在其上使用命令(mkdirrm 等),但也可以使用 git 命令并将其用作集成编译器和调试器(一般来说,但至少对于 C++)?我该怎么办?

【问题讨论】:

  • 您的计算机上安装了 Windows 10 吗?如果是这样,您可以只使用 WSL 而不是 Cygwin。
  • 此外,@user2766296 我不相信 VS Code 无论如何都可以与 Cygwin 作为终端一起使用。
  • @ifconfig,确实如此。我刚试过(见下面的答案),效果很好。甚至外壳颜色 (.dircolors) 也有效。

标签: windows git g++ cygwin visual-studio-code


【解决方案1】:

自 VS Code 1.55(2021 年 3 月)以来,使用终端配置文件:

"terminal.integrated.profiles.windows": {
  "Cygwin": {
    "path": "C:\\cygwin\\bin\\bash.exe",
    "args": ["--login"],
    "env": {"CHERE_INVOKING": "1"}
  }
}

【讨论】:

    【解决方案2】:

    对于 VS Code v1.60,采用以下方法:"

        "terminal.integrated.profiles.windows": {
          "Cygwin": {
            "source": "PowerShell",
            "args": ["C:\\cygwin\\cygwin.bat -i /Cygwin-Terminal.ico -"]
           }
         }
    

    缺点:打开的目录将是根文件夹。

    【讨论】:

      【解决方案3】:

      如果您取出以下部分,终端将在您打开的项目中启动。

      // Make it a login shell
      /*"terminal.integrated.shellArgs.windows": [
          "--login"
        "-l"
      ]*/,
      

      【讨论】:

        【解决方案4】:

        VS Code 只允许您一次设置一个默认终端配置,并且用户可能希望在任何时候都可以使用多个 shell,例如 CMD、Powershell 和 Cygwin Bash,最好使用 Visual Studio代码扩展名为Shell Launcher

        此工具可让您随时启动任意数量的 shell。首先,您需要将 CTRL-SHIFT-T 热键重新分配给 shellLauncher 或使用其他未使用的热键。

        然后,进入 VS Code 的 settings.json 并添加以下块:

        "shellLauncher.shells.windows": [
          {
            "shell": "C:\\Windows\\System32\\cmd.exe",
            "label": "cmd"
          },
          {
            "shell": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
            "label": "PowerShell"
          },
          {
            "shell": "C:\\cygwin\\bin\\bash.exe",
            "args": ["-l"],
            "env": {"CHERE_INVOKING": "1"},
            "label": "Cygwin Bash"
          }
        ],
        

        注意:根据需要更改上述路径

        现在,当您点击分配的热键时,您将获得配置的可用终端的下拉列表。

        【讨论】:

          【解决方案5】:

          结合以上答案,这是我的工作配置。

          {
              "terminal.integrated.shell.windows": "C:\\cygwin\\bin\\bash.exe",
              "terminal.integrated.env.windows": {
                  "CHERE_INVOKING": "1"
              },
              "terminal.integrated.shellArgs.windows": [
                  "--login",
                  "-i"
              ],
          }
          

          {在 ubuntu 18.04lts 测试,在 Virtualbox 5.2.12 中运行 Windows 7 Ultimate 32bt}

          【讨论】:

          • Bash 选项:-i Force the shell to run interactively--login Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell with ‘exec -l bash’. When the shell is not interactive, the login shell startup files will be executed. ‘exec bash -l’ or ‘exec bash --login’ will replace the current shell with a Bash login shell.
          • 对于 64 位 Cygwin,请记住将 Cygwin 与 cygwin64 一起放置,否则,这似乎到目前为止有效。
          • visual studio官网正确配置代码:code.visualstudio.com/docs/editor/integrated-terminal
          • 这警告您正在使用已弃用的设置,但我找不到解决方法。
          【解决方案6】:

          这些配置设置对我有用:

          {
            // start bash, not the mintty, or you'll get a new window
            "terminal.integrated.shell.windows": "C:\\cygwin\\bin\\bash.exe",
            // Use this to keep bash from doing a 'cd ${HOME}'
            "terminal.integrated.env.windows": {
              "CHERE_INVOKING": "1"
            },
            // Make it a login shell
            "terminal.integrated.shellArgs.windows": [
              "-l"
            ],
          }
          

          【讨论】:

          • 有没有办法将此添加为额外的 shell 选项,以便您在创建新终端窗口时可以选择使用 cmd 或 bash?
          • 另外,对于 64 位 Cygwin 安装,路径可能是 "C:\\cygwin64\\bin\\bash.exe"
          • 这个设置可以放在workspace文件.code-workspace中吗?那会很有帮助。
          • 您要将这些设置添加到哪个文件?
          【解决方案7】:

          您可以直接调用Cygwin.bat 而不会出现 ENV 问题:

          {
              // Replace with your Cygwin.bat file path 
              "terminal.integrated.shell.windows": "C:\\cygwin64\\Cygwin.bat",
          }
          

          确保 BAT 脚本适合您的 Cygwin。

          【讨论】:

          • 但是终端>运行活动文件不再起作用我该如何解决这个@Nick Tsai
          • @Mark,恐怕集成会失去Run Active File 功能,但只有Run Selected Text 有效。
          猜你喜欢
          • 1970-01-01
          • 2023-01-25
          • 1970-01-01
          • 1970-01-01
          • 2017-06-25
          • 2017-07-07
          • 2021-08-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多