I’ve been using both the second version of the Windows Subsystem for Linux (WSL2), and the newish Windows Terminal for quite a while, and thought it’d be useful to collate some tips and tricks on them all into a single blog.
Introduction
As a bunch of techies, we’ve had plenty of those conversations about Macs vs Windows vs Linux. I’m not going to start (and don’t want to have) the conversation about what desktop you should be on, but as someone who does as much with Office documents as they do with code I’ve been on Windows for some time. If you are on Windows, and do want to do technical stuff, then you can do worse than use the Windows Subsystem for Linux (WSL) together with Visual Studio Code and the Windows Terminal.
WSL2
The second version of WSL is much more closely integrated with the host Windows operating system, being a full-blown Linux kernel running in Hyper-V (see this Microsoft article on the version differences). Personally, the only issues I’ve had using it have been related to the use of low-level devices, be they mapped devices such as serial devices, or abstracted devices like network devices. Visual Studio Code, with the WSL extension, can treat WSL as a remote development environment just like any server or container.
Your WSL config is stored in the root of your user folder, for example /mnt/c/Users/scott/.wslconfig
in WSL itself. Mine sets a memory limit (see this post for more on constraining WSL’s resources) and enables host forwarding which means webservers run from WSL are available on Windows as localhost:
[wsl2]
localhostForwarding=true
memory=6GB
Windows Terminal tricks
Windows Terminal is a terminal shell for all your shells, including WSL, Powershell, Azure Cloud Shell and even good old cmd. Being a new-Microsoft thing, it’s even open source. It supports the use of customisation profiles for each shell, all defined in a single JSON settings file.
Profiles
All your settings are stored in the json file /mnt/c/Users/<username>/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json
. This lets you set the default profile and other global settings, and also configure each profile. Here’s a simple example for WSL:
{
"guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
"hidden": false,
"name": "Ubuntu-20.04",
"source": "Windows.Terminal.Wsl",
}
You can use the profile name or GUID to launch the terminal with one or more tabs of the different profiles. For example, this command launches an instance with two tabs, one WSL tab (invoked by its GUID) and one Powersehll tab (invoked by its name):
wt -p "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}" ; new-tab -p "Windows PowerShell"
Custom Profiles
Windows Terminal lets you set configuration options for each shell profile. You can use this to set a custom background image, tab title and colour for the different shells, which can help distinguish them. For example, these settings for Powershell:
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"backgroundImage": "C:/Users/scott/Pictures/shell/potw2013a.jpg",
"backgroundImageOpacity" : 0.4,
"colorScheme": "VibrantTom",
"tabTitle": "Powershell",
"tabColor": "#44B4CC",
"startingDirectory": null,
"hidden": false
},
Gives you a shell with a blue tab colour, and one background image:
Which can contrast with WSL that uses red tabs and a different image:
You can even, if so inclined, programmatically set an animated gif as the background image. There’s a full list of profile settings in this Microsoft documentation, and in this blog. As you can see, I’m a fan of the NASA picture of the day.
You can assign customer colour schemes to a profile, which are also defined in the settings file:
// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
// Color Scheme: VibrantTom
"background" : "#16171D",
"black" : "#878787",
"blue" : "#44B4CC",
"brightBlack" : "#E373C8",
"brightBlue" : "#a6a6c0",
"brightCyan" : "#19D1D8",
"brightGreen" : "#81EC0D",
"brightPurple" : "#FF00FF",
"brightRed" : "#FF0000",
"brightWhite" : "#E5E5E5",
"brightYellow" : "#FFD93D",
"cyan" : "#19D1D8",
"foreground" : "#FFFFFF",
"green" : "#CCFF04",
"name" : "VibrantTom",
"purple" : "#9933CC",
"red" : "#FF6600",
"white" : "#F5F5F5",
"yellow" : "#FFD93D"
}
],
Panes
See this guide for using panes, which lets you split the window:
You can open a new pane either to the right using
alt + shift + =
, or to the bottom usingalt + shift + -
…
To resize the panes, use
alt + shift + arrow key
to resize in the direction of the arrow key used.…
You can select each pane either by clicking it, or you can use the
alt + arrow key
shortcut to move the focus between the panes, in the direction of the arrow key.…
Finally, to close a pane, you can use the
ctrl + shift + w
shortcut.
WSL & Windows Terminal Tips
The above covered customising and using Windows Terminal. Below are the collated shortcuts and tricks I’ve found to make it easier to work with WSL2, Windows Terminal, and Windows itself.
Launch Explorer in the current folder
Like any other executable on the Windows path, you can invoke Explorer directly:
Explorer.exe .
Your Linux home folder is mapped to a network drive, for example \\wsl$\Ubuntu-20.04\home\scott
.
Launch a Windows process from WSL
To start a Windows process you can, for example, create an alias such as start
:
alias start="cmd.exe /c start $*"
So start winword doc.docx
will open the provided file in Word, and start powershell.exe
will launch a powershell window in the current folder.
Launch Powershell in a new Windows Terminal instance
Or to do the same, but in a new Windows Terminal instance (with the guid for your Powershell profile):
start wt.exe -p {61c54bbd-c2c6-5271-96e7-009a87ff44bf}
Note that this requires "startingDirectory": null
to be set for the Powershell profile. Launching a new tab in the current window would be nice, but currently isn’t a feature.
Further Links
- https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows
- https://weblog.west-wind.com/posts/2019/Sep/03/Programmatically-Opening-Windows-Terminal-in-a-Specific-Folder
- https://blogs.windows.com/windowsdeveloper/2020/06/30/3-ways-to-customize-your-windows-terminal/
- https://endjin.com/blog/2020/05/5-tips-for-an-awesome-windows-terminal-experience
- https://www.hanselman.com/blog/adding-reaction-gifs-for-your-build-system-and-the-windows-terminal