Toon’s Z shell configuration


Path

export PATH=\
"$HOME/bin:"\
"$HOME/devel/emacs/active/bin:"\
"/usr/local/bin:"\
"/usr/local/sbin:"\
"$HOME/.cargo/bin:"\
"$HOME/.yarn/bin:"\
$PATH

Environment

Set XDG_CONFIG_HOME

This is used by git to find the global git config.

export XDG_CONFIG_HOME=$HOME/.config

Secrets

Load secrets from external directory.

for env in ~/Dropbox/.secret/env/*.env ;
do
    export $(cat $env)
done

zfunctions

Add zfunctions to the fpath.

fpath=(~/Dropbox/dot/ziffsh/zfunctions $fpath)

Perl & CPAN

These are autogenerated by cpan.

PATH="/home/toon/perl5/bin${PATH:+:${PATH}}"; export PATH;
PERL5LIB="/home/toon/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/toon/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/toon/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/toon/perl5"; export PERL_MM_OPT;

Prompt

Just a simple prompt with only the essentials:

export PS1="%c ❯"

Pure

PURE_GIT_PULL=0

autoload -U promptinit; promptinit
prompt pure

prompt_newline=$(echo -n "\u200B")

Bindings

Use emacs keybindings

bindkey -e

Fuzzy history

When pressing up/down arrow keys find command in history beginning with what you’ve typed already.

See: https://superuser.com/a/418299/94259

bindkey '^[OA' history-beginning-search-backward
bindkey '^[OB' history-beginning-search-forward

History

Set the zsh history file (if not already set).

if [ -z "$HISTFILE" ]; then
    HISTFILE=$HOME/.zsh_history
fi

Set the history size.

HISTSIZE=10000
SAVEHIST=10000

Create alias to print out the history with yyyy-mm-dd timestamps.

alias history="fc -il 1"

Configure the history:

append_history
Append instead of replacing history file.
extended_history
Save commands with timestamp and duration.
hist_expire_dups_first
Trim oldest history event with duplicate before unique events.
hist_ignore_dups
Ignore if command is duplicate of the previous command.
hist_ignore_space
Ignore command if the first character is a space.
hist_verify
Whenever the user enters a line with history expansion, don’t execute the line directly.
inc_append_history
Add history lines incrementally (as soon as they are entered).
share_history
Share history with your zshells on the same host.
setopt append_history
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_verify
setopt inc_append_history
setopt share_history

Completion

Load module.

zmodload -i zsh/complist

Configure some options:

menu_complete
Unset to not insert the first match immediately.
flowcontrol
Unset to disable output flow control via start/stop characters (usually assigned to ^S/Q).
auto_menu
Use menu completion by pressing the tab key repeatedly.
complete_in_word
Do completion from both ends of the word.
always_to_end
When full completion is inserted, the cursor is moved to the end of the word.
unsetopt menu_complete
unsetopt flowcontrol
setopt auto_menu
setopt complete_in_word
setopt always_to_end

Use menu like selection mode.

zstyle ':completion:*:*:*:*:*' menu select

When pressing Shift-Tab move through the completion menu backwards.

if [[ "${terminfo[kcbt]}" != "" ]]; then
  bindkey "${terminfo[kcbt]}" reverse-menu-complete
fi

Autoload completion

autoload -U compinit compinit

Homebrew

Load zsh completion that are installed by Mac Homebrew.

fpath=(/usr/local/share/zsh/site-functions $fpath)

autoload -U compinit && compinit
zmodload -i zsh/complist

Tools

ssh-agent

Start ssh-agent, or take over the environment variables if it is already running.

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    ps ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

Chruby

Load chruby, the Ruby version changer.

source /usr/local/share/chruby/chruby.sh

Also automatically use the default version.

source /usr/local/share/chruby/auto.sh

Password store

Encrypt the passwords in password store ASCII armored.

export PASSWORD_STORE_GPG_OPTS=--armor

kubectl

Disabled at the moment.

source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc'
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'

WakaTime

export WAKATIME_HOME=$HOME/.config/wakatime

Aliases

Basics

Just some simple aliases I use every day.

alias l="ls -la"

Bundler

Short aliases for bundler. Inspired by Vendor Everything Still Applies.

alias b="bundle"
alias bi="b install --path vendor/bundle"
alias bil="bi --local"
alias bu="b update"
alias be="b exec"
alias binit="bi && b package && echo 'vendor/ruby' >> .gitignore"

Emacs

Emacs is the $EDITOR.

export EDITOR="emacsclient"

Alias for emacs client.

alias e="emacsclient -n"

magit

And an alias to directly start magit in the current working directory.

alias magit='emacsclient -n -e "(progn (magit-status) (delete-other-windows))"'

git

I use git a lot, so add some easy to use aliases.

alias g="git"
alias gst="git status"
alias gco="git checkout"

GitLab

alias gitlab-rake="be rake"

Functions

Smart chruby

Automatically guess the ruby version from:

  • Given argument
  • Gemfile
  • .ruby_version
  • ~/.ruby_version
function chrb () {
    if [ -n "$1" ]
    then
        chruby $@
    elif [ -e Gemfile ]
    then
        chruby $(grep ^ruby Gemfile | tr -cd '[[:digit:]].')
    elif [ -e .ruby_version ]
    then
        chruby_auto
    else
        chruby
    fi
    # print the current ruby version
    env ruby --version
}

la-di-da

Generate funny “La-di-da” gifs:

ladida () {
    url="https://bd8wz9ifl5.execute-api.eu-west-3.amazonaws.com/Production/%7Bladida+%7D"
    curl -G -s --data-urlencode "text=$*" $url | xclip
}

fzf

if [[ ! "$PATH" == */home/toon/src/fzf/bin* ]]; then
  export PATH="$PATH:/home/toon/src/fzf/bin"
fi

[[ $- == *i* ]] && source "/home/toon/src/fzf/shell/completion.zsh" 2> /dev/null

# Key bindings
# ------------
source "/home/toon/src/fzf/shell/key-bindings.zsh"