points/.zshrc

323 lines
7.5 KiB
Bash
Raw Normal View History

2023-03-23 09:49:43 +01:00
#!/bin/zsh
###
### Zsh Configuration
###
# Customize spelling correction prompt.
SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}
###
### Path
###
path=($path $HOME/.local/bin)
path=($path $HOME/bin)
path=($path $HOME/go/bin)
path=($path $HOME/dev_local/bin)
#export LD_LIBRARY_PATH=$HOME/dev_local/lib
fpath=($fpath /usr/share/zsh/site-functions)
# Remove duplicates
typeset -U path fpath
2023-03-23 09:49:43 +01:00
###
### Modules
###
### ASDF
#
# The direnv plugin is not up-to-date with new `asdf direnv setup` command.
#
source "${XDG_CONFIG_HOME:-$HOME/.config}/asdf-direnv/zshrc"
### b4b4r07/enhancd
#
# See https://github.com/b4b4r07/enhancd
#
ENHANCD_DIR=$HOME/.zim
ENHANCD_DOT_ARG='.'
### fzf
#
# Adapted from https://github.com/zimfw/fzf
#
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse'
if (( ${+commands[fd]} )); then
export FZF_DEFAULT_COMMAND='command fd --color=always --hidden --no-ignore-vcs --exclude=.git --type=file --strip-cwd-prefix'
export FZF_ALT_C_COMMAND='command fd --color=always --hidden --no-ignore-vcs --exclude=.git --type=directory --strip-cwd-prefix'
# Trigger for completion: "**"
# Commands using _dir: FZF_COMPLETION_DIR_COMMANDS
_fzf_compgen_path() {
command fd --color=always --hidden --no-ignore-vcs --exclude=.git --type=file . "${1}"
}
_fzf_compgen_dir() {
command fd --color=always --hidden --no-ignore-vcs --exclude=.git --type=directory . "${1}"
}
export FZF_DEFAULT_OPTS="--ansi ${FZF_DEFAULT_OPTS}"
elif (( ${+commands[rg]} )); then
export FZF_DEFAULT_COMMAND="command rg -uu -g '!.git' --files"
_fzf_compgen_path() {
command rg -uu -g '!.git' --files "${1}"
}
fi
if (( ${+commands[bat]} )); then
export FZF_CTRL_T_OPTS="\
--preview='command bat --color=always --line-range :100 --style=plain {}'\
--preview-window=border-left\
${FZF_CTRL_T_OPTS}"
fi
if (( ${+FZF_DEFAULT_COMMAND} )) export FZF_CTRL_T_COMMAND=${FZF_DEFAULT_COMMAND}
# fzf: Debian Specific
#
FZF_CTRL_R_OPTS="--no-sort --exact"
# Scripts sourced by zvm_after_init()
#
# - source /usr/share/doc/fzf/examples/key-bindings.zsh
# https://github.com/junegunn/fzf#key-bindings-for-command-line
#
# - source /usr/share/doc/fzf/examples/completion.zsh
# https://github.com/junegunn/fzf#fuzzy-completion-for-bash-and-zsh
# fzf: Alternate history (Ctrl-Win-R)
#
if [[ -f $ALT_HISTORY_FILE ]]; then
fzf-alt-history-widget() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
selected=( $(cat ${ALT_HISTORY_FILE} |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
local ret=$?
BUFFER=$selected
zle reset-prompt
return $ret
}
zle -N fzf-alt-history-widget
bindkey -M vicmd '^[[114;13u' fzf-alt-history-widget
bindkey -M viins '^[[114;13u' fzf-alt-history-widget
fi
### ZVM
#
# https://github.com/jeffreytse/zsh-vi-mode
function zvm_config() {
ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
}
2023-03-23 09:49:43 +01:00
# bindkey for other modules
function zvm_after_init {
# fzf
source /usr/share/doc/fzf/examples/key-bindings.zsh
source /usr/share/doc/fzf/examples/completion.zsh
# Automatic expansion of global aliases
bindkey " " globalias
bindkey "^ " magic-space # control-space to bypass completion
bindkey -M isearch " " magic-space # normal space during searches
}
###
### Zim Framework
###
# Use degit instead of git as the default tool to install and update modules.
zstyle ':zim:zmodule' use 'degit'
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
# Download zimfw script if missing.
if (( ${+commands[curl]} )); then
curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
else
mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
fi
fi
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
source ${ZIM_HOME}/zimfw.zsh init -q
fi
source ${ZIM_HOME}/init.zsh
###
### Aliases
###
#
# See https://www.thorsten-hans.com/5-types-of-zsh-aliases
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME'
alias hg="kitty +kitten hyperlinked_grep"
alias ls='exa --icons --no-permissions --no-user'
alias make='colormake-short'
2023-04-06 20:31:55 +02:00
# Neovim — nvim
local nvim_python=($HOME/.config/nvim/.direnv/python-3*(om))
nvim_python=$nvim_python[1]
# Use `export NVIM=$(nvr --serverlist | fzf)` to choose existing instance
alias nvr="${nvim_python}/bin/nvr"
2023-04-08 21:06:03 +02:00
alias vi='nvim'
2023-04-06 20:31:55 +02:00
2023-03-23 09:49:43 +01:00
# Debian Specific:
# sudo dpkg-divert --divert /usr/bin/fd --rename /usr/bin/fdfind
alias cp='cp --interactive'
alias mv='mv --interactive'
alias rm='rm -I'
autoload -U zmv
alias mmv='noglob zmv -W'
lg()
{
export LAZYGIT_NEW_DIR_FILE=~/.lazygit/newdir
lazygit "$@"
if [ -f $LAZYGIT_NEW_DIR_FILE ]; then
cd "$(cat $LAZYGIT_NEW_DIR_FILE)"
rm -f $LAZYGIT_NEW_DIR_FILE > /dev/null
fi
}
###
### Aliases -- Global & automatic expansion
###
alias -g D='$(date +%Y-%m-%d_%H-%M-%S)'
alias -g E='2> /dev/null'
alias -g G='|& grep -i'
alias -g H='| head'
alias -g HL='--help |& less -r'
alias -g L='| less'
alias -g N='&> /dev/null'
alias -g P='| peco'
alias -g S='| sort'
alias -g T='| tail'
# Expand aliases inline - see http://blog.patshead.com/2012/11/automatically-expaning-zsh-global-aliases---simplified.html
globalias() {
if [[ $LBUFFER =~ ' [A-Z0-9]+$' ]]; then
zle _expand_alias
# If you want the content of the alias to be also expanded:
# zle expand-word
# (otherwise, <BS><Tab> do the job...)
fi
zle self-insert
}
zle -N globalias
###
### Colors
###
if [[ -r /etc/grc.zsh ]]; then
# ! Incompatible with P10K Transient
source /etc/grc.zsh
# I want the default behaviour for:
unset -f journalctl
unset -f ls
fi
###
### Exports
###
export BROWSER=firefox
2023-04-08 21:06:03 +02:00
export EDITOR=nvim
2023-03-23 09:49:43 +01:00
# -i Searches ignore case
# -F Automatically exit if the entire file can be displayed
# -R ANSI "color" escape sequences are output in "raw" form
# -S Chop long lines
# -X Disables sending the termcap initialization and deinitialization
# -M Long prompt
# -K Quit on ^C
export LESS=-iFRS
export SYSTEMD_LESS='iFRSXMK'
###
### History
###
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt hist_no_functions
setopt hist_reduce_blanks
setopt hist_save_no_dups
setopt hist_verify
setopt inc_append_history
HISTFILE=${ZDOTDIR:-${HOME}}/.zhistory
HISTSIZE=4000
SAVEHIST=3000
HISTORY_IGNORE="(?|??|???|cd [a-zA-Z]*|* --help|#*|builtin *|run-help *)"
###
### Key Binding
###
# `bindkey | grep -v self-insert` to print existing bindings
# `showkey -a` to print keys
# Undo (useful to cancel completion -- borrowed from emacs keymap)
bindkey "^_" undo
###
### Zsh Options
###
## Input/Output
setopt correct
setopt interactive_comments
# Disallow `>` to overwrite existing files. Use `>|` or `>!` instead.
setopt no_clobber
## Job control
# Prevent background jobs being given a lower priority.
setopt no_bg_nice
# Prevent status report of jobs on shell exit.
setopt no_check_jobs
# Prevent SIGHUP to jobs on shell exit.
setopt no_hup
# Remove path duplicates
2023-03-23 09:49:43 +01:00
typeset -U path fpath
###
### Completion Late Definitions
###
compdef colormake=make
compdef colormake-short=make
# Hide "parameters" (i.e. environment variables) when looking for a "command"
zstyle ':completion:*:-command-:*' tag-order '!parameters'