Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active June 4, 2019 01:23
Show Gist options
  • Save 0racle/c40389aee05bc3263dd173591134eb5f to your computer and use it in GitHub Desktop.
Save 0racle/c40389aee05bc3263dd173591134eb5f to your computer and use it in GitHub Desktop.
zshrc
# load my .aliases file if it exists
[[ -f ~/.aliases ]] && . ~/.aliases
# enable some features
autoload -U compinit && compinit
autoload -U colors && colors
autoload -U promptinit && promptinit
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
autoload -Uz vcs_info
# enable completion module
zmodload zsh/complist
# export LS_COLORS for use in path completion
if [[ $(uname -s) == Linux ]]; then
eval $(dircolors -b)
else
CLICOLOR=1
# UNIX/BSD/Darwin does not use LS_COLORS
# they use LSCOLORS=exfxcxdxbxegedabagacad
# which converted to LS_COLORS looks like this
LS_COLORS="di=34;0:ln=35;0:so=32;0:pi=33;0:ex=31;0:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43:"
fi
# set options
setopt prompt_subst # enable command subst in prompt (required for vcs_info)
setopt correct # enable command correction prompt
setopt auto_cd # '/etc' automatically does 'cd /etc'
setopt glob_complete # wildcard completion
setopt extended_glob # extended globbing
setopt nonomatch # try to avoid the 'zsh: no matches found...'
setopt complete_in_word # allow mid-word completion
setopt interactive_comments # ignore lines prefixed with '#'
setopt list_ambiguous # complete until cmd get ambiguous
setopt no_flowcontrol # disable flow control ( stty -ixon )
setopt no_beep # disable term beep
# history settings
setopt append_history
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=$HISTSIZE
# disable mail checking
MAILCHECK=0
# enable completion menu
zstyle ':completion:*' menu select
# enable completion colors
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# show completion in groups
zstyle ':completion:*' group-name ''
# complete from history
zstyle ':completion:*:history-words' menu yes
# completion group descriptions
zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}%B%d%b%{\e[0m%}'
# option flag descriptions
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:options' auto-description '%d'
# describe options in full
zstyle ':completion:*:options' description 'yes'
# complete uppercase from lowercase
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# show warnings for no matches
zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches%{\e[0m%}' # %d
# ADDITIONAL COMPLETION OPTIONS ( STOLEN FROM GRML )
# complete user processes on process completion
zstyle ':completion:*:processes' command 'ps -au $USER'
# complete man pages by their section
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.*' insert-sections true
zstyle ':completion:*:man:*' menu yes select
# path for sudo completion
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin \
/usr/local/bin \
/usr/sbin \
/usr/bin \
/sbin \
/bin
#############################
# KEY BINDINGS AND SETTINGS #
#############################
if [[ $ZSH_VERSION != <5->* ]]; then
zle -N zle-line-init
fi
zle -N zle-keymap-select
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
# smart history on up/down
bindkey '\e[A' up-line-or-beginning-search
bindkey '\e[B' down-line-or-beginning-search
# shift-tab goes backwards
bindkey -M menuselect '\e[Z' reverse-menu-complete
bindkey '\e[Z' menu-expand-or-complete
###################
# SHELL FUNCTIONS #
###################
# Ctrl-Z sends to the background, and back into the foreground as well
function bgfg() {
if (( ${#jobstates} )); then
zle .push-input
[[ -o hist_ignore_space ]] && BUFFER=' ' || BUFFER=''
BUFFER="${BUFFER}fg"
zle .accept-line
else
zle -M 'No background jobs'
fi
}
zle -N bgfg
bindkey "^z" bgfg
# smart cd function, allows switching to /etc when running 'cd /etc/fstab'
cd() {
if (( ${#argv} == 1 )) && [[ -f ${1} ]]; then
[[ ! -e ${1:h} ]] && return 1
print "Correcting ${1} to ${1:h}"
builtin cd ${1:h}
else
builtin cd "$@"
fi
}
##################
# PROMPT BUILDER #
##################
# ZSH prompt are weird, so here it's been abstracted to 5 steps
# 1. declare what tokens appear in your prompt
# 2. define the code for each token
# 3. define the colors for each token
# 4. define function that takes the above and returns a prompt string
# 5. call the function with the prompt tokens and assign to the PROMPT variable
# declare what tokens you want to appear in your PROMPT ( and RPROMPT if you want )
left_items=(bgtask user at host path sigil)
#right_items=(errno)
# code for each prompt token
local -A prompt_tokens
prompt_tokens=(
at '@'
bgtask '$BGTASK'
clock '%D{%H:%M:%S}'
cr $'\n'
cwd ' %d '
date '%D{%Y-%m-%d}'
errno '%(?..%? )'
host '%m'
job '%(1j.%j .)'
path ' %40<..<%~%<< '
sigil '%# '
user '%n'
)
# colors for each token (undefined tokens should be white)
local -A prompt_colors
prompt_colors=(
at 'white'
bgtask 'magenta'
clock 'cyan'
cwd 'yellow'
errno 'red'
host 'green'
path 'yellow'
user "$( (($EUID)) && echo blue || echo red )" # normal || root
)
# PROMPT FUNCTIONS
# This function takes the tokens above to construct the $PROMPT variable
function build_prompt() {
local prompt=''
for i in $@; do
[ $prompt_colors[$i] ] && prompt+=%{%B%F{$prompt_colors[$i]}%}
if [[ -z $prompt_tokens[$i] ]]; then prompt+=$i
else prompt+=$prompt_tokens[$i]; fi
prompt+=%{%b%f%}
done
echo $prompt
}
# This function shows an indicator if a bg task is running
function bgtask() {
TASK=''
BGTASK=''
[[ -z $TMUX ]] && $(which tmux) ls > /dev/null 2>&1 && TASK='T'
(( ${#jobstates} )) && TASK=${#jobstates}
[ $TASK ] && BGTASK='['%{%f%}$TASK%{%B%F{magenta}%}'] '
}
add-zsh-hook precmd bgtask
# call the build_prompt function with the list prompt tokens and assign to the PROMPT variable
PROMPT=$(build_prompt $left_items)
#RPTOMPT=$(build_prompt $right_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment