281 lines
7.4 KiB
Bash
281 lines
7.4 KiB
Bash
# Main zsh config file
|
||
|
||
# Enable completions
|
||
autoload -U compinit && compinit
|
||
|
||
# Some utility stuff
|
||
ZSH_PLUGIN_DIR="${ZSH_CONFIG_DIR}/plugins"
|
||
# load_plugin <name>
|
||
function load_plugin {
|
||
source "${ZSH_PLUGIN_DIR}/${1}/${1}.plugin.zsh"
|
||
}
|
||
# cmd_exists <name>
|
||
function cmd_exists {
|
||
hash "${1}" >/dev/null 2>&1
|
||
}
|
||
|
||
# User configuration file
|
||
[[ -v ZSH_USER_DIR ]] || ZSH_USER_DIR="${HOME}/.zsh.d"
|
||
# source_user_file <name>
|
||
function source_user_file {
|
||
[ -e "${ZSH_USER_DIR}/${1}" ] && source "${ZSH_USER_DIR}/${1}"
|
||
}
|
||
|
||
# Load user early init file
|
||
source_user_file "early-init.zsh"
|
||
|
||
# Some options
|
||
setopt autocd extendedglob rm_star_silent completealiases
|
||
unsetopt beep notify
|
||
|
||
# Some general, random configuration
|
||
# History stuff
|
||
[ -v HISTFILE ] || HISTFILE="${HOME}/.cache/zsh/history"
|
||
[ ! -d "$(dirname "${HISTFILE}")" ] && mkdir -p "$(dirname "${HISTFILE}")"
|
||
HISTSIZE=1000
|
||
SAVEHIST=10000
|
||
|
||
# Tools for graphical sessions
|
||
export BROWSER=firefox
|
||
export READER=zathura
|
||
alias clip="xclip -selection clipboard"
|
||
|
||
# I mess this up a lot
|
||
alias cd..="cd .."
|
||
|
||
# Neovim stuff
|
||
if [[ -v NVIM ]]; then
|
||
export EDITOR=nvr
|
||
alias n=nvr
|
||
alias nvim=nvr
|
||
else
|
||
export EDITOR=nvim
|
||
alias n=nvim
|
||
fi
|
||
export VISUAL="${EDITOR}"
|
||
alias se=sudoedit
|
||
|
||
# Safer file functions
|
||
local rm_confirm_flag='-i'
|
||
uname | grep -i linux >/dev/null && rm_confirm_flag='-I'
|
||
alias rm="rm ${rm_confirm_flag}"
|
||
alias cp="cp -i"
|
||
alias mv="mv -i"
|
||
|
||
# Enable mouse support in less
|
||
export LESS="--mouse"
|
||
|
||
# Bat configuration
|
||
if cmd_exists bat; then
|
||
# Pager
|
||
export PAGER="bat --paging=always"
|
||
|
||
# Less syntax highlighting in interactive shells
|
||
alias less="bat --paging=always"
|
||
|
||
# Use bat instead of cat
|
||
alias cat="bat --paging=never"
|
||
alias pcat="bat -pp"
|
||
alias ncat="bat -pp --color=never"
|
||
|
||
# Bat as man pager
|
||
export MANPAGER="zsh -c 'col -bx | bat -l man -p --paging=always'"
|
||
fi
|
||
|
||
# Exa configuration
|
||
if cmd_exists exa; then
|
||
alias ls="exa --git -F"
|
||
alias la="ls -a"
|
||
alias l="ls -l"
|
||
alias ll="ls -al"
|
||
fi
|
||
|
||
# Delta configuration
|
||
if cmd_exists delta; then
|
||
export DELTA_FEATURES='unobtrusive-line-numbers decorations side-by-side'
|
||
export DELTA_PAGER='bat -p'
|
||
export GIT_PAGER='delta'
|
||
fi
|
||
|
||
# Git aliases
|
||
alias ga="git add"
|
||
alias gaa="git add -A"
|
||
alias gco="git commit"
|
||
gcm() {
|
||
local args="${@}"
|
||
git commit -m "${args}"
|
||
}
|
||
alias gca="git commit -a"
|
||
gcam() {
|
||
local args="${@}"
|
||
git commit -am "${args}"
|
||
}
|
||
alias gp="git push"
|
||
alias gu="git pull"
|
||
alias gf="git fetch"
|
||
alias gt="git status"
|
||
|
||
# Dotfile management
|
||
[ -v ZSH_MANAGE_DOTFILES_REPO ] || ZSH_MANAGE_DOTFILES_REPO="${HOME}/src/dotfiles"
|
||
function dots {
|
||
local args=("--git-dir=${ZSH_MANAGE_DOTFILES_REPO}" '--work-tree=/' '--bare')
|
||
if (( ${#} == 0 )); then
|
||
(cd /
|
||
for file in $(git ${args} ls-files); do
|
||
printf '%s\t%s\t\e[38;2;255;255;0m%s\e[m\n' \
|
||
"$(git ${args} -c color.status=always status -s "${file}" | sed "s#${file}##")" \
|
||
"/${file}" \
|
||
"$(git ${args} log --max-count=1 --format='%s' "${file}")"
|
||
done) | column -t -T2 --separator=" "
|
||
else
|
||
git ${args} "${@}"
|
||
fi
|
||
}
|
||
compdef -e "words[1]=(git '--git-dir=${HOME}/src/dotfiles' --work-tree=/ --bare); service=git; (( CURRENT+=3 )); _git" dots
|
||
|
||
# Sudo last line with <Esc><Esc>
|
||
sudo-command-line() {
|
||
[[ -z $BUFFER ]] && zle up-history
|
||
if [[ $BUFFER == sudo\ * ]]; then
|
||
LBUFFER="${LBUFFER#sudo }"
|
||
elif [[ $BUFFER == $EDITOR\ * ]]; then
|
||
LBUFFER="${LBUFFER#$EDITOR }"
|
||
LBUFFER="sudoedit $LBUFFER"
|
||
elif [[ $BUFFER == sudoedit\ * ]]; then
|
||
LBUFFER="${LBUFFER#sudoedit }"
|
||
LBUFFER="$EDITOR $LBUFFER"
|
||
else
|
||
LBUFFER="sudo $LBUFFER"
|
||
fi
|
||
}
|
||
zle -N sudo-command-line
|
||
bindkey -M vicmd "^f" sudo-command-line
|
||
bindkey -M viins "^f" sudo-command-line
|
||
|
||
# Autosuggestions
|
||
load_plugin zsh-autosuggestions
|
||
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
|
||
bindkey '^ ' autosuggest-accept
|
||
|
||
# Use vi mode
|
||
bindkey -v
|
||
# Fast switch of modes
|
||
KEYTIMEOUT=1
|
||
# Implement a replace mode
|
||
bindkey -N virep viins
|
||
bindkey -M vicmd "R" overwrite-mode
|
||
function overwrite-mode {
|
||
zle -K virep
|
||
zle .overwrite-mode
|
||
}
|
||
zle -N overwrite-mode
|
||
|
||
# Fancy prompt (starship)
|
||
eval "$(starship init zsh)"
|
||
# Change cursor shape for different vi modes.
|
||
function __zsh_vim_key_prompt_handler {
|
||
SPACESHIP_CHAR_SYMBOL="❮"
|
||
local _shape=0
|
||
case "${KEYMAP}" in
|
||
main) _shape=6 ;; # vi insert: line
|
||
viins) _shape=6 ;; # vi insert: line
|
||
isearch) _shape=6 ;; # inc search: line
|
||
virep) _shape=4 ;; # vi replace: underscore
|
||
command) _shape=4 ;; # read a command: underscore
|
||
vicmd) _shape=2 ;; # vi cmd: block
|
||
visual) _shape=2 ;; # vi visual mode: block
|
||
viopp) _shape=1 ;; # vi operation pending: blinking block
|
||
*) _shape=0 ;;
|
||
esac
|
||
|
||
zle reset-prompt
|
||
printf '\e[%d q' "${_shape}"
|
||
}
|
||
function zle-keymap-select {
|
||
__zsh_vim_key_prompt_handler
|
||
}
|
||
function zle-line-init {
|
||
printf '\e[6 q'
|
||
}
|
||
zle -N zle-keymap-select
|
||
zle -N zle-line-init
|
||
|
||
# Clear scrollback on ^l
|
||
__zsh_clear_screen_and_scrollback() {
|
||
echoti civis >"$TTY"
|
||
printf '%b' '\e[H\e[2J' >"$TTY"
|
||
printf '%b' '\e[3J' >"$TTY"
|
||
echoti cnorm >"$TTY"
|
||
zle .reset-prompt
|
||
zle -R
|
||
}
|
||
zle -N __zsh_clear_screen_and_scrollback
|
||
bindkey '^L' __zsh_clear_screen_and_scrollback
|
||
|
||
# Direnv
|
||
if cmd_exists direnv; then
|
||
eval "$(direnv hook zsh)"
|
||
fi
|
||
|
||
# Bookmarks
|
||
[ -v ZSH_BOOKMARK_DIR ] || ZSH_BOOKMARK_DIR="${HOME}/.cache/zsh/bookmarks"
|
||
[ -v ZSH_BOOKMARK_LS ] || ZSH_BOOKMARK_LS=true
|
||
source "${ZSH_CONFIG_DIR}/bookmark.zsh"
|
||
|
||
# Platform specific stuff
|
||
[ -f /usr/bin/pacman ] && source "${ZSH_CONFIG_DIR}/arch.zsh"
|
||
|
||
# FZF Integration
|
||
load_plugin fzf-tab
|
||
# Disable sort when completing `git checkout`
|
||
zstyle ':completion:*:git-checkout:*' sort false
|
||
# Set descriptions format to enable group support
|
||
zstyle ':completion:*:descriptions' format '[%d]'
|
||
# Set list-colors to enable filename colorizing
|
||
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
|
||
# Preview directory's content with exa when completing cd
|
||
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'exa -1 --color=always $realpath'
|
||
# Remove the '.' prefix at the start of every completion
|
||
zstyle ':fzf-tab:*' prefix ''
|
||
# Switch groups
|
||
zstyle ':fzf-tab:*' switch-group 'ctrl-h' 'ctrl-l'
|
||
# Toggle selected for all visible entries
|
||
zstyle ':fzf-tab:*' fzf-bindings 'ctrl-a:toggle-all'
|
||
|
||
# Load user init file
|
||
source_user_file 'local.zsh'
|
||
|
||
# THE FOLLOWING PLUGINS MUST COME LAST
|
||
|
||
# More completions
|
||
load_plugin zsh-completions
|
||
|
||
# Syntax highlighting
|
||
load_plugin zsh-syntax-highlighting
|
||
|
||
# History substring search
|
||
load_plugin zsh-history-substring-search
|
||
bindkey '^[[A' history-substring-search-up
|
||
bindkey '^[[B' history-substring-search-down
|
||
bindkey '^k' history-substring-search-up
|
||
bindkey '^j' history-substring-search-down
|
||
bindkey -M vicmd '^k' history-substring-search-up
|
||
bindkey -M vicmd '^j' history-substring-search-down
|
||
bindkey -M vicmd 'k' history-substring-search-up
|
||
bindkey -M vicmd 'j' history-substring-search-down
|
||
bindkey -M emacs '^P' history-substring-search-up
|
||
bindkey -M emacs '^N' history-substring-search-down
|
||
|
||
# Only match at the beginning of the line
|
||
HISTORY_SUBSTRING_SEARCH_PREFIXED="true"
|
||
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND=""
|
||
setopt histignoredups
|
||
|
||
# Clean up internal functions
|
||
unfunction load_plugin
|
||
unfunction cmd_exists
|
||
unfunction source_user_file
|
||
|
||
# Run fortune and cowsay if we are not in nvim
|
||
[[ -v NVIM ]] || fortune | cowsay -felephant-in-snake -n
|