Just started using WSL2 the first time coming from linux. Some minor issues triggered me to create some minor helper functions to enhance the convenience working in the shell. By the way PowerShell seems quite cool although I'm living in the bash most of the time. Nevertheless the integration of WSL in windows is well accomplished.
Actually everybody has other needs. The very basic tools and functionality I need before I even start coding include:
- working installations of
docker
andgit
- new shell tabs opening in the same path as the tab I'm coming from
- my
.inputrc
and auto-completion - the possibility to share data between different WSL distros
- a simple way to see what distro I'm currently using (some hint in the title of the terminal)
After searching around I figured out how to achieve all of these features. As I'm lazy I've created a script that helps me setting up a distro in a reproducable way.
The setup is performed via the following commands:
# ensure "set +o posix"
set +o posix # if neccessary
bash <(curl -s https://raw.githubusercontent.com/docdnp/wsl-convenience/main/setup-wsl.sh)
Alternatively use the Windows curl.exe
in case the distribution you have has no curl installed yet.
# ensure "set +o posix"
set +o posix # if neccessary
bash <(curl.exe -s https://raw.githubusercontent.com/docdnp/wsl-convenience/main/setup-wsl.sh)
Or as by downloading and exectuting it:
curl -s https://raw.githubusercontent.com/docdnp/wsl-convenience/main/setup-wsl.sh > YOUR_PLACE
bash YOUR_PLACE
This installs the following assets:
- Under Windows:
- Git
- Git Credential Helper
- Git LFS
- Docker Credential Helper
- On Linux:
- crudini
- curl
- docker.io
- git
To uninstall changes in .bashrc
and remove wrapper tools under .local/bin
do:
wsl-convenience-uninstall
As the windows paths slow down extremly the autocompletion of bash, I decided to remove them.
But I still wanted to use windows tools directly from bash.
What is command
or which
under windows? ;-)
After playing around with pwsh
I've built wsl-which
which can help to search windows tools and create wrapper functions or aliases in bash.
Here we go.
user@wsl:~$ wsl-which '(wt|wsl)' # actually its a grep -E 'regexp'
/mnt/c/Users/WSLUSER/AppData/Local/Microsoft/WindowsApps/wsl.exe
/mnt/c/Users/WSLUSER/AppData/Local/Microsoft/WindowsApps/wslconfig.exe
/mnt/c/Users/WSLUSER/AppData/Local/Microsoft/WindowsApps/wslg.exe
/mnt/c/Users/WSLUSER/AppData/Local/Microsoft/WindowsApps/wt.exe
/mnt/c/Windows/system32/wsl.exe
/mnt/c/Windows/system32/wslg.exe
As searching the path takes some time the results are temporarly cached under /tmp/wsl-which.db
.
Maybe I should move this to some more persistent place as .local/share
.
To rebuild the cache, either remove the file or simply call wsl-which [-u|--update]
.
Another quite helpful bash function you'll find in your .bashrc
after calling the installer is to-alias
.
When piping to this function it creates either an alias or a wrapper function in your .bash_aliases
.
The reason for wrapper functions is that not all "executable" files under windows can be called directly from the bash.
Thus they are passed to the powershell.
Here an example:
user@wsl:~$ wsl-which '(appwiz.cpl|pwsh.exe)' | to-alias
alias pwsh="/mnt/c/Program\ Files/PowerShell/7/pwsh.exe" # added by "to-alias"
appwiz () { pwsh -c "& 'C:/Windows/System32/appwiz.cpl' $@"; } # added by "to-alias"