zsh-config/init.zsh

324 lines
8.2 KiB
Bash
Raw Normal View History

2022-08-28 02:52:37 -07:00
# Main zsh config file
2023-05-03 16:24:43 -07:00
# Enable completions
2022-08-28 02:52:37 -07:00
autoload -U compinit && compinit
2023-05-03 16:24:43 -07:00
# Some utility stuff
2022-08-28 02:52:37 -07:00
ZSH_PLUGIN_DIR="${ZSH_CONFIG_DIR}/plugins"
# load_plugin <name>
function load_plugin {
source "${ZSH_PLUGIN_DIR}/${1}/${1}.plugin.zsh"
}
2023-05-03 16:24:43 -07:00
# cmd_exists <name>
function cmd_exists {
hash "${1}" >/dev/null 2>&1
}
2022-08-28 02:52:37 -07:00
2023-05-03 16:24:43 -07:00
# source_user_file <name>
function source_user_file {
2023-11-21 04:04:09 -08:00
local machine_specific="${ZSH_CONFIG_DIR}/${1}.${HOST}.zsh"
local default="${ZSH_CONFIG_DIR}/${1}.zsh"
2023-09-15 14:55:47 -07:00
if [ -e "${machine_specific}" ]; then
source "${machine_specific}"
elif [ -e "${default}" ]; then
source "${default}"
fi
2023-05-03 16:24:43 -07:00
}
# Load user early init file
2023-09-15 14:55:47 -07:00
source_user_file "early-init"
2022-08-28 02:52:37 -07:00
# Some options
2023-11-28 04:18:41 -08:00
setopt autocd extendedglob rm_star_silent completealiases rc_quotes histignorespace
2022-08-28 02:52:37 -07:00
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
2023-05-03 16:24:43 -07:00
# Tools for graphical sessions
2023-06-12 10:21:55 -07:00
export BROWSER=mullvad-browser
2022-12-01 00:01:48 -08:00
export READER=zathura
2023-10-30 00:39:26 -07:00
if [[ -v WAYLAND_DISPLAY ]]; then
function clip {
(( ${#} >= 1 )) && (cat "${@}" | wl-copy) || wl-copy
}
else
alias clip="xclip -selection clipboard"
fi
2022-08-28 02:52:37 -07:00
2023-05-03 16:24:43 -07:00
# I mess this up a lot
alias cd..="cd .."
# Make xargs, sudo, etc. understand aliases
alias xargs='xargs '
2024-03-27 02:54:07 -07:00
if cmd_exists doas; then
__zsh_sudo_cmd=doas
alias sudo='doas '
alias doas='doas '
alias sudoedit="doas $EDITOR "
alias se="doas $EDITOR "
else
__zsh_sudo_cmd=sudo
alias sudo='sudo '
alias se='sudoedit '
fi
2023-09-27 16:34:49 -07:00
# Emacs and Neovim stuff
if [[ -v NVIM ]]; then
export EDITOR=nvr
alias n=nvr
alias nvim=nvr
2023-09-27 16:34:49 -07:00
alias e=nvr
2023-09-30 21:40:00 -07:00
alias emacs=nvr
2023-09-27 16:34:49 -07:00
elif [[ -v INSIDE_EMACS ]]; then
export EDITOR='emacsclient'
alias e='emacsclient '
2023-09-30 21:40:00 -07:00
alias emacs='emacsclient '
2023-09-27 16:34:49 -07:00
alias n='emacsclient '
2024-01-09 15:10:46 -08:00
alias dired='emacsclient '
alias d='emacsclient '
else
2024-01-09 21:35:32 -08:00
export EDITOR='emacsclient -a nvim -nw'
2023-10-15 15:27:04 -07:00
# Because I keep using n by mistake
2024-03-04 22:44:22 -08:00
alias emacs="${EDITOR} "
2024-01-09 21:35:32 -08:00
alias n=emacs
alias e=emacs
2024-01-09 15:10:46 -08:00
function dired() {
if (( ${#} == 0 )); then
emacsclient -nw --eval '(dired ".")'
else
args=()
for arg; do
args+=(--eval "(dired \"${arg:gs#\\#\\\\#:gs#\"#\\\"#}\")")
done
emacsclient -nw ${args}
fi
}
alias d=dired
fi
export VISUAL="${EDITOR}"
2022-08-28 02:52:37 -07:00
# Safer file functions
alias cp="cp -i"
alias mv="mv -i"
2023-11-07 19:42:16 -08:00
# Ledger stuff
2023-11-21 04:30:25 -08:00
alias ldg='ledger -f "${HOME}/finance/finances.ledger"'
2023-11-07 19:42:16 -08:00
2023-10-30 22:46:20 -07:00
# Trash put for safety
if cmd_exists trash-put; then
2023-10-30 22:50:04 -07:00
alias rm='echo "rm: I''m unsafe! Don''t use me."; false'
alias tp=trash-put
2023-10-30 22:46:20 -07:00
alias trr=trash-restore
alias trl=trash-list
alias tre=trash-empty
alias trm=trash-rm
else
local rm_confirm_flag='-i'
uname | grep -i linux >/dev/null && rm_confirm_flag='-I'
alias rm="rm ${rm_confirm_flag}"
fi
2023-05-03 16:24:43 -07:00
# Enable mouse support in less
export LESS="--mouse"
2022-08-28 02:52:37 -07:00
# Bat configuration
2023-05-03 16:24:43 -07:00
if cmd_exists bat; then
2022-08-28 02:52:37 -07:00
# 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
2023-10-20 15:12:45 -07:00
export MANPAGER="zsh -c 'col -bx | bat -l man --paging=always --style=plain'"
2023-07-29 11:12:14 -07:00
export MANROFFOPT="-c"
2022-08-28 02:52:37 -07:00
fi
2023-09-10 03:40:05 -07:00
# Eza configuration
if cmd_exists eza; then
alias ls="eza --git -F"
2022-08-28 02:52:37 -07:00
alias la="ls -a"
2023-05-03 16:24:43 -07:00
alias l="ls -l"
alias ll="ls -al"
2022-08-28 02:52:37 -07:00
fi
2023-04-14 14:33:11 -07:00
# Delta configuration
2023-05-03 16:24:43 -07:00
if cmd_exists delta; then
2023-04-14 14:33:11 -07:00
export DELTA_FEATURES='unobtrusive-line-numbers decorations side-by-side'
export DELTA_PAGER='bat -p'
2023-04-14 14:16:01 -07:00
export GIT_PAGER='delta'
fi
2023-04-14 14:33:11 -07:00
# Git aliases
2022-08-28 02:52:37 -07:00
alias ga="git add"
alias gaa="git add -A"
alias gco="git commit"
2022-11-07 14:42:44 -08:00
gcm() {
2024-01-09 15:30:33 -08:00
git commit -m "${${@}}"
2022-11-07 14:42:44 -08:00
}
2022-08-28 02:52:37 -07:00
alias gca="git commit -a"
2022-12-01 21:33:48 -08:00
gcam() {
2024-01-09 15:30:33 -08:00
git commit -am "${${@}}"
2022-12-01 21:33:48 -08:00
}
2022-08-28 02:52:37 -07:00
alias gp="git push"
alias gu="git pull"
alias gf="git fetch"
alias gt="git status"
2024-04-20 16:10:03 -07:00
alias gd="git diff"
2022-08-28 02:52:37 -07:00
# Sudo last line with <Esc><Esc>
sudo-command-line() {
[[ -z $BUFFER ]] && zle up-history
2024-03-27 02:54:07 -07:00
if [[ $BUFFER == ${__zsh_sudo_cmd}\ * ]]; then
LBUFFER="${LBUFFER#${__zsh_sudo_cmd} }"
2022-08-28 02:52:37 -07:00
else
2024-03-27 02:54:07 -07:00
LBUFFER="${__zsh_sudo_cmd} $LBUFFER"
2022-08-28 02:52:37 -07:00
fi
}
zle -N sudo-command-line
2022-11-30 23:28:40 -08:00
bindkey -M vicmd "^f" sudo-command-line
bindkey -M viins "^f" sudo-command-line
2022-08-28 02:52:37 -07:00
# Autosuggestions
load_plugin zsh-autosuggestions
2022-12-02 10:31:48 -08:00
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
bindkey '^ ' autosuggest-accept
2022-08-28 02:52:37 -07:00
2023-05-03 16:24:43 -07:00
# 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
2022-11-30 23:28:40 -08:00
# Fancy prompt (starship)
eval "$(starship init zsh)"
2022-08-28 02:52:37 -07:00
# Change cursor shape for different vi modes.
function __zsh_vim_key_prompt_handler {
2022-08-28 02:52:37 -07:00
SPACESHIP_CHAR_SYMBOL=""
local _shape=0
case "${KEYMAP}" in
2022-11-30 23:28:40 -08:00
main) _shape=6 ;; # vi insert: line
viins) _shape=6 ;; # vi insert: line
2022-08-28 02:52:37 -07:00
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
}
2022-11-30 23:28:40 -08:00
function zle-line-init {
printf '\e[6 q'
}
2022-08-28 02:52:37 -07:00
zle -N zle-keymap-select
zle -N zle-line-init
2022-12-02 16:34:57 -08:00
# 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
2022-12-02 16:34:57 -08:00
}
zle -N __zsh_clear_screen_and_scrollback
bindkey '^L' __zsh_clear_screen_and_scrollback
2022-11-21 03:22:22 -08:00
# Direnv
2023-05-03 16:24:43 -07:00
if cmd_exists direnv; then
2022-11-30 23:28:40 -08:00
eval "$(direnv hook zsh)"
fi
2022-11-21 03:22:22 -08:00
2022-08-28 02:52:37 -07:00
# Bookmarks
2024-03-10 05:54:07 -07:00
# [ -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"
[[ -v BM_MODE ]] || BM_CWD_LS=1
[[ -v BM_MODE ]] || BM_MODE=daemon
source "${ZSH_CONFIG_DIR}/emacs-bookmark.zsh"
2022-08-28 02:52:37 -07:00
2022-12-02 10:31:48 -08:00
# Platform specific stuff
[ -f /usr/bin/pacman ] && source "${ZSH_CONFIG_DIR}/arch.zsh"
2022-10-03 04:07:06 -07:00
# 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}
2023-09-10 03:40:05 -07:00
# Preview directory's content with eza when completing cd
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath'
2022-10-03 04:07:06 -07:00
# 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'
2023-05-03 16:24:43 -07:00
# Load user init file
2023-11-21 04:04:09 -08:00
source_user_file 'local'
2023-05-03 16:24:43 -07:00
# THE FOLLOWING PLUGINS MUST COME LAST
2022-08-28 02:52:37 -07:00
# More completions
load_plugin zsh-completions
2023-03-01 23:18:09 -08:00
# Syntax highlighting
2024-03-27 01:56:49 -07:00
load_plugin fast-syntax-highlighting
2022-08-28 02:52:37 -07:00
# History substring search
load_plugin zsh-history-substring-search
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
2023-09-12 03:06:00 -07:00
#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
2022-08-28 02:52:37 -07:00
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
2023-05-03 16:24:43 -07:00
# Only match at the beginning of the line
2022-08-28 02:52:37 -07:00
HISTORY_SUBSTRING_SEARCH_PREFIXED="true"
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND=""
setopt histignoredups
2024-01-12 15:26:00 -08:00
# Run fortune and cowsay if we are not in nvim or ssh
if cmd_exists fortune && cmd_exists cowsay; then
[[ -v NVIM ]] || [[ -v SSH_CLIENT ]] || \
fortune | cowsay -felephant-in-snake -n
fi
2022-08-28 02:52:37 -07:00
# Clean up internal functions
unfunction load_plugin
2023-05-03 16:24:43 -07:00
unfunction cmd_exists
unfunction source_user_file