Insights and discoveries
from deep in the weeds
Outsharked

Tuesday, October 8, 2019

WSL, Windows Terminal, almost bliss

You thought this blog was dead? Ha! Never!


I have to admit admit, using this "blogger" editor for the first time in several years feels pretty retro. It's the same feeling I got the last time I tried to use a rotary phone. I'm honestly a bit surprised everything is still as I left it so long ago, and it still seems to work. But hey, what's old is new/cool again, right? Or something. Yeah, the format is crap. I'm thinking about adding an "under construction" animated gif to round it out.

Anyway, I'm using a Windows machine full-time again after a couple years using a MacBook. This mostly makes me happy. Luckily, the worst thing about the Windows software developer experience - the wretched console/shell situation - has improved somewhat. Not completely there yet, but it was far easier to set myself up with a basically high-functioning linux shell for all my console keyboarding, and be able to use Visual Studio Code and so on. Here's what I did.

1) Set up WSL (Windows Subsystem for Linux)
2) Install Microsoft Windows Terminal. The easiest way is with Chocolatey:
> choco install microsoft-windows-terminal

Note: Windows Terminal is definitely pre-beta software, but I've been using it a couple weeks without incident. While it lacks a lot of config options so far, it's fast, seems to avoid all the jank and complexity of ConEmu, and basically... just works.

3) Install windows-terminal-quake, or my not-yet-mainlined fork here. This is a helper that adds a vital missing UX feature to Windows Terminal - hotkey access. CTRL+~ will open/close the terminal in quake style; or bring to the foreground if it's not. This will most likely go away soon; this is a much requested feature so it seems likely it will become part of WT before long.

4) Install oh-my-zsh in your wsl environment:
> sudo apt install zsh
> ssh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

5) Add windows-style CLI editing capability to your wsl zsh shell with this gist. This lets you use familiar windows keyboard combinations for editing inline - e.g. shift+left/right to select text, ctrl+shift+left/right to select by word, home/end to move to start or end of the line, etc.

6) If you want, change your keybindings in Windows Terminal so CTRL+V is "paste." The default is CTRL+SHIFT+V. Since I rarely use the native bash CTRL+V this doesn't do much for me; I'd rather CTRL+V work the same everywhere. So just rebind it. :
{
  "command": "paste",
  "keys": [ "ctrl+v" ]
}

But what about copy? CTRL+C will already copy text when you are editing inline in zsh without any config changes to Windows Terminal if you did step #5. That script will automatically bind CTRL+C to copy the selected text in a command line to the Windows clipboard -- but only while zsh is within the interactive CLI.

When a command is running, CTRL+C will still send an interrupt and break the running program. This results in the very natural (to me) behavior of CTRL+C copying when I expect it to, and breaking when I expect it to.

You can also copy text from outside the command line using the mouse within Windows Terminal's native behavior. Just click-select something and right-click it, and it will be coped. WT handles pasting in every context; no special config needed here other than to change the keybinding.



That's about it! I can pretty much do everything I want: 
  • Use a real linux CLI environment
  • Copy/Paste work as expected
  • I can edit text in the interactive CLI as I would elsewhere in windows
  • Quake!
  • Just type `code .` to launch vscode to work in the current wsl directory