59

I want to open a directory using cmd in visual studio code but its giving me an error in cmd. So, what setting is required for that?

I have performed below command

D:\RND>code .
Allen M
  • 1,423
  • 9
  • 15
baj9032
  • 2,414
  • 3
  • 22
  • 40

15 Answers15

107

It needs to be setup so that Code is found in your PATH. If you're on a mac system, do the following (for windows systems, read below):

  1. Launch VS Code
  2. Open up command palette (press F1) and type shell command to find Shell Command: Install 'code' command in PATH command.
  3. Restart terminal

VSCode screen capture

If you're on Windows, you can also set the PATH manually by adding:

C:\Program Files (x86)\Microsoft VS Code\bin

to your PATH environment variable.

  1. Open command prompt
  2. Type the command - setx path "%path%;C:\Program Files (x86)\Microsoft VS Code\bin"
  3. Restart terminal

Update: If you're on Windows 10 and VSCode 1.47.2 and above, the new PATH has to be set to:

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
Tejashree
  • 750
  • 12
  • 14
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • @AnkitBhanderi see picture. you can't find that? – d.moncada Oct 09 '17 at 05:06
  • @AnkitBhanderi does it now work, or am I right that you can't find it? – d.moncada Oct 09 '17 at 05:09
  • Had the same issue but in my case, the path variable was updated for local admin bcoz I installed it with local admin. – Ravi Khambhati Jan 03 '18 at 21:02
  • On my win 10 I had to edit the Path manually - Select Path under 'System Variables', click on edit, on the new window click new and add new path "C:\Program Files (x86)\Microsoft VS Code" – Avishay Apr 02 '18 at 20:19
  • Uninstall and again install visual code.. it will work – Ankur Shah May 16 '18 at 05:00
  • @d.moncada https://stackoverflow.com/questions/50182417/code-command-is-not-working/52501613#52501613 see no solution is working for me – Nikhil S Sep 25 '18 at 17:11
  • In my environment the path is **C:\Users\YourUserName\AppData\Local\Programs\Microsoft VS Code** – ilans Feb 01 '20 at 18:39
  • 1
    @ilans is correct. In Windows 10 and VSCode 1.47.2 at least, the PATH has to be `C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin`. – Paul Razvan Berg Jul 26 '20 at 09:07
  • In version 1.49.2 the command palette's shortcut is `shift` + `cmd` + `p` – Ahmet Sep 28 '20 at 15:02
  • In my experience, on Windows 10 and Mac OS Big Sur, I have not been able to find the command palette command to add 'code' to the PATH. Instead, I had to manually edit the path. NOTE: Once your path is properly edited, you will need to restart any shell instances for the change to take effect therein. – Shad Feb 09 '21 at 16:13
  • 2
    slightly different here: `C:\Users\\AppData\Local\Programs\Microsoft VS Code\_\bin` worked for my machine – nate Feb 09 '22 at 20:32
  • In my case I forgot to restart the Git Bash – Rahul Chouhan Mar 20 '22 at 09:32
  • 1
    thanks for destroying my dev environment entire path variable is lost!! guess I'm not sleeping tonight! DO NOT TRUST THIS!!!! – ParsaGachkar Mar 25 '22 at 18:40
  • Now is worst!, my CMD Does not find anything, neither other path even if they are still on my path environment – Melvin Guerrero Jun 12 '22 at 03:31
21

if using Windows:

  1. Press the "Windows"-button
  2. Type "Visual Studio Code"
  3. Right click on "VS Code" (or "Visual Studio Code")
  4. Click on "Open file location"

You will then be directed to a folder, where either the VS Code program is or a VS Code shortcut is.

If it is the shortcut, then right click on it and again press "Open file location". Copy the path:

path picture

which in my situation will be: "C:\Program Files\Microsoft VS Code"

And then do as Chawki posted - paste it into your "Environmnet variables" under "Path".

screenshot of environment variable

