nixos-config/dotfiles/zsh-init.sh

158 lines
4.3 KiB
Bash

# vim: ft=zsh
setopt appendhistory notify
unsetopt beep nomatch
#
# key binding configuration
#
# create a zkbd compatible hash
# to add other keys to this hash, see: man 5 terminfo
typeset -A key
key[Home]=${terminfo[khome]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}
key[Backspace]=${terminfo[kbs]}
# setup keybindings!
bindkey -v
export KEYTIMEOUT=1
[[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" backward-delete-char
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey "${key[Right]}" forward-char
[[ -n "${key[Backspace]}" ]] && bindkey "${key[Backspace]}" backward-delete-char
[[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" up-history
[[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" down-history
bindkey '^P' up-history
bindkey '^N' down-history
bindkey '^h' backward-delete-char
bindkey '^w' backward-kill-word
bindkey '^r' history-incremental-search-backward
bindkey -a '/' history-incremental-search-backward
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
function enter-editing-mode () {
printf '%s' "${terminfo[smkx]}"
}
function exit-editing-mode () {
printf '%s' "${terminfo[rmkx]}"
}
zle -N zle-line-init enter-editing-mode
zle -N zle-line-finish exit-editing-mode
fi
#
# Aliases
#
# standard functions
function nixos-edit() {
(cd ~/nixos-config && vim configuration.nix)
}
function nixos-apply() {
sudo nixos-rebuild switch --flake ~/nixos-config#$HOST "$@"
}
lsflags=()
if ls --group-directories-first &>/dev/null; then
lsflags+=("--group-directories-first")
fi
alias ls="ls ${lsflags[@]} --color=auto";
alias ll="ls -lh";
alias lh="ll -ab";
alias l="ls -ab";
# https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell
function urldecode() {
python3 -c "import sys; from urllib.parse import unquote; print(unquote(sys.stdin.read()));"
}
function highlight() {
THING="$1"
shift
egrep --color=always "$THING|\$" "$@"
}
function rmida () {
rm -f *.idb *.i64 *.id0 *.id1 *.id2 *.id3 *.nam *.til
}
function rustc() { $(/bin/which rustc) "$@" && echo "Good girl." }
function scale () {
INP=$1
OUT=$2
SCALE=${3-2}
SIZE="$(file $INP | egrep -o '[[:digit:]]+ x [[:digit:]]+')"
X=$(cut -d' ' -f1 <<<$SIZE)
Y=$(cut -d' ' -f3 <<<$SIZE)
convert $INP -size $(($X / $SCALE))x$(($Y / $SCALE)) $OUT
}
# watch a file and display a diff between an old and new version with respect to some command
function watchdiff() {
WATCHME="$1"
NEWFILE=$(mktemp)
OLDFILE=$(mktemp)
shift
trap "rm -f $NEWFILE $OLDFILE; return" INT
while true; do
inotifywait -q -e close_write "$WATCHME"
"$@" >$NEWFILE
date
diff -u $OLDFILE $NEWFILE | perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | colordiff
mv $NEWFILE $OLDFILE
done
}
# virtualenv integration
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
function tmpvirtualenv() {
rmvirtualenv test &>/dev/null
mkvirtualenv "$@" test && workon test
}
# default virtualenv
if [ -d ~/.virtualenvs/default ]; then
. ~/.virtualenvs/default/bin/activate
fi
# standard vars
export PYTHONBREAKPOINT="ipdb.set_trace"
#export UV_USE_IO_URING=0 # TODO remove - quitting nvim on python files hangs
export COLORTERM=1
export SHELL=$(which zsh)
export npm_config_prefix=~/.local
# site vars, functions, and aliases
if [ -e ~/.site_aliases.sh ]; then
source ~/.site_aliases.sh
fi