ZSH power tips

Originally published Jun 18, 2020

Heads up!

This is a work-in-progress; I'll add to it over time.

As a web developer, I spend a large portion of my day in the shell. I even use the shell for some commands that my IDE offers, such as Git, since my muscle memory is strong after years of use. Here are a few of my favorite pieces of my ZSH configuration.

Antigen

Antigen is a plugin manager for ZSH, and it's the one I set up on all of my machines. With a few lines of code in my ~/.zshrc, I can install my most commonly-used utilities.

The following headings correspond to bundles; add antigen bundle ${PACKAGE_TITLE} to your ~/.zshrc to install.

git

I use a few custom git aliases, but most of my commonly-used commands are provided by the git bundle (antigen bundle git). I've grown accustomed to the short aliases (e.g. gc = git commit) as well as the longer ones (glgga = git log --graph --decorate --all; shows an ASCII graphic of all git branches).

knu/manydots-magic

This little tool allows you to traverse directories with repeated periods. Instead of cd ../.., I can type ... and jump up two levels. This is particularly useful in monorepos.

Also part of this tool: type - to jump to the most recent directory in your history.

zsh-users/zsh-autosuggestions

Finds the most recent command matching the start of the current entered command, and suggests it. You can either press ctrl- to complete the next word, or to complete to the end of the line. Alternatively, you can use bindkey to change the bindings. I like to use bindkey '^ ' forward-word so that ctrl-space moves forward one word.

Tarrasch/zsh-autoenv

Powerful tool that allows you to customize your environment based on which directory you're in. You can add .autoenv.zsh and .autoenv_leave.zsh to a directory to run commands when you enter or leave the directory, respectively. Autoenv comes with the commands autostash and autounstash to restore variables to their previous values when traversing directories, and stash and unstash for more manual control.

Autoenv keeps track of edits to these files and prompts you if they change. This helps keep things more secure; any program can write to these files, so you could end up auto-executing something malicious without this check.

Here are a few of my favorite command sets:

Setting AWS_PROFILE

Assuming you have a profile myproject set up using aws configure --profile myproject:

# .autoenv.zsh
autostash AWS_PROFILE=myproject

Setting npm configuration

Similarly to my AWS_PROFILE, I also use different npm configurations based on the directory. This is mostly for my work laptop, and I usually only use this when I'm generating a lockfile for a project I'm about to open source (to make sure the hostname of our internal registry doesn't leak).

I used to set my ~/.npmrc file as a symlink, but I've since learned about npmrc config env vars, and the following Autoenv file handles this automatically:

# .autoenv.zsh
autostash NPM_CONFIG_USERCONFIG=$HOME/.npmrc-foss

Changing command history file

I'm experimenting with live coding, and I don't want a command containing secrets to be autosuggested while my screen is visible. To work around this, I have a ~/Projects/Streaming directory with this configuration:

# .autoenv.zsh
autostash HISTFILE=$HOME/.streaming_zsh_history
fc -p $HISTFILE
# .autoenv_leave.zsh
unstash HISTFILE
fc -P

This way, I can still keep a useful command history that's separate from my normal work.