This is just to show, that the two above might not be the solution for you (they weren't for me).

Should this still not work for some reason then try restarting the terminal and/or add "/bin" to the path you have added inside your environment variable.

nelion
  • 1,712
  • 4
  • 17
  • 37
20

For me it was due to not running cmd as an administrator. (Right click Command Prompt -> Run as Administrator)

user3428422
  • 4,300
  • 12
  • 55
  • 119
12

If you have the insider's editions of vscode installed, the command will be code-insiders.

Roy Touw
  • 148
  • 2
  • 5
11

O/S windows

Setting the environment variable path to C:\Users\chawki\AppData\Local\Programs\Microsoft VS Code\bin solve the issue.

Like this.

U.A
  • 2,991
  • 3
  • 24
  • 36
  • Everyone, do not do it, it literally removes all the Paths for different softwares when you add this environmental variable, click "Cancel" before proceeding. – Ali Guliyev Aug 20 '23 at 19:38
4

Is your VS Code corrupted?

This path issue may indicate that VS Code files are corrupted and you may face other issues. Especially, if code command was working before.

No Repair or Reinstall

According to Microsoft, repair is unsupported.

Ultimate solution

  1. Uninstall code from control panel.
  2. Download and install code using the latest installer.
  3. Make sure to checkbox the path checkbox.

enter image description here

Note: All users?

Download the system installer if you want to make code available to all users.

Finally, enjoy!

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
4

1.Enter "View advanced systems settings" into the box Type here to search

2.Click "View advanced systems settings"

3.Click Environment Variables

enter image description here

3.On System variables click button New Then enter the variable name and add the following path C:\Users\user_name\AppData\Local\Programs\Microsoft VS Code\bin

enter image description here

4.Click OK

5.Close All tab VS Code and open again or Restart

Trinh Hieu
  • 379
  • 3
  • 6
3

Make sure you know where is the installation of VS Code, it can be in Program Files (x86) if you installed the 32bits version or Program Files if you installed the 64bits version VS Code location Once you know that Run (Win+r) cmd and type: setx path "%path%;C:\Program Files (x86)\Microsoft VS Code"

or

setx path "%path%;C:\Program Files\Microsoft VS Code"

after that, you can type code . to open VS Code with the project of the current folder.

oswazteca
  • 47
  • 3
  • 2
    You should probably avoid using `setx path` because it [truncates the number of characters](https://superuser.com/questions/387619/overcoming-the-1024-character-limit-with-setx). – svict4 Sep 12 '18 at 02:11
3

I tried all the solutions but they didn't work. Turns out that Code was not installed on my system.

First go to the directory C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin and check if there is a folder named Microsoft VS Code. If there isn't any, download it from the website link attached below.

https://code.visualstudio.com/download

Adnan Arshad
  • 370
  • 3
  • 15
1

Notice that if you're using VS code insider's editions instead of using the command "code ." the command will be "code-insiders .".

That is obvisly after you set your PATH.

Ahmed Adel
  • 49
  • 6
1

Windows 11

  • Close the Visual Studio Code
  • Open the Environment variables (Control Panel > System and Security > System Properties)
  • Edit the variable "Path", creating a C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
  • Ok and ok in options and run VS Code;
  • For test write code in cmd, if to open VS Code is working.
Leffa
  • 369
  • 4
  • 7
0

The best solution was on Mac is to move MSCode to the applications folder, and on Win is to search for the location of MSCode installation ( like: C:\Users{username}\AppData\Local\Programs\Microsoft VS Code\bin ) then you set the Path.

setx path "%path%;C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vacak7
  • 11
  • 2
0

First, check if you're on Windows 10 & Set User variables to

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin

Give (OK) in environment variables and relaunch any unclosed terminals and type

code

Check if you have installed the VScode-insiders version

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code Insiders\bin

Give (OK) in environment variables and relaunch any unclosed terminals and type

code-insiders
0

In my case, I had two installs of VS Code in Windows 10. One was appended with (user).

I don't know how this happened but uninstalling the older version fixed my issue.

user1889992
  • 355
  • 3
  • 7
0

Just uninstall and reinstall the vs code it worked for me

Xorrier
  • 1
  • 1