dotfiles/dot_config/zsh/dot_zshrc.tmpl

366 lines
9.5 KiB
Cheetah
Raw Normal View History

autoload -Uz promptinit
setopt appendhistory
setopt extendedglob
setopt correctall
bindkey -v
bindkey -v '^?' backward-delete-char
2021-02-15 20:53:16 -05:00
## --------------------------------------------------
# autocompletion
## --------------------------------------------------
# Highlight the current autocomplete option
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
autoload -Uz compinit && compinit -i
autoload -Uz bashcompinit && bashcompinit
2021-02-15 20:53:16 -05:00
# ignore full hostnames in ssh config file
# the default ssh function does not seem to differentiate b/t
# HOST and HOSTNAME directives
zstyle ':completion:*:(ssh|scp|rsync|sshfs):*:hosts' ignored-patterns '*.yavin4.ch'
## --------------------------------------------------
# syntax highlighting a la fish
## --------------------------------------------------
for hlpath in zsh/plugins/zsh-syntax-highlighting zsh-syntax-highlighting; do
if [[ -e "/usr/share/$hlpath/zsh-syntax-highlighting.zsh" ]]; then
. "/usr/share/$hlpath/zsh-syntax-highlighting.zsh"
break
fi
done
## --------------------------------------------------
# history
## --------------------------------------------------
setopt hist_expire_dups_first
setopt hist_ignore_dups
setopt hist_reduce_blanks
setopt inc_append_history
setopt share_history
autoload -Uz up-line-or-beginning-search
autoload -Uz down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey '\eOA' up-line-or-beginning-search
bindkey '\e[A' up-line-or-beginning-search
bindkey '\eOB' down-line-or-beginning-search
bindkey '\e[B' down-line-or-beginning-search
## --------------------------------------------------
## PROMPT
## --------------------------------------------------
setopt prompt_subst
if [[ ${EUID} -eq 0 ]]; then
user_color="%F{red}"
else
user_color="%F{cyan}"
fi
HOSTNAME=""
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
HOSTNAME="@%M"
fi
2022-02-17 23:06:59 -05:00
## git prompt
zsh-git-off () {
export ZSH_USE_GIT=0
}
zsh-git-on () {
export ZSH_USE_GIT=1
}
zsh-git-on
PROMPT=""
RPROMPT=""
git_info() {
# Exit if not inside a Git repository
! git rev-parse --is-inside-work-tree > /dev/null 2>&1 && return
# Git branch/tag, or name-rev if on detached head
local GIT_LOCATION=$(git symbolic-ref -q HEAD || \
git name-rev --name-only \
--no-undefined --always HEAD)
GIT_LOCATION=${GIT_LOCATION#(refs/heads/|tags/)}
local AHEAD="%F{red}⇡NUM%f"
local BEHIND="%F{cyan}⇣NUM%f"
local MERGING="%F{magenta⚡%f"
local UNTRACKED="%F{red}●%f"
local MODIFIED="%F{yellow}●%f"
local STAGED="%F{green}●%f"
local -a DIVERGENCES
local -a FLAGS
local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
DIVERGENCES+=( "${AHEAD//NUM/$NUM_AHEAD}" )
fi
local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_BEHIND" -gt 0 ]; then
DIVERGENCES+=( "${BEHIND//NUM/$NUM_BEHIND}" )
fi
local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
FLAGS+=( "$MERGING" )
fi
if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then
FLAGS+=( "$UNTRACKED" )
fi
if ! git diff --quiet 2> /dev/null; then
FLAGS+=( "$MODIFIED" )
fi
if ! git diff --cached --quiet 2> /dev/null; then
FLAGS+=( "$STAGED" )
fi
local -a GIT_INFO
[[ ${#DIVERGENCES[@]} -ne 0 ]] && GIT_INFO+=( "${(j::)DIVERGENCES}" )
[[ ${#FLAGS[@]} -ne 0 ]] && GIT_INFO+=( "${(j::)FLAGS}" )
GIT_INFO+=( "%F{green}$GIT_LOCATION%f" )
echo "─(%B${(j: :)GIT_INFO}%b)"
}
function zle-line-init zle-keymap-select {
2022-02-17 23:06:59 -05:00
if [[ "$ZSH_USE_GIT" -eq 1 ]]; then
GIT=$(git_info)
else;
GIT=""
fi
NORMAL="%F{yellow}N%f"
INSERT="%F{cyan}I%f"
VIMODE="─%B(${${KEYMAP/vicmd/$NORMAL}/(main|viins)/$INSERT})%b"
USER="$user_color%n$HOSTNAME%f"
TIME="%F{green}%*%f"
WD="%F{green}%1~%f"
PROMPT=$'\n%B┌($USER)─($TIME)$VIMODE$GIT\n└─($WD)─>%b '
zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select
export KEYTIMEOUT=1
## --------------------------------------------------
## Title
## --------------------------------------------------
precmd () { echo -ne "\033]0;urxvt: ${PWD}\007" }
## --------------------------------------------------
## Aliases
## --------------------------------------------------
alias als='alias | grep'
alias hs='history 1 | grep'
alias grep='grep --color=auto'
alias sudo='sudo '
if [ -e /bin/colordiff ]; then
alias diff=colordiff
else
alias diff='diff --color'
fi
alias ls='ls --color'
if [ -e /bin/exa ]; then
alias ll='exa -alhg --group-directories-first'
alias llt='exa -T'
alias llg='ll --git'
else
alias ll='ls -alh'
[ -e /bin/tree ] && alias llt='tree'
fi
setopt autopushd
alias d='dirs -v | head -10'
alias d1='cd -'
alias d2='cd -2'
alias d3='cd -3'
alias d4='cd -4'
alias d5='cd -5'
alias d6='cd -6'
alias d7='cd -7'
alias d8='cd -8'
alias d9='cd -9'
alias u='cd ..'
alias uu='cd ../..'
alias uuu='cd ../../..'
alias uuuu='cd ../../../..'
alias uuuuu='cd ../../../../..'
alias reboot='sudo /sbin/reboot'
alias poweroff='sudo /sbin/poweroff'
alias halt='sudo /sbin/halt'
alias shutdown='sudo /sbin/shutdown'
alias sc="sudo systemctl"
alias sce="sudo systemctl enable"
alias scd="sudo systemctl disable"
alias scs="sudo systemctl start"
alias sct="sudo systemctl stop"
alias scr="sudo systemctl restart"
alias sca="systemctl status"
alias scu="systemctl --user"
alias scue="systemctl --user enable"
alias scud="systemctl --user disable"
alias scus="systemctl --user start"
alias scut="systemctl --user stop"
alias scur="systemctl --user restart"
if [ -e /bin/git ]; then
alias g='git'
alias gcl='git clone'
alias ga='git add'
alias grm='git rm'
alias ga='git add'
alias gus='git reset HEAD'
alias gm="git merge"
alias gmv='git mv'
alias gs='git status -s -b'
alias gp='git push'
alias gpo='git push origin'
alias gpom='git push origin master'
alias gr='git remote'
alias gd='git diff'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcm='git commit -v -m'
alias gcam="git commit -v -am"
alias gci='git commit --interactive'
alias gb='git branch'
alias gco='git checkout'
alias gg="git log --graph --pretty=format:'%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]' --abbrev-commit --date=relative"
alias ggs="gg --stat"
alias gw="git whatchanged"
alias gt="git tag"
alias gnew="git log HEAD@{1}..HEAD@{0}"
alias ggui="git gui"
fi
if [ -e /bin/chezmoi ]; then
alias cze="chezmoi edit"
alias czea="chezmoi edit --apply"
alias czd="chezmoi diff"
alias cza="chezmoi add"
alias czc="chezmoi cd"
fi
## --------------------------------------------------
## Manly Colors
## --------------------------------------------------
man() {
env LESS_TERMCAP_mb=$'\E[01;31m' \
LESS_TERMCAP_md=$'\E[01;38;5;13m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;3m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;10m' \
man "$@"
}
2021-06-10 23:36:24 -04:00
{{- if eq .chezmoi.hostname "petrucci4prez" }}
2021-02-15 20:53:16 -05:00
## --------------------------------------------------
# enable x11 clipboard sync
## --------------------------------------------------
function x11-clip-wrap-widgets() {
# NB: Assume we are the first wrapper and that we only wrap native widgets
# See zsh-autosuggestions.zsh for a more generic and more robust wrapper
local copy_or_paste=$1
shift
for widget in $@; do
if [[ $copy_or_paste == "copy" ]]; then
eval "
function _x11-clip-wrapped-$widget() {
zle .$widget
xclip -in -selection clipboard <<<\$CUTBUFFER
}
"
else
eval "
function _x11-clip-wrapped-$widget() {
CUTBUFFER=\$(xclip -out -selection clipboard)
zle .$widget
}
"
fi
zle -N $widget _x11-clip-wrapped-$widget
done
}
local copy_widgets=(
vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line
)
local paste_widgets=(
vi-put-{before,after}
)
x11-clip-wrap-widgets copy $copy_widgets
x11-clip-wrap-widgets paste $paste_widgets
2021-02-15 20:53:16 -05:00
## --------------------------------------------------
2021-03-20 14:55:46 -04:00
## Python/Ruby Virtual Environments
2021-02-15 20:53:16 -05:00
## --------------------------------------------------
2021-03-20 14:55:46 -04:00
# both of these languages have similar setups for their virtual envs which
# involve a) adding a "shims" directory to PATH and then b) adding some shell
# magic to automatically call the shims
zsh-pyenv () {
export PATH=$PYENV_ROOT/shims:$PATH
if command -v pyenv 1>/dev/null 2>&1; then
echo "Activating pyenv"
eval "$(pyenv init - | sed '/PATH/d' -)"
eval "$(pyenv virtualenv-init - | sed '/PATH/d' -)"
else
echo "pyenv not installed"
fi
}
2021-03-20 14:55:46 -04:00
zsh-rbenv () {
gempaths="$(/usr/bin/gem env gempath):"
export PATH="${gempaths//:/\/bin:}:$PATH"
export PATH=$RBENV_ROOT/shims:$PATH
if command -v rbenv 1>/dev/null 2>&1; then
echo "Activating rbenv"
eval "$(rbenv init - | sed '/PATH/d' -)"
else
echo "rbenv not installed"
fi
}
zsh-conda () {
if [[ -x $XDG_DATA_HOME/mambaforge/bin/conda ]]; then
eval "$($XDG_DATA_HOME/mambaforge/bin/conda shell.zsh hook)"
else
echo "conda installation not found"
fi
}
2021-06-12 15:42:06 -04:00
{{- end }}