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.
 
 
 
 
dotfiles/zsh/.zshrc

255 lines
9.2 KiB

#!/bin/zsh
# ~/.zshrc
#
# This file is sourced only for interactive shells.
#
# Global Order: zshenv, zprofile, zshrc, zlogin
#
# See:
# - https://zsh.sourceforge.io/Doc/Release/
# - https://stevenvanbael.com/profiling-zsh-startup
# - https://zsh.sourceforge.io/Doc/Release/Options.html
# - https://salsa.debian.org/debian/zsh/-/blob/debian/debian/zshrc
### History
HISTFILE="$HOME/.zsh_history"
HISTSIZE=500000000
SAVEHIST=100000000
setopt extended_history # record timestamp of command in HISTFILE
setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt hist_ignore_dups # ignore duplicated commands history list
setopt hist_ignore_space # ignore commands that start with space
setopt hist_verify # show command with history expansion to user before running it
setopt share_history # share command history data
## Aliases
# Common
[ -f "$HOME/.aliases" ] && . "$HOME/.aliases"
# ZSH only
alias history='history 1' # Print whole history
### Keybinds
# See:
# https://stackoverflow.com/a/21965133
# https://github.com/sorin-ionescu/prezto/pull/314
# https://vinipsmaker.wordpress.com/2014/02/23/my-zsh-config/
# https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/key-bindings.zsh
# /etc/zsh/zshrc
# Use emacs key bindings by default
bindkey ${KEYMAP:--e}
# Make sure that the terminal is in application mode when zle is active, since
# only then values from $terminfo are valid
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
function zle-line-init() {
echoti smkx
}
function zle-line-finish() {
echoti rmkx
}
zle -N zle-line-init
zle -N zle-line-finish
typeset -A key
# List of keys
key=(
BackSpace "${terminfo[kbs]}"
Home "${terminfo[khome]}"
End "${terminfo[kend]}"
Insert "${terminfo[kich1]}"
Delete "${terminfo[kdch1]}"
Up "${terminfo[kcuu1]}"
Down "${terminfo[kcud1]}"
Left "${terminfo[kcub1]}"
Right "${terminfo[kcuf1]}"
PageUp "${terminfo[kpp]}"
PageDown "${terminfo[knp]}"
)
function bind2maps () {
local i sequence widget
local -a maps
while [[ "$1" != "--" ]]; do
maps+=( "$1" )
shift
done
shift
sequence="${key[$1]}"
widget="$2"
[[ -z "$sequence" ]] && return 1
for i in "${maps[@]}"; do
bindkey -M "$i" "$sequence" "$widget"
done
}
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bind2maps emacs -- BackSpace backward-delete-char
bind2maps viins -- BackSpace vi-backward-delete-char
bind2maps vicmd -- BackSpace vi-backward-char
bind2maps emacs -- Home beginning-of-line
bind2maps viins vicmd -- Home vi-beginning-of-line
bind2maps emacs -- End end-of-line
bind2maps viins vicmd -- End vi-end-of-line
bind2maps emacs viins -- Insert overwrite-mode
bind2maps vicmd -- Insert vi-insert
bind2maps emacs -- Delete delete-char
bind2maps viins vicmd -- Delete vi-delete-char
bind2maps emacs viins vicmd -- Up up-line-or-beginning-search
bind2maps emacs viins vicmd -- Down down-line-or-beginning-search
bind2maps emacs -- Left backward-char
bind2maps viins vicmd -- Left vi-backward-char
bind2maps emacs -- Right forward-char
bind2maps viins vicmd -- Right vi-forward-char
bind2maps emacs viins vicmd -- PageUp up-line-or-history
bind2maps emacs viins vicmd -- PageDown down-line-or-history
unfunction bind2maps
else
# Fallback to manually managed user-driven database
printf 'Failed to setup keys using terminfo (application mode unsuported).\n'
# TODO: set zkbd mode
# See: https://github.com/vinipsmaker/dotfiles/blob/master/config/.zshrc
fi
### Completion (dump)
# Note: This stuff probably has to be run before defining completion configs
# TODO: Check if the above note is true
# See:
# https://www.csse.uwa.edu.au/programming/linux/zsh-doc/zsh_23.html
# https://linux.die.net/man/1/zshcompsys
# Load all stock functions (from $fpath files) called below.
autoload -U compinit
# Save the location of the current completion dump file.
compinit -u -C -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/.zcompdump-${ZSH_VERSION}"
### Completion
zmodload -i zsh/complist
WORDCHARS=''
unsetopt menu_complete # do not autoselect the first completion entry
unsetopt flowcontrol
setopt auto_menu # show completion menu on successive tab press
setopt complete_in_word
setopt always_to_end
# should this be in keybindings?
bindkey -M menuselect '^o' accept-and-infer-next-history
zstyle ':completion:*:*:*:*:*' menu select
# case insensitive (all), partial-word and substring completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'
# Complete . and .. special directories
zstyle ':completion:*' special-dirs true
zstyle ':completion:*' list-colors ''
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
# disable named-directories autocompletion
zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories
# Use caching so that commands like apt and dpkg complete are useable
zstyle ':completion:*' use-cache yes
zstyle ':completion:*' cache-path "${XDG_CACHE_HOME:-$HOME/.cache}/zsh"
# Don't complete uninteresting users
zstyle ':completion:*:*:*:users' ignored-patterns \
adm amanda apache at avahi avahi-autoipd beaglidx bin cacti canna \
clamav daemon dbus distcache dnsmasq dovecot fax ftp games gdm \
gkrellmd gopher hacluster haldaemon halt hsqldb ident junkbust kdm \
ldap lp mail mailman mailnull man messagebus mldonkey mysql nagios \
named netdump news nfsnobody nobody nscd ntp nut nx obsrun openvpn \
operator pcap polkitd postfix postgres privoxy pulse pvm quagga radvd \
rpc rpcuser rpm rtkit scard shutdown squid sshd statd svn sync tftp \
usbmux uucp vcsa wwwrun xfs '_*'
# ... unless we really want to.
zstyle '*' single-ignored show
# ssh/mosh completion
# source: https://serverfault.com/a/170481
# ~/.ssh/known_hosts is disabled because hashed by default
# Tip to unhash it: https://unix.stackexchange.com/a/175199
# TODO: handle case in ~/.ssh/config where `Host` has multiple entries
h=()
if [ -r ~/.ssh/config ]; then
h=($h ${${${(@M)${(f)"$(< ~/.ssh/config)"}:#Host *}#Host }:#*[*?]*})
fi
if [ "$ZSH_THEME_SSH_KHOSTS" = "true" ] && [ -r ~/.ssh/known_hosts ]; then
h=($h ${${${(f)"$(< ~/.ssh/known_hosts{,2} || true)"}%%\ *}%%,*}) 2>/dev/null
fi
if [ $#h -gt 0 ]; then
zstyle ':completion:*:ssh:*' hosts $h
zstyle ':completion:*:slogin:*' hosts $h
zstyle ':completion:*:mosh:*' hosts $h
#zstyle ':completion:*:sftp:*' hosts $h
fi
unset h
# automatically load bash completion functions
autoload -U +X bashcompinit && bashcompinit
# Colored manpages
# See:
# - https://boredzo.org/blog/archives/2016-08-15/colorized-man-pages-understood-and-customized
# - https://unix.stackexchange.com/a/600214
# - Since mid 2023 this is needed on Debian sid (and probably other distros too):
# - https://stackoverflow.com/a/32456403
# - https://bbs.archlinux.org/viewtopic.php?pid=2113876#p2113876
# - https://savannah.gnu.org/bugs/?func=detailitem&item_id=63583
# Careful with "" char, it may appear as "^[" in some text editors!
man() {
env \
LESS_TERMCAP_mb="" \
LESS_TERMCAP_md="" \
LESS_TERMCAP_me="" \
LESS_TERMCAP_se="" \
LESS_TERMCAP_so="" \
LESS_TERMCAP_ue="" \
LESS_TERMCAP_us="" \
MANROFFOPT=-c \
man "$@"
}
# Colored `ls` command and colored dir and files suggestions
autoload -U colors && colors
# For GNU ls, we use the default ls color theme.
if [[ -z "$LS_COLORS" ]]; then
(( $+commands[dircolors] )) && eval "$(dircolors -b)"
fi
# Take advantage of $LS_COLORS for completion as well.
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
# Rudimentary plugin loader
# Note: the files are loaded in alphabetical order
# Notes about the `(xEXN)`:
# - `.` plain files, should be comparable to a `-f` in a `find`
# - `@` symbolic links
# - `x`, `E`, `X` are respectively executable for owner, group and world
# - the `N` is a nullglob applied for the current pattern
# - See: https://zsh.sourceforge.io/Doc/Release/Expansion.html#Glob-Qualifiers
for plugin_f in "${XDG_CONFIG_HOME:-$HOME/.config}/zsh/plugins/"*(.zsh|.zsh-theme)(xEXN); do
source $plugin_f || printf "An error has occurred sourcing: %s\n" "$plugin_f"
done
# If a default prompt is set, then change to this custom one
if [ "$PS1" = "%m%# " ]; then
PS1="%B%{$fg[red]%}[%{$fg[yellow]%}%n%{$fg[green]%}@%{$fg[blue]%}%M %{$fg[magenta]%}%~%{$fg[red]%}]%{$reset_color%}$%b "
fi