如何在Visual Studio Code中使用Cmder?

51

在工作中,存在一项企业安全政策,规定所有可执行文件只能从 C:\Program FilesC:\Program Files (x86) 中运行。

在 Visual Studio Code 中,使用以下设置: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极好的文件/目录命名规则允许使用空格,因此很难指向其中一个Program File路径。

VSCode不喜欢您转义空格字符,因此此代码会导致错误字符串中的无效转义字符。当我尝试将属性更改为以下内容时:

{
    ...
    "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?


修复在此处:https://dev59.com/OKbja4cB1Zd3GeqPkrmg#48104881?noredirect=1#comment83209354_48104881 - Adson Cicilioti
1
@Adson,谢谢?我已经发布了我的问题的解决方案,而你提供的答案直到我提问5个月后才出现。 - homersimpson
1
将整个路径用引号括起来接近但不完全是正确的语法。引号应该包围包含空格的特定文本块,即 "C:\\\"Program Files (x86)\"\\Cmder\\vendor\\init.bat"。我知道这个问题已经以其他方式解决,但我希望这个小提示仍然有用。 - cg_and
2
这里是官方Cmder指南:https://github.com/cmderdev/cmder/wiki/Seamless-VS-Code-Integration - David Refoua
14个回答

75

在互联网上搜索答案后,我没有找到解决方案,但我自己解决了问题,并想在这里发布它以供其他人查看,因为我发现来自不同论坛的人都有同样的问题,但找不到答案。

在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),并在VS Code的settings.json中设置以下内容:

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

如何在集成终端中成功加载Cmder:

Cmder在VS Code集成终端中成功加载的图片。


你是如何在VSCode中实现Cmder的颜色和外观的?我按照您的步骤进行了操作,虽然成功在控制台运行了Cmder,但显示的全是白色。 - David Martinez
1
@David Martinez,请尝试将颜色方案更改为较暗的一个。请参阅此文章:https://code.visualstudio.com/docs/getstarted/themes - homersimpson
2
如果你找不到C:/Windows/Sysnative文件夹,请尝试使用以下命令:C:\\WINDOWS\\SysWOW64\\cmd.exe - feyzullahyildiz
有一份官方文档介绍如何在 VS Code 中集成 Cmder:https://github.com/cmderdev/cmder/wiki/Seamless-VS-Code-Integration - addmoss

24

另一种解决方案是将您的cmder位置设置为新路径。

image was from nikrolls when giving the solution

只需在您的settings.json中进行设置即可。

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

你可以在cmder的Github问题页面找到它。



17

这是一种新方法来完成"2021"的任务。

相关文章请参考此文

步骤1: 下载 Cmder

步骤2:将 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",

1
运行良好,但是否有任何方法可以在打开新终端时删除初始日志记录? - g-otn
@g-otn 你可以更新 %CMDER_ROOT%/bin/vscode_init_args.cmd 文件并注释掉最后几行。 - MrSnoozles
2022年对我有用。记得将C:\替换为实际的cmdr路径,如果它不在根目录中。 - Goku

11

另一种方法。

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

示例:

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

官方Cmder维基上摘录:

路径中的空格

注意: Windows中的命令行解释器存在一些关于路径中包含空格的问题,例如 C:\Program Files (x86)\Cmder。我们不建议将Cmder安装在包含空格的路径下。

相反,我们建议将Cmder安装在不含任何空格的路径下,例如: C:\apps\CmderC:\tools\Cmder,以避免与VS Code发生任何冲突。

如果出于某种原因确实需要从路径中包含空格的位置启动Cmder,则可能需要在每个空格前面加上一个^符号,如此settings.json文件中的C:\\Example Directory for Test\\Cmder将变成C:\\Example^ Directory^ for^ Test\\Cmder


3
根据我的经验,您需要转义括号和空格。这是我如何使用 C:\Program Files (x86)\Cmder"C:\\Program^ Files^ ^(x86^)\\Cmder". - Akira

3
一个非常简单的解决方案(来源:https://github.com/Microsoft/vscode/issues/12006#issuecomment-315539390):
在您的 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


3

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",

2

Cmder与VSCode 2021

  • 步骤1:下载Cmder
  • 步骤2:将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",
  • 步骤五:重新启动VSCode
  • 步骤六:愉快地编码吧!

2

这对我很有帮助。我的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设置中。享受它!

之前的解决方案对我没有用,终端显示了一个关于无法访问“C:\Config”的错误消息。然而,这个解决方案对我起作用了。 - Kevin Peter

0

@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"
]

0

这个解决方案在打开终端时很好,但会破坏通过带有参数的插件启动的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
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接