74

I'm trying to change the default path of the integrated terminal in Visual Studio Code, but I'm not sure how to. I do know how to change it in the windows command prompt, but not in Visual Studio Code. I looked in user settings, but I can't find anything there to change.

The current default path is C:\Users\User_Name.

I'd like to change it to C:\Project.

How do I do this in Visual Studio Code?

12 Answers 12

83

Short answer

Edit the user preference "terminal.integrated.cwd": "" to the path that you want the integrated terminal to open to.


Long answer

The same answer, but the long step-by-step version,

In Visual Studio Code go to:

Menu FilePreferencesSettings


Now that you are in the "User Settings", using the "Search Settings" bar across the top of the window paste or type this:

terminal.integrated.cwd


It will list the following as a result:

// An explicit start path where the terminal will be launched, this is used
as the current working directory (cwd) for the shell process. This may be
particularly useful in workspace settings if the root directory is not a
convenient cwd.
"terminal.integrated.cwd": "",


You will notice that it will not let you type here to change this setting. That is because you can't change the default setting. You instead need to change your personal settings. Here's how...


Click the pencil icon to the left of the this option and then the "Copy to Settings" option that pops-up.


You should have a split screen in which the right side of the screen has the heading Place your settings here to overwrite the Default Settings. This is the correct place for you to make changes. You might already have a few personalized settings listed here.


When you clicked "Copy to Settings" it automatically added this line for you:

"terminal.integrated.cwd": ""

Notice that whichever item is last in this list will not have a trailing comma but any items before it in the list will require one.

FYI: you could have simply typed or copy/pasted this into the personalized settings yourself, but following these steps is the process to learn for changing other preferences as needed.


Now you are able to type to set the path you want to use. Make sure to use \\ in place of \ and you do not need the trailing \. For example including this line would always start your terminal in the baz directory:

{
    "terminal.integrated.cwd": "C:\\Users\\foo\\bar\\baz"
}


To apply the change, simply Save and restart Visual Studio Code.

3
  • 1
    FYI this answer uses Visual Studio Code version 1.17.1
    – ninj4 n00b
    Commented Oct 17, 2017 at 2:37
  • 2
    On a mac I didn't need the extra slash characters. The following worked: "terminal.integrated.cwd": "/Users/MyUserName/dev/ProjectName/WebUI". Replace MyUserName of course with your own. Quick way to get the path on mac is to make sure "Show Path Bar" is selected in finder view menu, and then right click on the last folder at the bottom of finder, and "Copy FolderName as Path Name" . Then you can use that path in the json settings of vscode. Hope this helps someone.
    – firecape
    Commented Nov 27, 2018 at 5:57
  • I just found terminal.integrated.cwd and it contained the offending location that I had set previously. I just hit delete and pressed enter and default behaviour was restored. The long answer didn't need completing.
    – John
    Commented Jan 5, 2023 at 12:32
33

As others have already explained, you can add a setting to change the default folder for your integrated terminal to start in. This setting also accepts Visual Studio Code variables, so to make a relative path from the root folder of your workspace you can use ${workspaceFolder}.

For example, for your terminal to always start in the subfolder mystart, your setting would be:

"terminal.integrated.cwd": "${workspaceFolder}/mystart"
2
  • 1
    Definitely the best answer. Useful information: besides restarting VS Code, always make sure you start a new terminal session because VS Code might restore your previous session and the working directory you had back then.
    – gyohza
    Commented Jun 9, 2022 at 13:07
  • 1
    Great answer. FYI on a windows machine I had to use double backslash instead of forwardslash. Commented Sep 7, 2022 at 12:14
21

Thanks for the original answer from Peter. Definitely helped ! It has now slightly changed in more recent versions of VS Code.

  1. Navigate to File -> Preferences -> Settings
  2. Type cwd in search
  3. Choose Terminal > Integrated: Cwd settings
  4. Type the default path you want to set in the text block below (simply, no need for double hashes to escape)
  5. No need for saving, It's automatic
  6. Restart VS Code

terminal cwd screen

1
  • this is the one that is working at 23/03/21 look at the picture, if not in the box just type your path. example: C:\Users\rober\onedrive nothing more
    – ROB
    Commented Mar 23, 2021 at 15:43
