如何在Visual Studio代码中使用Cmder?

How to use Cmder in Visual Studio Code?

在工作中,有一个企业安全策略,其中所有可执行文件只允许 运行 出 C:\Program FilesC:\Program Files (x86)

在Visual Studio代码中,在settings.json中使用以下设置:

{
    "terminal.integrated.shell.windows": "C:\Windows\Sysnative\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k C:\Program Files (x86)\Cmder\vendor\init.bat"
    ]
}

...在集成终端初始化时,我收到以下错误消息:

'C:\Program' is not recognized as an internal or external command, 
operable program or batch file.

由于 Windows' 很棒的 file/directory 命名约定允许 spaces,很难指向 Program File 路径之一。

VSCode 不喜欢你转义 space 字符,这段代码给我错误 Invalid escape character in string。当我尝试将 属性 更改为:

{
    ...
    "terminal.integrated.shellArgs.windows": [
        "/k C:\Program\ Files\ (x86)\Cmder\vendor\init.bat"
    ]
}

...我收到以下错误消息:

'C:\ProgramFiles' is not recognized as an internal or external command,
operable program or batch file.

最后,尝试用这样的引号将路径括起来:

{
    ...
    "terminal.integrated.shellArgs.windows": [
        "/k \"C:\Program Files (x86)\Cmder\vendor\init.bat\""
    ]
}

...给我这个错误信息:

'\"C:\Program Files (x86)\Cmder\vendor\init.bat\""' is not recognized as an 
internal or external command,
operable program or batch file.

有什么方法可以在VSCode中集成Cmder吗?

在网上搜索答案后,我找不到解决方案,但我想通了,我想我可以 post 在这里让其他人看到,因为我看到来自不同国家的人论坛有同样的问题,但没有答案。

在Windows中,dir命令有一个/X,其中指出:

  /X          This displays the short names generated for non-8dot3 file
              names.  The format is that of /N with the short name inserted
              before the long name. If no short name is present, blanks are
              displayed in its place.

因此,在 C:\ 上执行 dir /X 命令会显示以下内容:

C:\>dir /X
 Volume in drive C is OSDisk
 Volume Serial Number is XXXX-XXXX

 Directory of C:\

...
08/17/2017  08:02 PM    <DIR>          PROGRA~1     Program Files
08/09/2017  03:58 PM    <DIR>          PROGRA~2     Program Files (x86)
...

您可以使用目录短名称 PROGRA~2 替代 Program Files (x86),并在您的 settings.json 中为 VS Code 进行以下设置:

{
    "terminal.integrated.shell.windows": "C:\Windows\Sysnative\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k C:\PROGRA~2\Cmder\vendor\init.bat"
    ]
}

集成终端中哪个加载Cmder成功:

另一个解决方案是您可以将您的 cmder 位置设置为新路径

然后在 settings.json

中设置
"terminal.integrated.shell.windows": "C:\Windows\system32\cmd.exe",
"terminal.integrated.shellArgs.windows": [
    "/k %CMDER_ROOT%\vendor\init.bat"
]

你可以在 cmder github issue

上找到它

这对我有用。我的Cmder根目录:D:\soft\cmder,注意啦!

"terminal.integrated.env.windows": {"CMDER_ROOT": "D:\soft\cmder"},
"terminal.integrated.shellArgs.windows": ["/k D:\soft\cmder\vendor\init.bat"],

将其添加到您的 VSCode 设置中。好好享受吧!

另一种方法。

Cmder 团队建议在路径中的每个 space 之前添加一个 ^ 字符,而不是使用 8dot3 命名方法。

示例:

{
    "terminal.integrated.shell.windows": "C:\Windows\Sysnative\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k C:\Program Files^ (x86)\Cmder\vendor\init.bat"
    ]
}

取自official Cmder wiki:

Spaces in path

CAUTION: The command line interpreter in Windows has some issues with spaces in the path, such as C:\Program Files (x86)\Cmder. We do not recommended to install Cmder in a path that contains spaces.

Instead, we recommend installing Cmder in a path that does not contain any spaces, such as: C:\apps\Cmder or C:\tools\Cmder to avoid any conflicts with VS Code.

If you for some reason really need to launch Cmder from a path with spaces, you might need to prepend a ^ symbol before each space, such that C:\Example Directory for Test\Cmder will become C:\Example^ Directory^ for^ Test\Cmder in your settings.json file.

https://github.com/cmderdev/cmder/wiki/Seamless-VS-Code-Integration

 "terminal.integrated.shell.windows": "cmd.exe",

  "terminal.integrated.env.windows": {
  "CMDER_ROOT": "[cmder_root]"
  },
  "terminal.integrated.shellArgs.windows": [
    "/k [cmder_root]\vendor\init.bat"
  ],

将 [cmder_root] 替换为您的 Cmder 安装目录。

第二种解决方案

"terminal.integrated.shell.windows": "C:\Program Files\cmder\vendor\git-for-windows\bin\bash.exe",

一个非常简单的解决方案 (source):

使用以下代码在 cmder 文件夹 vscode.bat 的根目录中创建一个文件。

@echo off
SET CMDER_ROOT=C:\cmder <--your path to cmder
"%CMDER_ROOT%\vendor\init.bat"

然后在您的 vscode 设置中将以下内容添加到您的 settings.json

"terminal.integrated.shell.windows": "C:\WINDOWS\sysnative\cmd.exe",
 "terminal.integrated.shellArgs.windows": ["/K", "C:\cmder\vscode.bat"] <-- your path

您还可以通过在 "terminal.integrated.shellArgs.windows" 中输入和注释掉 cmdcmder 之间轻松切换。

我找到的最佳解决方案: 快速简单。

"terminal.external.windowsExec": "C:\Utils\Cmder\Cmder.exe",
"terminal.integrated.shell.windows": "C:\WINDOWS\sysnative\cmd.exe"
"terminal.integrated.shellArgs.windows" : ["/K","C:\Utils\cmder\vendor\init.bat"],

这个解决方案在开放式终端上很好,但会破坏通过插件启动的 cmd 调用,这些调用带有 npm build - lint 等参数

修复它的解决方案是创建一个自定义 init.bat 来包装这些调用并在 sellArgs 上引用它。

settings.json

"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [
    "/K C:\SoftDev\App\Cmder\vendor\vstudio.bat"
],

C:\SoftDev\App\cmder\vendor\vstudio.bat

@echo off
if "%1" == "" (
    C:\SoftDev\App\cmder\vendor\init.bat
) else (
    cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
    exit
)

Windows 中的命令行解释器在路径 space 中存在一些问题,例如 C:\Program Files\Cmder 或 C:\Program Files (x86)\命令。相反,创建一个新文件夹,如 "apps" 并使用没有 space 的路径,如 c:\apps

@Ari Maulana 很有魅力...

在变量上添加 "CMDER_ROOT" 并在 settings.json

中设置
"terminal.integrated.shell.windows": "C:\Windows\system32\cmd.exe",
"terminal.integrated.shellArgs.windows": [
    "/k %CMDER_ROOT%\vendor\init.bat"
]

有两个扩展可以将 Cmder 包含到 VS Code 中,但是 none 它们中的任何一个都可以在 Win 7 中工作(我知道它已经很老了),而这里的一些答案对我的情况没有帮助。

位置详情:

cmd.exe = C:\Windows\System32\cmd.exe
I am using a portable version of Cmder in C:\cmder\Cmder.exe
init.bat = C:\cmder\vendor\init.bat
setting.json = C:\Users\Admin\AppData\Roaming\Code\User\settings.json

这是我的settings.json

{
    "window.zoomLevel": 0,
    "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k C:\cmder\vendor\init.bat"
    ]
}

以及它正常工作的屏幕截图

Cmder integrated into VS Code

注意:我不需要将 Cmder 添加到环境路径中

这是实现“2021”的新方法

this article相关

第 1 步:Download Cmder.

第二步:将Cmder保存到C:\盘

第 3 步:在 VSCode 中打开 Settings.json(文件 - 首选项 - 设置 ...)

第 4 步:输入此参数:

"terminal.integrated.profiles.windows": {
    "Cmder": {
      "path": "${env:windir}\System32\cmd.exe",
      "args": ["/k", "C:\cmder\vendor\bin\vscode_init.cmd"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "Cmder",

Cmder VSCode 2021

  • 第 1 步:下载 Cmder
  • 第二步:将Cmder保存到C:\cmder
  • 第 3 步:在 VSCode
  • 中打开 Settings.json
  • 步骤 4:创建 VSCode 集成终端设置
"terminal.integrated.profiles.windows": {
    "Cmder": {
      "path": "${env:windir}\System32\cmd.exe",
      "args": ["/k", "C:\cmder\vendor\bin\vscode_init.cmd"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "Cmder",
  • 第 5 步:重启 VSCode
  • 第 6 步:编码愉快!

在 visual studio 中按 ctrl + p

写/转到settings.json

把这个粘贴进去

"terminal.integrated.profiles.windows": {
  "Cmder": {
    "path": "${env:windir}\System32\cmd.exe",
    "args": ["/k", "C:\cmder\vendor\git-for-windows\bin\bash.exe"]
  }
},
"terminal.integrated.defaultProfile.windows": "Cmder",

确保 "args" 是您在计算机中放置 cmder 的目录。