Public part of my dotfiles.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.6 KiB

#!/usr/bin/env zsh
# ZSH aliases and customizations
# TODO Learn `setopt extendedglob`
# TODO Add asdf (oh-my-zsh plugin is air, git clone is the way (aur package outdated))
# Approximate RGB colors by terminal colors
[[ "$COLORTERM" == (24bit|truecolor) || "${terminfo[colors]}" -eq '16777216' ]] || zmodload zsh/nearcolor
# Plugins
mkdir -p $ADOTDIR
typeset -a ANTIGEN_CHECK_FILES=(~/.config/shell/zshrc)
[ -f ~/.local/share/zsh/antigen.zsh ] || (mkdir -p ~/.local/share/zsh/ && curl -L > ~/.local/share/zsh/antigen.zsh)
source ~/.local/share/zsh/antigen.zsh
# This is better to have them installed as system since we can use them as root
# pacman -S zsh-autosuggestions zsh-history-substring-search zsh-syntax-highlighting zsh-completions --needed
function plugin() {
if [ -d "/usr/share/licenses/$2" ]
trysource "/usr/share/zsh/plugins/$2/$2.zsh"
# It's ok if it fails
antigen bundle "$1/$2"
plugin zsh-users zsh-completions
plugin zsh-users zsh-syntax-highlighting
plugin zsh-users zsh-autosuggestions
plugin zsh-users zsh-history-substring-search
antigen apply
# Prompt customization
zmodload zsh/datetime
function preexec() {
function powerline_precmd() {
local __ERRCODE=$?
local __DURATION=0
if [ -n $__TIMER ]; then
__DURATION="$(($__ERT - ${__TIMER:-__ERT}))"
echo -en "\033]; ${USER}@${HOST} $PWD\007"
# echo -en "… $\r"
eval "$(powerline-go -shell zsh -eval -duration $__DURATION -error $__ERRCODE "${POWERLINE_GO_DEFAULT_OPTS[@]}")"
unset __TIMER
function install_powerline_precmd() {
for s in "${precmd_functions[@]}"; do
if [ "$s" = "powerline_precmd" ]; then
# Cursor based on mode
if [ $TERM = "linux" ]; then
_LINE_CURSOR="\e[6 q"
_BLOCK_CURSOR="\e[2 q"
function zle-keymap-select zle-line-init
case $KEYMAP in
vicmd) print -n -- "$_BLOCK_CURSOR";;
viins|main) print -n -- "$_LINE_CURSOR";;
# zle reset-prompt
# zle -R
function zle-line-finish
print -n -- "$_BLOCK_CURSOR"
zle -N zle-line-init
zle -N zle-line-finish
zle -N zle-keymap-select
# Should I really put a comment to explain what this does?
bindkey 'jk' vi-cmd-mode
# Edit command line
autoload edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
# History search
# bind UP and DOWN arrow keys
zmodload zsh/terminfo
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
# bind UP and DOWN arrow keys (compatibility fallback
# for Ubuntu 12.04, Fedora 21, and MacOSX 10.9 users)
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# bind P and N for EMACS mode
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' history-substring-search-down
# bind k and j for VI mode
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
# Autocompletion
autoload -Uz promptinit
# Color common prefix
zstyle -e ':completion:*:default' list-colors 'reply=("${PREFIX:+=(#bi)($PREFIX:t)(?)*==35=00}:${(s.:.)LS_COLORS}")'
setopt GLOBDOTS # Complete hidden files
setopt NO_BEEP # that annoying beep goes away
# setopt NO_LIST_BEEP # beeping is only turned off for ambiguous completions
setopt AUTO_LIST # when the completion is ambiguous you get a list without having to type ^D
# setopt BASH_AUTO_LIST # the list only happens the second time you hit tab on an ambiguous completion
# setopt LIST_AMBIGUOUS # this is modified so that nothing is listed if there is an unambiguous prefix or suffix to be inserted --- this can be combined with BASH_AUTO_LIST, so that where both are applicable you need to hit tab three times for a listing
unsetopt REC_EXACT # if the string on the command line exactly matches one of the possible completions, it is accepted, even if there is another completion (i.e. that string with something else added) that also matches
unsetopt MENU_COMPLETE # one completion is always inserted completely, then when you hit TAB it changes to the next, and so on until you get back to where you started
unsetopt AUTO_MENU # you only get the menu behaviour when you hit TAB again on the ambiguous completion.
# Fuzzy matching all the way
# trysource /usr/share/fzf/completion.zsh
trysource /usr/share/fzf/key-bindings.zsh
# Help
# TODO Doesn't work (how ironic)
autoload -Uz run-help
unalias run-help
alias help=run-help
autoload -Uz run-help-git
autoload -Uz run-help-ip
autoload -Uz run-help-openssl
autoload -Uz run-help-p4
autoload -Uz run-help-sudo
autoload -Uz run-help-svk
autoload -Uz run-help-svn
# Dir stack
if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then
dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
# [[ -d $dirstack[1] ]] && cd $dirstack[1]
chpwd() {
print -l $PWD ${(u)dirstack} >$DIRSTACKFILE
setopt PUSHD_IGNORE_DUPS # Remove duplicate entries
setopt PUSHD_MINUS # This reverts the +/- operators.
# Command not found
# (since we have syntax highlighting we are not forced to wait to see that we typed crap)
trysource /usr/share/doc/pkgfile/command-not-found.zsh
# History
# From
setopt BANG_HIST # Treat the '!' character specially during expansion.
unsetopt EXTENDED_HISTORY # Write the history file in the ":start:elapsed;command" format.
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY # Share history between all sessions.
setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicate entries first when trimming history.
setopt HIST_IGNORE_DUPS # Don't record an entry that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS # Delete old recorded entry if new entry is a duplicate.
setopt HIST_FIND_NO_DUPS # Do not display a line previously found.
setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space.
setopt HIST_SAVE_NO_DUPS # Don't write duplicate entries in the history file.
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry.
unsetopt HIST_VERIFY # Don't execute immediately upon history expansion.
unsetopt HIST_BEEP # Beep when accessing nonexistent history.