I have installed ubuntu 18.04 on wsl2. I know that with the command 'code .' you can open VS Code, but I want to know is there a commands for all Apps that u can open them from wsl2 terminal, like for example command for Visual Studio?
By default, WSL has "Interop" enabled, which allows you to run/launch any Windows executable from within WSL. Also by default, WSL appends the Windows path to your WSL path (also controlled in the same Interop section of /etc/wsl.confg
).
So as long as the Windows application is in the path, appname.exe
will work. For instance, notepad.exe
will launch Notepad.
The reason that you don't need the .exe
with VSCode is because the Windows version provides a shell script (code
) that is designed to work with WSL.
For apps that are not on the path, you can either edit the WSL path (in, for instance, your .bashrc
) or provide the full path like:
/mnt/c/Program\ Files/Windows\ Photo\ Viewer/ImagingDevices.exe
Note that you need to account for the difference between Linux/POSIX path and the Windows equivalent. For instance:
notepad.exe ~/test.txt
... will not work, since Notepad won't understand the Linux path. WSL provides the wslpath
command to translate paths:
notepad.exe $(wslpath -w ~/test.txt)
or
notepad.exe '\\wsl$\Ubuntu\home\username\test.txt'
Please note: This is a bad example, just to make it easy to understand. Do not use notepad.exe to edit Linux files, as Notepad will add CRLF (DOS line-endings) which Linux apps will not handle properly.
I've managed to run Visual Studio 2022 from WSL2 this way:
# Path to Visual Studio executable
VSRUN='C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Visual Studio 2022.lnk'
function open_in_vs() {
# Take first passed argument and generate absolute path
FIRST_ARG=`realpath $1`
# Convert path (which is now in Unix style) to Windows path
PATH_TO_FILE=`wslpath -w $FIRST_ARG`
# Run process using PowerShell
powershell.exe "Start-Process '${VSRUN}' '${PATH_TO_FILE}'"
}
# open_in_vs ./testfile.txt
# This adds an `openvs` alias you can use in your terminal to open any file in VS
alias openvs=open_in_vs
© 2022 - 2024 — McMap. All rights reserved.