Shell (zsh/bash) Configuration

August 20, 2019

macOS ships with Zsh as its default shell starting with Catalina (10.16). Thankfully, Zsh shares much of the fuctionality with bash, so existing configurations should transfer over without issue.

Dotfiles

Shell startup files, also known as dotfiles, are usually found in your home directory (if it exists). They are sourced in the following order:

  1. .zshenv - may not exist.
  2. .zprofile - login shell, sourced before .zshrc. I have PATH environment variables here.
  3. .zshrc - login and interactive shells. I hold aliases, color configurations, and other environment variables here.
  4. .zlogin - login shell, may not exist.

Prompt Colors

Terminal Colors

The prompt is configured using $PS1, which isn’t the gaming console but the “prompt string”. Here is a list of important escapes:

  • %n - username
  • %M - hostname
  • %d, %/ - current working directory
  • %~ - current working directory, with $HOME replaced with a tilde ~.
  • %F%f - foreground color, referenced by name or ASNI code.
  • %B...%b - bold

You are also able to use double quotes to preserve line breaks.

PS1="
%B%F{green}%n%f%b %F{blue}%~%f
%B%F{magenta}»%f%b "

Tab Completion Colors

Terminal Tab Completion

Zsh uses list-colors and menu select modules to select from a list of tab-completed strings. To use colors with the regular ls command, set CLICOLOR to 1. You’ll need autoload at the top of your configuration file to use these features:

autoload -Uz compinit
compinit

export CLICOLOR=1

export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu select

Keybindings

Unfortunately, Zsh doesn’t have backwards-kill, using Ctrl + U or ^U, enabled by default. If you’re coming from bash, you can re-enable this feature using the bindkey command:

bindkey '^U' backward-kill-line

Editor

EDITOR is the environment variable that points to the path of the text editor of your choice. The default editor is “vim”, and used for things like git commit messages. Thankfully you can change the default editor to something else:

# Use VSCode instead.
export EDITOR='/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron -w'

Here, the -w flag waits for the file to be closed before returning, which is required for git commit messages.

# Use nano instead.
export EDITOR='usr/bin/nano'

Nano uses tabs instead of spaces. To have the editor always use spaces instead of tabs, you can either alias nano or include the -T4 flag in the environment variable.

# Use nano with spaces instead of tabs.
export EDITOR='usr/bin/nano -T4'

# Always open nano with spaces instead of tabs.
alias nano='nano -T4'

If you can’t remember the path to the editor, use the which command:

$ which code
/usr/local/bin/code

$ which nano
/usr/bin/nano

Profile picture

Written by Samuel Moon who lives and works in Los Angeles building useful things. Check out my GitHub.