13

Try this option in the "Intergrated Terminal" section of Settings.

"terminal.integrated.cwd": "", 
6

You can also set it to a relative path to the open folder with

"terminal.integrated.cwd": "./example"

So if you do menu FileOpen Folder... → project, and open the terminal with your keyboard shortcut, it will open to ~/project/example automatically.

It can be a general folder like src you would always use, or a specific one (but then it would be best to save it in file .vscode/settings.json).

1

in code to pasd in project

"terminal.integrated.cwd": "./example"

best to save it in file .vscode/settings.json).

thanks read me.

1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 18 at 13:12
1

Msys2

On windows, when using Msys2 you may want to start the shell bash as me.

Symptom

I encountered the same effect after setting up Msys2. First it worked, but after some time (may be just after restart of vscode) it didn't work anymore. The initial directory was always ~.

Bash prompt:

<user>@<computer> MSYS ~

Cause

I had an incomplete setup in file C:\Users\...\AppData\Roaming\Code\User\settings.json:

{
...
"terminal.integrated.defaultProfile.windows": "bash (MSYS2)",
"terminal.integrated.profiles.windows": {
    "PowerShell": {
        ...
    },
    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    },
    "Git Bash": {
        "source": "Git Bash"
    },
    "bash (MSYS2)": {
        "path": "C:\\msys...\\usr\\bin\\bash.exe",
        "args": [
            "--login",
            "-i"
        ]
    }
},
...

Cure

I found this documentation here.

Hence, I just added "env": { "CHERE_INVOKING": "1" } entry to entry "bash (MSYS2)" as shown below:

"bash (MSYS2)": {
    "path": "C:\\msys64\\usr\\bin\\bash.exe",
    "args": [
        "--login",
        "-i"
    ], 
    "env": { "CHERE_INVOKING": "1" }
}

Why this? Explanation here.

Other terminals

For a lot of terminals it is documented here, as well.

Today the following are documented:

  • PowerShell
  • zsh
  • Cmder
  • Cygwin
  • Git Bash
  • WSL
0

The below option will help you do this.

Menu FilePreferencesSettings.

Add or edit the below setting.

terminal.integrated.shell.windows": ""

From the next terminal it will be reflected.

And add file .profile to your default shell, and add the default path to it.

More information can be found at Integrated Terminal.

0

If anyone else had trouble getting this to work with ${workspaceFolder} in Windows like I did, keep in mind that the starting directory you set in Visual Studio Code will be overridden by anything in your PowerShell profile. You can edit your PowerShell profile by typing

code $PROFILE

in your terminal. I got tripped up even more because I had two profiles. To list all of your PowerShell profiles, try the command

Get-ChildItem -Path $PROFILE.AllUsersAllHosts,$PROFILE.AllUsersCurrentHost,$PROFILE.CurrentUserAllHosts,$PROFILE.CurrentUserCurrentHost -Filter *.ps1

and look through any items that come up to see if any of them are changing your default path. Also, having two PowerShell profiles is not a good idea, so combined them into the one pointed to by $PROFILE (the other was in the same directory named profile.ps1.

Once this is ironed out, setting something like "terminal.integrated.cwd": "${workspaceFolder}" should work.

0
  1. Go to File > Preferences > Settings
  2. Type cwd in search bar
  3. Then choose Integrated: cwd and type the address
0

Go your VsCode: in with Ctrol + , keybinding go to Settings copypaste this on the search bar:

@feature:terminal cwd

In Terminal > Integrated: Cwd , put this:

${fileDirname}

source: https://code.visualstudio.com/docs/editor/variables-reference

0

Add this snippet to your Settings.json in Visual Studio Code:

"terminal.integrated.defaultProfile.windows": "PowerShell (Anaconda)",

    "terminal.integrated.profiles.windows": {
        "PowerShell (Anaconda)": {
          "source": "PowerShell",
          "args": [
            "-ExecutionPolicy"
            , "ByPass" 
            , "-NoExit"
            , "-Command"
            ,";& 'C:\\Users\\User_name\\anaconda3\\shell\\condabin\\conda-hook.ps1' ${env:CONDA_ENV}"
            ],
            "color": "terminal.ansiGreen"
        }
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented May 19 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.