Setting WSL to CWD in VSCode Terminal

Viewed 4509

I have ubuntu installed on my windows 10 machine and have been using vscode. I'd like to use the wsl integrated terminal. If I just open vscode and then a new wsl terminal it shows my path as: username@Computer:/mnt/c/Users/winusername

If I then open a project folder (not workspace), and then a new terminal it shows as: username@Computer:~

This isn't in my project folder location- is there a way to get the wsl integrated terminal to set the project location as the current working directory?

Git bash does this just fine if I use it as my terminal, like this:

winusername@Computer MINGW64 /d/my/project/path

But I'd like to use wsl.

5 Answers

Not exactly the same issue, but similar symptom due to a different cause.

When I'd open a project folder (not workspace) and land on my username@Computer:~ rather than the project dir that I just opened.

It was due to my pretty "clever" solution of placing a cd ~ in my .bashrc|.zshrc intending to replace the WSL default behavior of launching your terminal at the /mnt/... path instead of the WSL distro user dir (/home/username).

In other words:

WSl default behavior when you launch a new WSL distro terminal window is to start at the:

āžœ  windowsusername pwd
/mnt/...

I tried to "fix" that by adding the following to my .bashrc or .zshrc:

cd ~

That made my terminals to always open at my distro home dir which's convenient for me, but also messed up my vscode terminal since it was naturally always changing the directory to my distro home user dir /home/distrousername too before any other task.

āžœ  ~ pwd
/home/distrousername

A few extra side effects of that unthoughtful approach were my vscode debugger not able to attach to my nodejs server and my jest vscode extension not able to trigger the tests via extension utilities (e.g.. the Debug buttons that the extension adds to your *.test.js|ts or *.spec.js|ts).

Not a straight answer to the problem reported above, but might help someone dropping here for the same reasons that I did.

I agree with @AlanBueno This worked for me :-)

if [ "$NAME" != "Code" ]; then
    cd ~
else
    cd $OLDPWD
fi

I think your configuration is little screwed.

Check the terminal configuration about User settings and workspace settings.

You can find in an easy way on UI.

  1. File > Preference > Settings

  2. Check the default 'integrated terminal' path, and cwd value.

    in my case using wsl.exe for default terminal, not bash.exe

enter image description here enter image description here

  1. compare with User settings and Workspace settings on the upside on UI about same options keys.(You can compare in easy just tapping the title)

  2. if nothing trouble on your settings, i recommend this VSCODE extension. It helps open terminal with path with opened file. https://marketplace.visualstudio.com/items?itemName=Tyriar.vscode-terminal-here&ssr=false

I edited my settings.json like this and it did work:

"terminal.integrated.shell.windows": "${fullPathToExecutable}",
"terminal.integrated.shellArgs.windows": ["run"],
"terminal.integrated.cwd": "C:\\Users\\winusername"

I agree with both @AlanBueno and @Dexter Le Blanc Jr.

I think the following has proved to be more stable for me across multiple IDEs.

if [ "$NAME" != "Code" ]; then
    cd ~
fi
Related