Skip to main content

Terminal velocity: Moving fast in your shell

Meet this set of utilities that help you quickly move through your filesystem.
Image
Terminal velocity

I’ve used a terminal daily for many years now. I use a heavily-customized, non-default shell. I make extensive use of tab completion and have memorized the flags for many commands. Until about a year ago, I thought I’d achieved the peak of terminal productivity. Then, I discovered a wave of shell utilities that, while obscure, fundamentally altered how I interact with my terminal environment for the better.

This article is the first in a series about these new utilities (plus a few tools I’ve known for longer, and love). Today, we will look at tools specifically for moving around your filesystem.

Jump around: z

You know how you can type the first few letters of a commonly-used website into your browser and hit Enter with the confidence that you’ll be taken to the right place? What if we brought that same experience into your shell? Imagine that you could teleport to a commonly-used directory just by typing a small part of its name. That’s what z does.

The z shell script keeps track of the directories that you visit and when you visited them. It combines the frequency of your access to a directory with the recency of your access to determine the "frecency" of your access. When you supply a couple of letters for the name of a directory, z chooses the candidate containing the provided substring with the highest frecency. The result is correct an eerie amount of the time.

Here it is in action:

Quick Facts:

  • bash/zsh implementation: GitHub
  • fish implementation: GitHub
  • License (bash/zsh): WTFPLv2
  • License (fish): MIT
  • Language: Shell script

There are a number of similar tools out there, like autojump and fasd. Try them all!

Fuzzy find: fzf

Sometimes, searching for things with regular expressions is overkill. There are times when it should be enough for the right letters to occur in the right order. The fzf utility lets you search for fuzzy matches painlessly. How does this tool relate to moving around your file system? Just wait and you’ll see.

By default, fzf works on stdin, so you can use it like grep:

$ fzf < long-log-file.txt

Live example:

Because fzf works to filter stdin and sends the output on stdout, you can do much more than search files with this tool. Want to search for a directory beneath the current one?

$ fzf

That’s right. If stdin isn’t a pipe, fzf searches all files below it and emits your choice on stdout, which makes it easy to combine the results with other commands like an editor.

If you’d like to achieve the same thing with more control, you could run:

$ find . -type d | fzf

You can use fzf to move around by running:

$ cd $(find . -type d | fzf) #bash/zsh
$ cd (find . -type d | fzf) #fish

Now, there are other fun applications as well You can:

  • Recall commands from your history with:
    $ eval $(history | fzf) #bash/zsh
    $ eval (history | fzf) #fish
    
  • Edit a file beneath the current directory with:
    $ $EDITOR $(find . | fzf) #bash/zsh
    $ $EDITOR (find . | fzf) #fish
    
  • Choose packages to install with:
    $ sudo apt install $(apt search <word> 2>/dev/null | tail -n+3 | grep '^\w' | sed 's|([^/]+)/.*|\1|' | fzf) #bash/zsh
    $ sudo apt install (apt search <word> 2>/dev/null | tail -n+3 | grep '^\w' | string split / | fzf) #fish
    
  • Make choices, in general, based on any line-delimited text. The sky is the limit!

The fzf utility has existing integrations with bash, zsh, and fish. If you install those shells, fzf naturally augments and extends workflows like "reverse index search" and tab completion. I’m a heavy user of the fish integration, myself.

Quick Facts:

  • Get fzf: GitHub
  • License: MIT
  • Language: Go

Want more?

If these tools look useful to you, stay tuned for more articles. If you can’t wait, this series is based on this talk that I gave at All Things Open 2019. You can find more tools and tricks there.

Topics:   Linux  
Author’s photo

Chris Waldon

Chris works as an SRE for IBM. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.