Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GregRos/7e9c67f150c9dc80843bdf85028f1789 to your computer and use it in GitHub Desktop.
Save GregRos/7e9c67f150c9dc80843bdf85028f1789 to your computer and use it in GitHub Desktop.
Some notes on "Configuring a pretty and usable terminal emulator for WSL"

These comments are based on a few years of experience working with WSL. It's based on this tutorial:

https://blog.ropnop.com/configuring-a-pretty-and-usable-terminal-emulator-for-wsl/

And are basically updates to make it more relevant.

Opening a shell

In the past, to make the WSL run a command from cmd or somewhere else, you had to run the bash.exe program from windows, which fired up bash (and always bash) in the WSL and made it execute a command.

This is deprecated. You should instead use wsl.exe. It's a more primitive executable that also makes more sense. It opens your default shell, with the working directory being your current Windows directory.

You can also use it to execute a specific Linux binary. wsl ls -la will execute ls -la in the current folder. However, you can't give it an arbitrary command, such echo a; echo b. If you want to do that, you'll need to invoke a shell explicitly, like this:

wsl bash -c 'echo a; echo b'

Using wsl.exe, there is no need to edit your .bashrc file and make bash start zsh. Just set zsh as the default shell using chsh -s /bin/zsh.

Password-less sudo

You might want to disable sudo's password prompt. If so, type sudo visudo and add this line to the end:

your_username ALL=(ALL) NOPASSWD:ALL

dbus-x11

Before installing any X11 applications, you have to run:

sudo apt install dbus-x11

Otherwise, some X11 applications won't work properly. This may become unnecessary in the future.

You should put the following in your .bashrc or .zshrc file, so you won't have to repeat the DISPLAY=:0 line every time you want to launch something with a GUI.

export DISPLAY=:0

Terminator Startup Script

This is an updated and modified version of the script in ropnop's original tutorial.

If an argument is passed to the script, it will be used as the current folder instead. It will be assumed to be in Windows format, and will be converted to WSL format using the command wslpath. If no argument is passed, the current directory will be ~.

The ~ folder can't be defined in Windows, which is why a cd is used.

' terminator.vbs
myCd = "~"
If WScript.Arguments.Length > 0 Then
    myCd = "'$(wslpath -u '" & WScript.Arguments(0) & "')'"
End If
args = "bash" & " -c ""cd " & myCd & "; DISPLAY=:0 terminator"""
WScript.CreateObject("Shell.Application").ShellExecute "C:\Windows\System32\wsl.exe", args, "", "open", 0

Pin it to the taskbar

The next trick is pinning this shortcut to the taskbar, start menu, etc. Windows won't let you pin a shortcut to a vbs file by default, so we'll have to trick it a little.

Create a shortcut to the VBS file, and then edit it so that the Target field (in Properties) put the following:

C:\Windows\System32\wscript.exe "(PATH TO VBS FILE)"

While doing this, you can also change the shortcut's icon to something else. It needs to be an .ico file. Just google "Terminator app icon" and you'll find several choices. I went with this one:

(Click)

Once you've done this, Windows will stop looking at it as the shortcut to a content file and see it as a shortcut to an executable instead.

Now you can pin it normally.

Disable DPI scaling

If you're using the Windows higher DPI settings, you should probably disable DPI scaling for the X11 server executable. The scaling will make applications look blurry - they won't scale properly. Play around with the application's settings to compensate.

To do this, go to the server executable (vcxsrv.exe), go to Properties->Compatibility->Change High DPI settings, and set "High DPI scaling override" to "Application".

Note that in some configurations, Windows defaults to high DPI scaling.

Modifying "Linux shell here"

Recently, Microsoft has added an option to the Explorer context menu that lets you open a Linux terminal in the current location.

Let's change this so that it opens Terminator, instead of the regular windows terminal. This is pretty simple. Navigate to the following registry keys in a registry editor:

HKEY_CLASSES_ROOT\Directory\Background\shell\WSL\command
HKEY_CLASSES_ROOT\Directory\shell\WSL\command

For each, edit the values called(default) to be equal to:

wscript.exe "(PATH TO VBS FILE)" "%V"

Working with files in the WSL

In practice, files in the Linux filesystem (such as the contents of ~, /bin/, and so on) are mapped to Windows files in a remote subdirectory. However, this is an implementation detail, and modifying Linux files from Windows can result in undefined behavior.

You should always work with Linux files using Linux applications. So it's wise to install a suite of these, even if you have similar applications for working in Windows. These will all work with your X11 server setup.

  1. A lightweight text editor - I prefer Sublime Text.
  2. A file browser. I prefer nautilus.

If you have to, you can also run larger applications, such as full-featured IDEs. It's usually as easy as running them on Ubuntu directly. However, every so often you'll run into some issue or another as the virtualization layer WSL uses isn't perfect. You may also encounter performance and graphical issues.

Some of the things I've run from WSL, though not always seamlessly:

  1. SmartGit
  2. GoLand
  3. Firefox

Update for WSL 2

WSL 2 is a new version of WSL with many improvements. It's currently (2019-07-19) only in the preview builds of Windows 10. In this new version, the WSL machine gets its own IP on a network between the WSL and the host, so the instructions have to change a bit.

First of all, you can't simply block all other IPs in the firewall, since the WSL will technically be an external client. Secondly, the DISPLAY variable will have to include the IP of the host on this virtual network.

You can get it from the host using ipconfig and from the WSL using cat /etc/resolv.conf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment