Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ZachBacon/425a35fba84eef88e47a88ce8e76ee5b to your computer and use it in GitHub Desktop.

Select an option

Save ZachBacon/425a35fba84eef88e47a88ce8e76ee5b to your computer and use it in GitHub Desktop.
zshrc
# --- history ---
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt hist_ignore_dups
setopt hist_ignore_space
setopt share_history
setopt inc_append_history
# --- core ---
setopt prompt_subst
autoload -Uz colors add-zsh-hook
colors
# --- glyphs ---
SEGMENT_SEPARATOR=$'\ue0b0'
BRANCH=$'\ue0a0'
DETACHED=$'\u27a6'
PLUSMINUS=$'\u00b1'
CROSS=$'\u2718'
LIGHTNING=$'\u26a1'
GEAR=$'\u2699'
: ${DEFAULT_USER:=""}
: ${PRIMARY_FG:=black}
# --- state ---
typeset -g CURRENT_BG=NONE
typeset -g _GIT_PROMPT=""
typeset -g _GIT_COLOR=""
# --- segments ---
prompt_segment() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
else
print -n "%{$bg%}%{$fg%}"
fi
CURRENT_BG=$1
[[ -n $3 ]] && print -n $3
}
prompt_end() {
if [[ $CURRENT_BG != 'NONE' ]]; then
print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else
print -n "%{%k%}"
fi
print -n "%{%f%}"
CURRENT_BG=NONE
}
# --- status (exit code / root / background jobs) ---
prompt_status() {
local symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}$CROSS"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}$LIGHTNING"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"
[[ -n $symbols ]] && prompt_segment $PRIMARY_FG default " ${(j: :)symbols} "
}
# --- context (user@host) ---
prompt_context() {
local user=$(whoami)
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
prompt_segment $PRIMARY_FG default " %(!.%{%F{yellow}%}.)$user@%m "
fi
}
# --- directory ---
prompt_dir() {
prompt_segment blue $PRIMARY_FG ' %~ '
}
# --- git update (synchronous precmd) ---
_git_update() {
_GIT_PROMPT=""
_GIT_COLOR=""
command git rev-parse --is-inside-work-tree &>/dev/null || return
local ref detached=0
ref=$(command git symbolic-ref --short HEAD 2>/dev/null) || {
ref=$(command git rev-parse --short HEAD 2>/dev/null) || return
detached=1
}
# green = clean, yellow = dirty (matches agnoster exactly)
local dirty
dirty=$(command git status --porcelain --ignore-submodules 2>/dev/null)
if [[ -n $dirty ]]; then
_GIT_COLOR=yellow
ref="${ref} $PLUSMINUS"
else
_GIT_COLOR=green
fi
if (( detached )); then
_GIT_PROMPT=" $DETACHED ${ref} "
else
_GIT_PROMPT=" $BRANCH ${ref} "
fi
}
# hook before prompt draw
add-zsh-hook precmd _git_update
# --- git segment ---
prompt_git() {
[[ -z $_GIT_PROMPT ]] && return
prompt_segment $_GIT_COLOR $PRIMARY_FG "$_GIT_PROMPT"
}
# --- prompt builder ---
build_prompt() {
RETVAL=$?
CURRENT_BG=NONE
prompt_status
prompt_context
prompt_dir
prompt_git
prompt_end
}
PROMPT='%{%f%b%k%}$(build_prompt) '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment