WillFuqua

Tips for using the Windows Command Line effectively

Although the command line experience on Windows has historically been less than stellar, the last few years have seen rapid improvement. In this post, I’ll cover three main areas of improvement:

  1. The new Windows Terminal
  2. Running PowerShell Core
  3. Developing with Windows Subsystem for Linux

In case you’re familiar with all three of these topics, I’ll be including some concrete tips in each topic that are real timesavers; hopefully at least one of the tips will be new.

The new Windows Terminal

Microsoft’s release of Windows Terminal, a modern, GPU-accelerated and open-source terminal application, is a real game changer. It can be installed and updated from the Microsoft store, with other methods of installation available on GitHub.

Windows Terminal supports running various shells in tabs (and side-by-side panes). For example, we can use PowerShell in one tab, ZSH under the Windows Subsystem for Linux in another tab, and to the side, a Command Prompt pain pane.

Windows Terminal is ultra-configurable, and uses JSON for its configuration store (with a work-in-progress Settings UI layered on top). An example of this configurability is the sendInput command, which can send arbitrary keystrokes and escape sequences to the terminal:

For example, we could bind a frequently used git log command to Ctrl+Alt+L:

// place this keybinding in the `actions` array
{
    "command": {
        "action": "sendInput",
        "input": "git log --all --decorate --oneline --graph\r\n"
    },
    "keys": "ctrl+alt+l"
},

Git, of course, has its own aliasing system, but it’s nice to have a keyboard shortcut for it, too. I personally also keybind some frequent directory navigation commands, as well as some common compiler / build tool commands.

Running PowerShell Core

When most people think of PowerShell, they probably think of the older PowerShell 5, which is the default version of PowerShell installed on Windows. This version of PowerShell is old (the most recent point release was 5.1, in 2017).

PowerShell Core, the cross-platform, open-source alternative, has been around since 2016 and is actively developed. It runs alongside PowerShell 5, instead of replacing it, so there’s no risk in installing it. It can be installed from the Microsoft Store or GitHub.

PowerShell Core has a lot of nice improvements across the usability of its shell, its programming language, and its performance. It’s worth upgrading just for the cd - and cd + features, which navigate back and forward through the working directory history:

[email protected]:~$ cd projects/death-ray

[email protected]:~/projects/death-ray$ cd node_modules

[email protected]:~/projects/death-ray/node_modules$ cd -

[email protected]:~/projects/death-ray$ cd -

[email protected]:~$ cd +

[email protected]:~/projects/death-ray$

Developing with Windows Subsystem for Linux

Now, it might be odd for a "Windows Command Line" blog post to recommend installing Linux, but, well, here we are. The Linux / BSD / Mac OS communities have been setting the standard in command line efficiency, and we can get all that goodness on Windows 10, too.

The Windows Subsystem for Linux 2 (WSL2) provides a real Linux command line inside Windows, with Linux kernel updates shipped via Windows Update (!!!). After installing WSL2, we can download distros like Ubuntu from the Microsoft Store. From there, advanced shells like ZSH and Fish are just an apt install away!

While the typical GNU/Linux command line tools, like grep and sed, are great, we can make our experience even better. When we run the code command inside WSL2, it will launch Visual Studio Code on Windows, and set up a client/server bridge to WSL2 automatically. This allows us to use the Visual Studio Code front-end on Windows, with all the IDE / editor features it supports, and it will communicate with our WSL2 backend to actually execute the program.

This way, we can use the WSL2 command line from Windows Terminal, with our code executing under WSL2, but we get a graphical editing and debugging experience:

/img/windows-terminal-vscode-with-wsl2.png

This is especially useful when doing development in languages where Windows is a bit of a second-class citizen, like on NodeJS or Python.

Enjoy this? You can get more!

You can find all these tips and many more in my new book, Windows Terminal Tips, Tricks, and Productivity Hacks, which is currently $10 off on Amazon! Thanks for reading.

tagged as , and