14

How do I do this in my non-vs-code terminal:

wsl:/home/peter/myproject$ code .

and then have myproject open in Visual Studio Code in a new WSL window or somehow in WSL mode?

I am able to open the current folder as a "normal" windows folder using:

"$(wslpath 'C:\Users\pvm\AppData\Local\Programs\Microsoft VS Code\Code.exe')" .

but then many things don't work, because it isn't opened as a WSL folder. I can however, click on where the red arrow points, and then on where the green arrow points.

code screenshot for reopen folder in wsl

As an alternative, I can open a WSL window in VS Code, and open a terminal from there. From inside this terminal-in-code, I can navigate to a folder and type code .. Then it gets opened exactly as I want, but this is only possible from a terminal-in-code, not from a standalone "normal" WSL terminal. If one tries to run the same code command from a normal terminal, one gets a "Command is only available in WSL or inside a Visual Studio Code terminal." error. And this is as designed.

So the question remains: How do I open a folder from a standalone WSL terminal in VS code in WSL mode?

2
  • Opening VS Code and using the terminal there to open further WSL VS Code instances has been my workflow, but it feels rather awkward. Very interested if there is a better way :) Commented Sep 21, 2021 at 10:47
  • I am quite curious about the subject as I need a command line solutio for this as I need to do this while testing some vscode extensions (automation). Have you found a way to start code and open a folder in wsl? Commented Feb 2, 2022 at 16:38

2 Answers 2

15

VS Code can be started with the --remote command line parameter:

code --remote wsl+<distro name> <path in WSL>

For your example:

"$(wslpath 'C:\Users\pvm\AppData\Local\Programs\Microsoft VS Code\Code.exe')" --remote wsl+<distro name> "$(pwd)"

I added an alias to my .bashrc to open VS Code in the current working directory:

alias c="code --remote wsl+ubuntu \"\$(pwd)\""
Sign up to request clarification or add additional context in comments.

5 Comments

Thankfully I'm no longer using Windows, so I have no idea if this works. If people comment that it works mentioning me, I'll accept the answer.
Worked like a charm. Check wsl distros in Powershell with wsl -l. I'm using Ubuntu-20.04. This did the trick code --remote wsl+Ubuntu-20.04 /home/me/git/project @PeterV.Mørch
Hi, anyone knows how to open folder in wsl extension workspace same as this question by double clicking in explorer?
@Shashank Bhatt I would try: 1. find registry key 2. Add --remote wsl+<distro name> to the registry key value (no quotation marks) 3. Find some way to change "%1" (file path argument) to the corresponding WSL paths. Other idea: Add a context menu item to the Windows Explorer. Isn't exactly what you want, but may be useful.
for workspace: Code.exe --remote wsl+Ubuntu /home/user/my_proj.code-workspace
0

Under a Linux-native installation of VSCode, I can do things like code ../foo.sh or code -n repo/src to quickly open relative paths in VSCode.

Under WSL, the method described in @upe's answer works with the caveat that all paths listed on the command line must be absolute paths in Linux/WSL format. Relative paths won't work.

To make that method work seamlessly with relative paths, I create the following Bash script as ~/bin/code. It translates relative paths in command-line arguments into absolute Linux/WSL paths:

#!/bin/bash
declare -a args

vscode_bin="/mnt/c/blah/blahb/blah/Code.exe"   # WSL-ified path to VS Code executable
wsl_dist="Ubuntu-24.04"                        # Default distro to use

a=("$vscode_bin" "--remote" "wsl+$wsl_dist")

# Replace all args that appear to be paths with their absolute paths
for var in "$@"
do
    case "$var" in
      -*) ;;                             # Leave command-line options alone
      /*) ;;                             # Leave absolute paths alone
      *) rp=$(realpath -s "$var");       # Don't resolve symlinks...
         [[ -n "$rp" ]] && var="$rp" ;;  # ... but do replace relative paths with absolute
    esac
    a+=("$var")
done

# Echo complete command-line to stderr, for debugging purposes
echo "WSL-ified command line for VSCode:" >&2
echo "${a[@]}" >&2

# Launch vscode
exec "${a[@]}"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.