Skip to content

Seamless VS Code Integration

David Refoua edited this page Nov 25, 2022 · 63 revisions

VS Code Terminal

To run Cmder shell as the VS Code terminal, follow the instructions below:

_Before you continue, make sure to update both VS Code and Cmder to the latest version, as the configuration has been changed in the recent versions.

  1. Press Ctrl + , (Control-Comma) to access VS Code Settings, then search for settings.json, then click Edit in settings.json.

    Tip: You can also open the settings.json file from Command Pallet (Ctrl + Shift + P), type settings.json and then hit Enter.

  2. In the settings.json file, append or modify the following into your configuration file:

    👉 Note: Please make sure that CMDER_ROOT is configured under your Windows Paths. Alternatively, you can also substitute the %CMDER_ROOT% variable with the path to your Cmder installation, although not recommended.

    "terminal.integrated.defaultProfile.windows": "Cmder",
    
    "terminal.integrated.profiles.windows": {
     "Cmder": {
     	"name": "Cmder",
     	"path": [
     		"${env:windir}\\Sysnative\\cmd.exe",
     		"${env:windir}\\System32\\cmd.exe"
     	],
     	"args": ["/k", "%CMDER_ROOT%\\vendor\\bin\\vscode_init.cmd"],
     	"icon": "terminal-cmd",
     	"color": "terminal.ansiGreen"
     },
    },

    example of using an absolute path instead of CMDER_ROOT variable:

    "args": ["/k", "C:\\Cmder\\vendor\\bin\\vscode_init.cmd"]

    The configured values will produce a default Cmder terminal using Cmd.exe (i.e. clink) shell, as if you launched Cmder.exe.

    👉 Please note the use of cmd.exe instead of cmder.exe in the settings.json. More Information…

    Tip: The terminal.integrated.profiles.windows and terminal.integrated.defaultProfile.windows configs are only valid with Windows-based shells and will have no effect on other operating systems such as Linux and macOS, so you can safely share your VS Code config across different machines.

    Note the double backslashes which are required. Alternatively, you can use a single forward slash (/) instead of double slashes (\\)

  3. Press Ctrl + ` (Control-Tilde) to open VS Code's terminal.

    👉 Note: Cmder and VS Code both have a hotkey mapping for Ctrl + ` (Control-Tilde). You need to change Cmder's mapping if you want to use VS Code's mapping. More Information…

  4. A generic vscode_init_args.cmd is created in one of the paths below on the first run of vscode_init.cmd if it does not already exist. (assuming a recent version of Cmder)

    • Edit either the %cmder_root%\bin\vscode_init_args.cmd or the %cmder_user_bin%\vscode_init_args.cmd script to set appropriate Cmder settings for VS Code according to the scripts internal documentation.

      User Configured Cmder vscode_init_args.cmd Example:

      @echo off
      echo Applying Cmder VSCode settings from '%~0'...
      
      if defined CMDER_CONFIGURED (
          rem Set Cmder settings here for when VSCode is launched inside Cmder.
          rem The below causes Cmder to not rerun '%cmder_root%\config\profile.d\*.cmd' or 'user_profile.cmd' scripts because Cmder already ran them.
          set CMDER_CONFIGURED=2
      ) else (
          rem Set Cmder settings here for when VSCode is launched from outside Cmder.
          rem Do NOT add optional Unix tools to the path the same as 'init.bat /nix_tools=0'
          set nix_tools=0
          rem Enable Cmder `fast_init` same as 'init.bat /f'
          set fast_init=1
      
          rem Enable Cmder User config same as 'Cmder.exe /c c:\Users\%USERNAME%\.cmder'
          rem NOTE: This is non-default config and results in a non-portable config.
          rem       This is for example purposes only and should only be done if you understand WHY this is done!
          set cmder_user_bin=c:\Users\%USERNAME%\.cmder\bin
          set cmder_user_config=c:\Users\%USERNAME%\.cmder\config
      )
      
      set verbose_output=1
      
      rem Set all required Cmder VSCode terminal environment settings above this line.
      echo Applying Cmder VSCode settings is complete!
    • Close the previous VSCode Cmder terminal by typing exit Enter in the terminal.

    • Press Ctrl + ` (Control-Tilde) to open VS Code's terminal.

    👉 Note: This might cause some verbose information messages to be displayed every time you open the Cmder terminal in VS Code, please refer to this discussion for more information. You'll need to set verbose_output to 0, and comment out any echo lines in vscode_init_args.cmd that you don't need.

You may now use Cmder with VS Code.

For more information/issues, read our guide. Here's a demo.

 

Spaces in the 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 recommend installing 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 with Spaces\\Cmder will become C:\\Example^ Directory^ with^ Spaces\\Cmder in your settings.json file.

You may also have to escape the parentheses ( and ) as well as spaces. Here's an example:

C:\Program Files (x86)\Cmder  →  "C:\\Program^ Files^ ^(x86^)\\Cmder"

💾 Alternatively you could use 8.3 filename convention:

C:\Program Files\Cmder  →  "C:\\PROGRA~1\\Cmder"

(Use PROGRA~2 for Program Files (x86))

👉 Note: There is currently an open issue in VS Code where command arguments are not properly escaped if they contain a variable that has spaces in its value. For example, if the CMDER_ROOT variable is set to a path that contains spaces, using the ${env:cmder_root} or %CMDER_ROOT% variables will result in improperly escaped arguments and thus Cmder shell will not run.

Please see vscode/#164696 for more details.

 

Install VS Code in portable mode

VS Code is able to work in portable mode. To install it as portable, just follow the official instructions and configure Cmder accordingly.

  • Extract the VS Code zip file to %CMDER_ROOT%\bin\vscode

  • Create %CMDER_ROOT%\bin\vscode\data to enable portable mode.

  • Create the below file in %cmder_root%\bin\vscode.cmd for launching VS Code Portable from Cmder:

    @call "%cmder_root%\bin\vscode\bin\code.cmd"
    
  • Start Cmder and type vscode.

  • Click FilePreferences, then click Edit in settings.json.

  • Add the below configuration to settings.json:

      "terminal.integrated.shell.windows": "cmd.exe",
      "terminal.integrated.shellArgs.windows": [
          "/K",
          "%CMDER_ROOT%/vendor/bin/vscode_init.cmd",
      ]

 

Use PowerShell instead of cmd.exe

Microsoft is trying to make PowerShell the default shell for Windows 10 and 11. To integrate Cmder with VS Code using PowerShell, use the following settings. Everything else is the same as above.

    "terminal.integrated.defaultProfile.windows": "Cmder PowerShell",

    "terminal.integrated.profiles.windows": [
	"Cmder PowerShell": {
		"name": "PowerShell",
		"source": "PowerShell",
		"args": [
			"-ExecutionPolicy", "Bypass",
			"-NoLogo", "-NoProfile", "-NoExit",
			"-Command", "Invoke-Expression '. ''${env:CMDER_ROOT}\\vendor\\profile.ps1'''"
		],
		"icon": "terminal-powershell",
		"color": "terminal.ansiGreen"
	},
    ]

Note the double backslashes which are required.

Of course, you can substitute ${env:cmder_root} with the absolute path of the location of your installation directory, but we instead recommend to define it.

For example, note that C:\apps\cmder\ would be the value of %CMDER_ROOT% ($env:CMDER_ROOT in PowerShell).

 

Automatically open VS Code at startup, as well as its built-in terminal

Related to issue #1758, make sure your Cmder has the "%cexec%" feature.

  1. Put VS Code into %cmder_root%\bin\vsCode folder for example

    (%cmder_root% is the environment variable of your Cmder installation directory, same as above.)

  2. Use the following variables in the configuration file:

    "args": ["/k", "${env:cmder_root}\\vendor\\bin\\vscode_init.bat", "/noautorun"],
  3. Use the following commands in user-profile.cmd to run VS Code at startup:

    %ccall% NOT "/noautorun" "start" "%cmder_root%\bin\vsCode\Code.exe %*"

 

Use Cmder Embedded Git in VS Code

Update or add the following to your user settings:

Note: Change [cmder_root] portion below with either the environment variable %cmder_root% or the absolute path to the installation folder for Cmder.

    "git.enabled": true,
    "git.path": "[cmder_root]\\vendor\\git-for-windows\\cmd\\git.exe",

if it does not work, you can try another way

    "terminal.integrated.shell.windows": "[cmder_root]\\vendor\\git-for-windows\\bin\\bash.exe",

(This will give you access to the built-in bash shell, with GNU/Linux tools such as ls, cp, mv, pwd, etc.)

👉 Note: Make sure to substitute [cmder_root] with your Cmder installation directory. You can define an environment variable to use as [cmder_root].

Optional Steps

Here's a list of unofficial extensions that you can install:

  • VSCodeCmder – Commands for VS Code to make it easier to use in a keyboard-only workflow
Clone this wiki locally