In /etc/wsl.conf (create if it doesn't exist) add:
[interop] appendWindowsPath=false
Afterwards run wsl --shutdown and open a new session for the changes to take effect. Note that this will mean Windows executables are no longer in your PATH, e.g., commands like code will no longer work. I think this is better anyway bc it unclutters your WSL PATH and could prevent any problems arising from using the Windows version of something accidentally when you meant to use the Linux version. However, you will manually need to re-add all the Windows executables you care about, like code either:
by creating an alias for the executable
alias code="/mnt/c/[your vscode installation directory]/bin/code"
or by creating a new script that redirects code to the executable
#!/usr/bin/env bash /mnt/c/[your vscode installation directory]/bin/code "$@"
Also if you do that latter, remember you need to run chmod +x on it and make it available in your PATH for it to be executable from anywhere.
e.g.
# assuming "$HOME/.local/bin" exists and is on your PATH... echo -e '#!/usr/bin/env bash\n/mnt/c/[your vscode installation directory]/bin/code "$@"' > "$HOME/.local/bin/code" chmod +x "$HOME/.local/bin/code"
You could also just re-add the executable directory to your PATH variable, but I think that defeats the purpose of cleaning /mnt/* entries from your PATH.

本文介绍了如何通过修改/etc/wsl.conf文件来阻止Windows路径自动添加到WSL的PATH中,以避免混淆不同版本的程序。步骤包括设置appendWindowsPath为false,重启WSL,然后手动添加你需要的Windows执行文件,如VSCode。文章提供了创建别名或脚本来调用Windows程序的方法,并强调了这种方法可以保持WSL环境的整洁。
3035

被折叠的 条评论
为什么被折叠?



