zshrc/zshrc
2025-03-22 18:17:22 +01:00

276 lines
8.9 KiB
Bash

# vim: ft=zsh
#
# Download Patched fonts from
# https://github.com/gabrielelana/awesome-terminal-fonts/tree/patching-strategy/patched
#
#---------------------------------- Pre Setup ---------------------------------
# Source this file in your .zshrc:
# source $HOME/.zsh/zshrc
autoload colors; colors
autoload -Uz add-zsh-hook
LAST_RETURN_VALUE=0
# Characters
if [[ -n $(echo '\u2603' 2>/dev/null) ]]; then
MULTIBYTE_SUPPORTED="\u2603"
fi
if [[ -n $MULTIBYTE_SUPPORTED ]]; then
SEGMENT_SEPARATOR_BACKWARD="\ue0b2"
else
SEGMENT_SEPARATOR_BACKWARD=""
fi
#---------------------------------- Helpers -----------------------------------
# Search file up in directory tree. Either print path to found file or nothing
find_up() {
path=$(pwd)
while [[ "$path" != "" ]]; do
if [[ -e "$path/$1" ]]; then
echo "$path/$1"
return
fi
path=${path%/*}
done
}
command-exists() {
return $(command -v $1 >/dev/null)
}
NEWLINE=$'\n'
ZDOTDIR=${ZDOTDIR:-$HOME/.zsh}
#---------------------------------- Tab completion ----------------------------
# Force a reload of completion system if nothing matched; this fixes installing
# a program and then trying to tab-complete its name
_force_rehash() {
((CURRENT == 1)) && rehash
return 1 # Because we didn't really complete anything
}
# Always use menu completion, and make the colors pretty!
zstyle ':completion:*' menu select yes
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# Completers to use: rehash, general completion, then various magic stuff and
# spell-checking. Only allow two errors when correcting
zstyle ':completion:*' completer _force_rehash _complete _ignored _match _correct _approximate _prefix
zstyle ':completion:*' max-errors 2
# When looking for matches, first try exact matches, then case-insensiive, then partial word completion
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'r:|[._-]=** r:|=**'
# Turn on caching, which helps with e.g. apt
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path "$HOME/.zsh/cache"
mkdir -p $HOME/.zsh/cache
# Show nice warning when nothing matched
zstyle ':completion:*:warnings' format '%No matches: %d%b'
# Show titles for completion types and group by type
zstyle ':completion:*:descriptions' format "%U%B%F{yellow}» %d%u%b%f"
zstyle ':completion:*' group-name ''
# Ignore some common useless files
zstyle ':completion:*' ignored-patterns '*?.pyc' '__pycache__'
zstyle ':completion:*:*:rm:*:*' ignored-patterns
# Directories
zstyle ':completion:*:default' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
zstyle ':completion:*' squeeze-slashes true
# kill: advanced kill completion
zstyle ':completion::*:kill:*:*' command 'ps xf -U $USER -o pid,%cpu,cmd'
zstyle ':completion::*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;32'
# sudo completion
zstyle ':completion:*:sudo:*' command-path append /sbin /usr/sbin
SSH_HOSTS=($([[ -f $HOME/.ssh/config ]] && print -n $(cat $HOME/.ssh/config | sed '/^Host [^*]/!d;s/Host *\([^ \#]\+\)/\1/' | sort)))
zstyle ':completion:*:ssh:*' hosts $SSH_HOSTS
zstyle ':completion:*:scp:*' hosts $SSH_HOSTS
zstyle ':completion:*:ssh:*' users # disables users completion
zstyle ':completion:*:scp:*' users # disables users completion
# maven completions (from zsh-users/zsh-completions)
zstyle ':completion:*:*:mvn:*:matches' group 'yes'
zstyle ':completion:*:*:mvn:*:options' description 'yes'
zstyle ':completion:*:*:mvn:*:options' auto-description '%d'
# Always do mid-word tab completion
setopt complete_in_word
# don't expand aliases _before_ completion has finished
setopt complete_aliases
#---------------------------------- Corrections -------------------------------
# dont correct arguments to dot-files
CORRECT_IGNORE='[._]*'
CORRECT_IGNORE_FILE='[._]*'
#---------------------------------- Prediction --------------------------------
autoload predict-on
autoload predict-off
zle -N predict-on
zle -N predict-off
bindkey "^Z" predict-on # C-z
bindkey "^X^Z" predict-off # C-x C-z
#---------------------------------- Aliases ----------------------------------
# Use interactive sudo instead of su
alias su="sudo -u root -i"
# disable sudo correction for commands
alias sudo="nocorrect sudo"
# Shortcuts for clipboard manipulation
alias xclip-in='xclip -selection c -in'
alias xclip-out='xclip -selection c -out'
# Standard bd usage
alias bd=". bd -si"
#---------------------------------- VIM pager --------------------------------
vim_pager() {
local source_file
if [ ! -t 1 ]; then
echo "Cannot use vim pager with non-terminal output" 1>&2
return 1
fi
if [ $# -gt 0 ]; then
source_file="$@"
elif [ ! -t 0 ]; then
source_file="-"
else
echo "Input stream or file name missing" 1>&2
return 2
fi
vim --cmd 'let no_plugin_maps = 1' -c 'runtime! macros/less.vim' $source_file
}
alias vless='vim_pager'
#---------------------------------- Miscellaneous ----------------------------
setopt extended_glob
setopt correct # try to correct the spelling of commands.
setopt correct_all # try to correct the spelling of all arguments in a line.
setopt numeric_glob_sort # if numeric filenames are matched by a filename generation pattern, sort the filenames numerically rather than lexicographically.
setopt nomatch # if there is match on file pattern, dont run command (instead of running command with unchanged parameters)
setopt interactive_comments # allow comments in command line
setopt multios # enable multiple input/output redirections that work as expected
unsetopt beep # (dont) beep on errors
unsetopt notify # (dont) report the status of background jobs immediately, rather than waiting until just before printing a prompt.
unsetopt auto_cd # (dont) enter the directory that was inputed as command
unsetopt auto_remove_slash # (dont) remove last slash when next character is delimiter
unsetopt csh_junkie_quotes # (dont) complain if a quoted expression runs off the end of a line; prevent quoted expressions from containing un-escaped newlines.
# Don't count common path separators as word characters
WORDCHARS=${WORDCHARS//[&.;\/]/}
# Report time if command takes too long
REPORTTIME=5
TIMEFMT=$(echo "$fg[green]${SEGMENT_SEPARATOR_BACKWARD}$bg[green]$fg[black] %*Es $fg[yellow]$SEGMENT_SEPARATOR_BACKWARD$bg[yellow]$fg[black] %P $reset_color")
# Don't glob with commands that expects glob patterns
for command in find wget git; do
alias $command="noglob $command"
done
# load zsh extended move
autoload -Uz zmv
#---------------------------------- Machine specific --------------------------
if [[ -r $HOME/.zlocal ]]; then
source $HOME/.zlocal
fi
#---------------------------------- Post Setup --------------------------------
# at last initialize completion
autoload -Uz compinit
if [[ -n "${ZDOTDIR}/.zcompdump(#qN.mh+24)" ]]; then
compinit -u
else
compinit -u -C
fi
store_last_return_value() {
LAST_RETURN_VALUE="$?"
}
# Store last return value into separate variable
# This must be exactly first precmd function, because other precmd might modify $?
precmd_functions=(store_last_return_value $precmd_functions)
zmodload zsh/complist
#----------------------------- XDG Base Directories ---------------------------
export XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
export XDG_STATE_HOME=${XDG_STATE_HOME:-$HOME/.local/state}
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
#---------------------------------- Plugins ------------------------------------
export ZPLUG_HOME=${ZPLUG_HOME:-$XDG_STATE_HOME/zplug}
export ZPLUG_ROOT=${ZPLUG_ROOT:-$XDG_DATA_HOME/zplug}
export ZPLUG_VERSION=${ZPLUG_VERSION:-2.4.2}
if [[ ! -d "$ZPLUG_ROOT" ]]; then
ZPLUG_PARENT=$(dirname "$ZPLUG_ROOT")
if [[ ! -w "$ZPLUG_PARENT" ]]; then
echo "ZPLUG_ROOT, which is $ZPLUG_ROOT doesn't exists and cannot be created" >&2
return
fi
printf "zplug not installed, clone to ${ZPLUG_ROOT}? [y/N]: "
if read -q; then
echo
git clone https://github.com/zplug/zplug -b "$ZPLUG_VERSION" $ZPLUG_ROOT
fi
fi
source ${ZPLUG_ROOT}/init.zsh
zplug "plugins/git", from:oh-my-zsh
zplug "plugins/sudo", from:oh-my-zsh
zplug "plugins/command-not-found", from:oh-my-zsh
zplug "zsh-users/zsh-syntax-highlighting", defer:2
zplug "zsh-users/zsh-autosuggestions"
zplug "zsh-users/zsh-history-substring-search"
zplug "zsh-users/zsh-completions"
zplug "junegunn/fzf", use:"shell/*.zsh"
zplug "spaceship-prompt/spaceship-prompt", use:spaceship.zsh, from:github, as:theme
zplug "nivertius/spaceship-maven-project", use:spaceship-maven-project.plugin.zsh, from:github, as:theme
zplug "$ZDOTDIR/zsh.d", from:local, defer:1
if ! zplug check --verbose; then
printf "Install? [y/N]: "
if read -q; then
echo
zplug install
fi
fi
zplug load