How to Level Up Your Terminal Game | by Alex Powell | Aug, 2022

6 Command Line Utilities to Improve Your Efficiency

Image by Author

For many new developers, using the terminal or command line might seem inefficient or even antiquated. However, the shell is one of the most useful tools available to any developer. In this article, I will show you six useful utilities to level up your terminal game!

Table of Contents1. xclip
2. croc
3. zoxide
4. htop
5. entr
6. fzf

When I say “Copy/Paste”, you probably immediately think of Ctrl-C and Ctrl-V. But did you know that you can also use the command line to programmatically copy/paste to/from your system’s clipboard without using any keyboard shortcuts?

If you are using a Linux-based operating system (or WSL on Windows), you can use the xclip utility to interact with your clipboard from the command line. You can install xclip using your system’s package manager (such as apt on Ubuntu, or pacman on Arch).

Running xclip -o will show the current contents of your clipboard, which you can pipe into another program, redirect to a file, etc.

Meanwhile, piping text into the command xclip will cause that text to be copied to your clipboard. (Sidenote: xclip is intended to be used with the X11 windowing system. If you are using Wayland, you should install wl-clipboard instead, which provides wl-copy and wl-paste).

If you are on MacOS, you can use the corresponding pbcopy and pbpaste commands instead. On Windows, if you are not using WSL, you can use clip.exe (which corresponds to copy) and/or the Powershell commands Get-Clipboard and Set-Clipboard.

Regardless of your operating system, I would recommend aliasing your copy/paste commands in your shell profile, as seen in the example below. If you are using bash, you would want to add your alias to ~/.bashrc (or ~/.zshrc for zsh). You can check what shell you are currently using by running echo $SHELL .

# Example copy/paste alias for Linux
alias paste='xclip -o'
alias copy='xclip -c'
# Example copy/paste alias for MacOS
alias paste=pbpaste
alias copy=pbcopy

Now, you can run commands such as the following:

paste > file        # paste clipboard into a file
cat file | copy # copy file contents to clipboard
paste | croc send # send clipboard contents using croc

Croc is a useful cross-platform tool for transferring data (such as files or text) securely between two computers. No more emailing zip files or fiddling around with Dropbox — if you need to send files to someone, croc is simpler and (probably) faster in most circumstances.

The only requirement is that croc is installed on the both the sender and the receiver’s systems. Croc is likely available from your favorite package manager — more specific installation instructions can be found on the project’s github page.

Below is an example of how Croc is used:

$ croc send file.dat
Sending 'file.dat' (0 B)
Code is: <code-phrase>

On a second computer, typing in croc <code-phrase> will prompt you to accept the transferred data, like so:

Accept 'file.dat' (0 B)? (Y/n)

Sending text using croc is just as easy — you can either pipe in the input into croc or type the text manually as shown below:

$ echo "Hello from croc!" | croc send
# or
$ croc send --text "Hello from croc!"

And if that wasn’t enough, croc allows supports multiple encryption/hashing algorithms, resumable transfers, and proxies! In my opinion, croc is simply a super useful utility to keep in your toolbelt.

Zoxide is a bit different from the other tools I’ve talk about. It is meant to improve the command line experience by helping you traverse your file system faster.

Zoxide allows you to cd into a directory without giving the full path. Instead, Zoxide takes your past history into account and figures out where you want to go based on the name you provide. In essence, Zoxide is just a “smarter” cd.

Zoxide supports all major shells and is likely available from your favorite package manager. You can find installation instructions and more advanced usage tips on its Github page.

Here’s an example of how Zoxide can be used. Note: Zoxide provides the command z, but I prefer aliasing it to cd because of muscle memory.

$ z /opt/local/lib  # changes your directory like normal
$ z ~ # jumps to home directory like normal
$ z lib # will automatically jump /opt/local/lib
$ z .. # go up one directory
$ z l # may also jump to /opt/local/lib

When using Zoxide, running z <something> changes the directory to the highest ranked choice based on Zoxide’s matching algorithm. Zoxide considers both how often a directory is visited, along with how long ago it was last accessed.

For example, if you enter the directory /opt/local/lib multiple times, you will increase its “score”. Now, if you run z lib from your home directory, Zoxide will you straight to /opt/local/lib. In fact, you might not even need to type in lib completely. Zoxide uses fuzzy string matching, so even running z l could take you to the right place.

I’ve personally found Zoxide useful since it allows me to jump to the directories of my different projects without having to remember where exactly they are on my system. As long as I know the name of a directory (or its first 3 characters), I can get there.

Htop is a resource monitor and process manager for the terminal.

Htop can be installed using your favorite package manager. The latest releases and source code can be found on its Github.

An official screenshot of htop from https://htop.dev/screenshots.html

Here’s a quick list of things htop allows you to do:

  • View per-core CPU load, memory usage (including swap), uptime, etc
  • Show what processes are using the highest amount of CPU/memory
  • Search processes by name or PID
  • Kill processes with your choice of interrupt (such as SIGINT, SIGKILL, SIGTERM, etc)

Htop supports both keyboard and mouse — you don’t have to learn the shortcuts if you don’t need to. In addition, with the tree view, you can see which processes are the children of other processes, which is pretty interesting. I mostly use htop for monitoring system load and killing misbehaving processes (on both my own system and remote servers).

Entr is a utility that allows you to rerun a command every time a file changes. This allows you to implement your own ad-hoc live-reload server with a simple terminal command. Entr should be available from your favorite package manager (or if you would like to install it manually, see its homepage here).

You use entr by providing a list of filenames into its standard input, along with a command to run every time one of those files changes. For the sake of example, say we have a simple Python project and we want to rerun the program whenever the source code changes.

This can be accomplished with one line:

$ find . -name *.py | entr python main.py

As seen above, the find command is used to generate a list of all files in the current directory that have the extention py , and then that list is piped into entr, which reruns main.py every time a change in one of those files is detected.

If we need more complicated behavior, we can simply add a Makefile and tell entr which target to build. For example, given a hypothetical C project, we may want to run the following and let Make handle the complexities of recompiling C files.

$ find src/ | entr make

Entr also allows you to use the name of the changed file in your command. As an example, running ls | entr echo /_ will monitor each file in the current directory and print out a file’s path when it detects a change.

If you want to learn more about entr, you can check out the homepage here. You’ll find more examples, along with documentation for special command line flags.

I’ll end this article with a great interactive utility. Fzf is a fuzzy finder that allows you to interactively select a choice from a list. It also has great shell integration and configurability.

Fzf is available from pretty much every package manager out there, but you can also install it with git (instructions here).

By default, running fzf will show you an interactive picker for every file located in the current directory or any of its children (the equivalent of find .). This in itself is already useful – if you want to interactively choose a file to edit, simply run vim $(fzf). By typing in a search term, you narrow down the choices that fzf provides. Then, you can press <Enter> to select the choice.

Using fzf to pick a file to edit

You can use fzf to pick an item from just about any list that you pipe into it. With fzf, you can easily write shell scripts to interactively checkout a git commit, choose a process to kill, choose a package to install, etc. The possibilities are endless, and you can find many useful scripts online.

Below shows the fcoc script from the fzf examples wiki.

Using fzf to checkout a git commit

Not to mention, fzf has many more advanced features, such as shell completion for commands, a preview window, vim/tmux integration, and more. For info on these, I would recommend reading through the project’s Github.

Don’t underestimate the terminal! The command line is home to some of the simplest, yet most useful tools out there!

Leave a Reply

Your email address will not be published.