A blog by Darren Burns
Darren
Burns
Hey πŸ‘‹ I'm Darren.
I'm a software engineer based in Edinburgh, Scotland

Posts
Twitter
GitHub

Power Up Your Command Line

January 3, 2019
productivity | command line | tips

This post is part one of a series of showcases of some of the best non-standard command line tools I've discovered in recent years. If you use the command line, you'll probably find at least one thing on this page that will make your life easier.

z, jump around

One of the best and most ubiquitous time-saving features in modern web browsers is the fuzzy and smart search capabilities of the address bar. Want to quickly go to Twitter? Typing "tw" into your address bar and hitting enter is probably enough.

By comparison, the standard way of navigating our file systems via the command line (using cd) seems almost prehistoric. Thankfully, z (GitHub) brings this browser-style navigation to the command line.

Jump Around

After a short learning period, z will allow you to jump to a directory from anywhere, using only a substring of the target directory name. The directory that z will take you to is determined by the string argument you gave it, how frequently you visit the directory, and how recently you visited the directory. They call it "frecency".

z not only increases the speed at which you can navigate your filesystem, it reduces the cognitive load of navigation. With cd, you need to recall precisely where the destination directory sits in the tree, and work out the path to get there. With z, knowing the name of the directory is enough.

Ports of z to other shells (such as fish and zsh) are readily available too. autojump (GitHub) is a similar project.

Installing z

  • Install bash version, on macOS (Homebrew): brew install z
  • Install fish shell version, on macOS (Fisher): fisher add jethrokuan/z

fzf, a fast fuzzy finder

After installing fzf (GitHub), you can press Ctrl + T at any point in time to open up an interactive fuzzy search interface, which will recursively search for files inside the current directory. You can enter a search term, and press the up/down keys to move through the results. If you press the enter key, the selected result is inserted into your terminal:

fzf

In the example above, I typed bat (but this could be any command, such as less, cd, etc.), then pressed Ctrl + T. I typed five, hit enter, and the path src/five.rs was inserted at my cursor position. This saves the alternative of (roughly): type src, press tab, type fi, press tab, which doesn't scale to long or hard to remember paths.

Installing fzf

  • On macOS (Homebrew): brew install fzf
  • Bindings for fish: fisher add jethrokuan/fzf

bat, to view files with syntax highlighting

If you want to quickly view a source file with full syntax highlighting, bat (GitHub) is your friend. bat can be used as a drop-in replacement for cat.

bat

If the output is large enough (as in the example above), bat will pipe it's output into less, meaning we get pagination for free!

Installing bat

bench, for benchmarking your code

bench (GitHub) is an incredibly useful tool for benchmarking your code. It's written in Haskell, which makes it the coolest thing on this page. You can pass any command you can run from your terminal to it (in quotes), and it will measure the execution time by repeatedly running the command. When it's finished, it'll output useful statistics to your terminal.

bench

This is a considerably more powerful means of measuring the execution time of your code than the built-in time command.

hyperfine (GitHub) is an alternative to bench written in Rust that you might also be interested in.

Installing bench

  • On macOS using Homebrew: brew install bench

asciinema & svg-term, for recording your terminal as an SVG animation

The terminal clips on this page are actually SVG animations! Using SVG rather than a video format has several huge benefits:

  • Perfect quality regardless of zoom πŸ”
  • We can put them in Markdown files like any other image 😱
  • Smaller file sizes compared to video formats 🧐
  • SVG animations are way cooler than videos πŸ”₯

To record the terminal, I use asciinema. Begin recording with asciinema rec. When you're finished, press Ctrl+D, and you'll be given the options of either saving the recording locally or uploading it to asciinema.org.

asciinema_example

If you want to generate an SVG animation from your recording using svg-term (GitHub), and you uploaded your recording to asciinema, you'll have to make it public by visiting the resulting link.

To convert the recording to an SVG animation, you can supply the ID of the cast (available on the asciinema page after making it public - the ID is in the URL), an output file, and numerous other optional arguments. For example, to save the terminal recording at https://asciinema.org/a/219486 to the SVG file you see in the example above, I used:

svg-term --cast=219486 --out ~/somewhere/out.svg --padding 18 --height 8 --width 80

Alternatively, if you don't want to upload your recording to asciinema, you can supply a local cast file directly to svg-term (thanks to Mario Nebl, the author of svg-term-cli for pointing this out to me):

asciinema rec cast.json
cat cast.json | svg-term-cli

Installing asciinema & svg-term

  • Installing asciinema on macOS: brew install asciinema
  • Installing svg-term on macOS: npm install -g svg-term-cli

wrk, for benchmarking your HTTP APIs

This is a handy little tool for performance testing your API. To demonstrate, I've got a minimal Python HTTP API server with a single endpoint (/hello) running on my local machine at port 8001. We can check how well the /hello endpoint performs using wrk (with 12 threads, 200 connections, for 5 seconds):

wrk

You can tweak the number of threads, connections, and the duration of your test to check performance under different loads. It's not a replacement for performance testing tools such as Locust and JMeter, but it's lightweight and will suffice in many situations.

Unfortunately, the command line interface for wrk makes it somewhat awkward to perform POST requests. If you want to do that, you'll need to write a small Lua script and supply it to the command as an argument (there's more information in the docs).

Installing wrk

  • On macOS using Homebrew: brew install wrk

exa, an alternative to ls

exa is a modern replacement for ls with colour coded output that's a little easier on the eye, and a larger variety of options for controlling how output is presented.

Exa

It supports features such as respecting your .gitignore files via the --git-ignore flag, and printing out a directory as a tree structure with the -T flag (see above).

Installing exa

  • On macOS using Homebrew: brew install exa

fd, for finding files & directories

If you're looking for a file or directory, you'd usually use the find command to perform a search based on a regular expression. fd (GitHub) is an alternative to find written in Rust which offers a more convenient interface by using sensible defaults, and it's faster to boot.

fd

It'll respect your .gitignore files, and it supports parallel command execution, which lets you execute a terminal command (in parallel) for every file or directory returned for a search. For example (from the fd documentation), to find all .jpg files and convert them to .png files in parallel using the Unix convert command, you can run:

fd -e jpg -x convert {} {.}.png

Installing fd

  • On macOS using Homebrew: brew install fd

rg (ripgrep), for finding strings in files

rg (GitHub) is a (much) faster alternative to grep.

rg

rg is written in Rust, and it powers the search functionality inside the VS Code text editor. It consistently outperforms similar tools in benchmarks.

Installing ripgrep

  • On macOS using Homebrew: brew install ripgrep

Conclusion

Hopefully you discovered something useful in this post! I'd like this page to become an up-to-date gallery of alternative and helpful command line tools, so I may update it from time to time. If you’re interested in more content like this, follow me on Twitter.


Copyright Β© 2022 Darren Burns