WSL2 with Windows Terminal
Tags: wsl, windows
Published: 2020-07-05 01:37:36

Windows has finally created a developer command line experience that can compete with OS X and bare metal Linux with the release of WSL2 combined with the new Windows Terminal. For WSL2 installion and updates, refer to the official [Windows Subsystem for Linux Documentation](https://docs.microsoft.com/en-us/windows/wsl/).

{% youtube q_cspAhX3rU %}

*Free your mind of an inferior Windows developer experience and make the jump*

The following has been tested on Windows 10 build 19041 with WSL2 enabled and set as the default version.
Additionally,

* Ubuntu from the [Windows Store](https://aka.ms/wslstore)
* Windows Terminal from the [Windows Store](https://aka.ms/terminal)

### Windows Terminal

One of the things I really like about the new Windows Terminal is the cut, copy and paste keybindings. No more <kbd>Ctrl</kbd>+<kbd>Shift</kbd> needed, just use <kbd>Ctrl</kbd>+<kbd>V</kbd> to paste. For example, here are some snippets from my terminal `settings.json`.

##### settings.json

```json
...

// If enabled, selections are automatically copied to your clipboard.
"copyOnSelect": true,

// If enabled, formatted data is also copied to your clipboard
"copyFormatting": false,

// A profile specifies a command to execute paired with information about how it should look and feel.
// Each one of them will appear in the 'New Tab' dropdown,
//   and can be invoked from the commandline with `wt.exe -p xxx`
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles":
{
  "defaults": {
    // Put settings here that you want to apply to all profiles.
    "fontSize": 11
  },
  ...
}

...

// Add custom keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about keybindings, visit https://aka.ms/terminal-keybindings
"keybindings":
[
    // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
    // These two lines additionally bind them to Ctrl+C and Ctrl+V.
    // To learn more about selection, visit https://aka.ms/terminal-selection
    { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
    { "command": "paste", "keys": "ctrl+v" },

    // Press Ctrl+Shift+F to open the search box
    { "command": "find", "keys": "ctrl+shift+f" },

    // Press Alt+Shift+D to open a new pane.
    // - "split": "auto" makes this pane open in the direction that provides the most surface area.
    // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
    // To learn more about panes, visit https://aka.ms/terminal-panes
    { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
]
```

### Oh My Zsh

One of the first things I do when setting up a new bash shell is install [zsh](http://zsh.sourceforge.net/) and [Oh My Zsh](https://ohmyz.sh/).

If you're an `npm` user, I reccommend installing [nvm](https://github.com/nvm-sh/nvm) as an Oh My ZSH! custom plugin by cloning `zsh-nvm` into your custom plugins repo as follows:

```bash
cd ~/.oh-my-zsh/custom/plugins

git clone https://github.com/lukechilds/zsh-nvm
```

Then load as a plugin in your `~/.zshrc` profile. Note that plugins need to be added before `oh-my-zsh.sh` is sourced.

##### .zshrc

```bash
...

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
 git
 zsh-nvm
)

source $ZSH/oh-my-zsh.sh
```
Additionally, if you're using Java and Maven, you can install and run those from WSL2 to build projects even if they exist on the mounted native Windows file system.

For example, I use [SDKMAN!](https://sdkman.io/) to manage mutiple JDK versions similar to how I manage multiple node versions using nvm.

![WSL windows terminal example of mvn --version](https://dev-to-uploads.s3.amazonaws.com/i/8yhnqmfjdv70z8319js4.png)

### Explorer

![WSL Ubuntu Files in Windows Explorer](https://dev-to-uploads.s3.amazonaws.com/i/c652v851vxr3xz1xpmmk.png)

Access your WSL files in Explorer by entering network path `\\wsl$` in the address bar.

#### Resources

*   [Docker Desktop WSL 2 backend](https://docs.docker.com/docker-for-windows/wsl/)
*   [github.com/robbyrussell/oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh)
*   [resolv.sh](https://gist.github.com/jimfrenette/21c7f19bc12c94c60628bebbf943a974)
*   [VS Code Remote Development](https://code.visualstudio.com/docs/remote/remote-overview)

Originally posted at [jimfrenette.com/2020/07/wsl2-windows-terminal](https://jimfrenette.com/2020/07/wsl2-windows-terminal/)