Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting WSL to CWD in VSCode Terminal

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.

like image 834
Nicros Avatar asked Sep 19 '18 02:09

Nicros


People also ask

How do I add VS Code to WSL PATH?

Visit the VS Code install page and select the 32 or 64 bit installer. Install Visual Studio Code on Windows (not in your WSL file system). When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL using the code command.

How to open a terminal in WSL from VS Code?

Opening a terminal in WSL from VS Code is simple. Once folder is opened in WSL, any terminal window you open in VS Code (Terminal > New Terminal) will automatically run in WSL rather than locally. You can also use the code command line from this same terminal window to perform a number of operations such as opening a new file or folder in WSL.

How do I open a WSL folder in Visual Studio Code?

Opening a folder inside the Windows Subsystem for Linux in VS Code is very similar to opening up a Windows folder from the command prompt. Open a WSL terminal window (using the start menu item or by typing wsl from the command prompt). Type code . in the terminal.

How do I debug a WSL file in VS Code?

Debugging in WSL # Once you've opened a folder in WSL, you can use VS Code's debugger in the same way you would when running the application locally. For example, if you select a launch configuration in launch.json and start debugging (F5), the application will start on remote host and attach the debugger to it.

What is remote WSL in VS Code?

Extensions inside of VS Code Remote The Remote-WSL extension splits VS Code into a “client-server” architecture, with the client (the user interface) running on your Windows machine and the server (your code, Git, plugins, etc) running remotely.


2 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.

like image 120
Alan Bueno Avatar answered Oct 10 '22 05:10

Alan Bueno


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

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

like image 39
Dexter Le Blanc Jr. Avatar answered Oct 10 '22 05:10

Dexter Le Blanc Jr.