Getting more from the Windows Command Line

24 November 2017 - CLI , Powershell , Productivity

One of my recent blog posts - "Git GUIs versus the CLI", discussed why I think that a GUI is far better suited for source control than the CLI. However ... being a big command line fan (albeit, perhaps not always for source control!), the last thing I wanted was to come across as anti-CLI! Far from it! So I thought a post about the CLI, together with a few tips to help you get the most out of it was in order!

Be warned - I'm a Powershell fan, so this post is a bit bias - but a lot applies to other shells, eg. Bash.

Why use a CLI?

First of all, let's start with the why ...

As developers, we're hopefully always looking for productivity wins to help us optimise our daily workflows. We're one of the few industries where we're luckily enough to be able to do this very easily. A big part of this, is automating bits of our workflow via tools and scripts.

Whilst our graphical tools play a big part in this - so also do our CLI tools. If you're only using graphical tools, then you're missing a very large subset of tooling you could be taking advantage of. I'm not saying the CLI is for everything. Use the right tool for the job of course - but it turns out that the CLI is the right tool for a surprising number of jobs!

The ability to pipe commands together is also extremely powerful. Allowing you to chain multiple commands together to do various things. A simple example might be this ...

ps IExplorer.exe | kill

Yeah, I know. Who wouldn't want to run that command eh?

Or another one ...

md SomeFolder | cd

Those are obviously very simplistic examples. You can chain together many commands, where each command does a distinct thing, then passes it onto the next command. Once you get comfortable with this - you start using it for a surprising number of things.

As mentioned earlier, my personal preference is PowerShell - where you're passing objects down the pipeline. In Bash, whilst this chaining concept is extremely powerful - you are just dealing with plain text. So to fully take advantage of this, you need to become proficient in using tools like Sed and Awk. Personally, I much prefer dealing with objects like you do in Powershell. That might just be my background as an object-oriented programmer though.

Finally, most shells provide tab completion. If you start typing something, then press the tab key - depending on what it is, the shell will try and complete the word for you. For example, if you start typing a path, pressing tab will cycle through the available sub-directories based on what you've typed so far. If you start typing a Powershell command (when in Powershell of course!), pressing tab will also autocomplete the command name. If you're using PoshGit (see below), then that also provides tab completion of things like branch names, etc!

And when you're comfortable with the CLI, this helps you become more comfortable with scripting languages - whether that be Bash, Powershell, batch files, or whatever. Then when running these scripts, it is more natural from the command line. You can obviously run them from Explorer, but it's not ideal.

A better CLI with ConEmu

Whether you run CMD, PowerShell, or Bash - the default command line experience in Windows isn't great. Even simple things like using the clipboard are a pain.

Enter ConEmu! If you're a Windows user, and you use the command line - this is a MUST! You can run different shells in it - eg. Cmd, PowerShell, bash, etc. It supports multiple tabs; split screen views; and much more! Here's an example screenshot showing a split screen layout ...

conemusplitscreenlayout

The above screenshot is of a project where I have three .NET Core services - Identity Server, a webapp, and an webapi. I run each of these with the usual dotnet run, and I like to see the console output of each of these in a separate pane. Conemu's split screen is perfect for this.

You can even create pre-defined tasks describing the split screen layout, so you can have different layouts for different projects or workflows.

Quickly search command history with Ctrl-R

This one is in both Bash and Powershell. By pressing Ctrl+R, you enter history search mode allowing you to type and search your command history. Given that a lot of the time, you're repeating a command you've already previously typed - this can be a massive time saving. Especially when you have commands with arguments that you may not remember.

Quickly changing directory with Z

A recent blog post by Scott Hanselman brought this very useful tool to my attention, and has changed the way I change directories in the command line!

Basically you can do z <some-substring-of-a-path-you've-used-before>, and it'll look up that path in the history and go to it. But, it gets better! You also get tab completion too! So you can type a bit of it, and tab through all the matches. This makes it switching between different directories really fast! And you don't even need to tab complete. If you just type a substring and hit enter, it'll go to the first match.

Chocolatey

If you've used Linux before, you may have heard of apt-get. Chocolatey does a similar thing, except it's for Windows. It's very easy to install, and once installed, you can easily install software by typing choco install <packagename>. Or just cinst <packname> for short.

chocolatey.png

The cinst -y command you see there, can be run in a script, so you can have a script to install all of the packages you want to install

And you can do cup all -y to quickly ensure all your packages are up to date.

PoshGit

Whilst I mentioned earlier about my recent post discussing why I think a GUI is better suited for source control than the CLI - I'd definitely recommend that you are at least comfortable in both. And sometimes, the CLI is faster - eg. very quickly cloning a repository.

If you're a Git user, then I'd definitely recommend installing PoshGit. This adds additional information to your command prompt about the state of your current directory (if you're inside a Git project).

poshgit

You can see the branch name (master in the above screenshot), and you can see status summary information. You also get tab completion - eg. you can start typing a branch name and press tab to auto-complete.

It's very easily to install via the above mentioned Chocolatey too!

RipGrep

I've only fairly recently discovered this gem, thanks to Joseph Woodward mentioning it on Twitter and taking the time to blog about it!

Again, installable via Chocolatey (cinst -y ripgrep) - this adds very fast grep functionality, including match highlighting.

ripgrep

Summary

To summarise - the command line is awesome. There's a lot of CLI tools to help you become more productive. It's not for everything, but by not using the CLI at all - you're missing all the goodness that the CLI is for. There's also going to be times, where the CLI is all you have - eg. when SSHing into a remote box.

Besides, more and more tools are now using the CLI again - with the dotnet CLI; Azure CLI; Angular CLI; Node CLI; <insert noun here> CLI, etc, etc. The CLI is making a return - so most certainly belongs in the toolbox of all developers!

Happy command-lining!

Search


Recent Posts


Featured Posts


.NET Oxford Links