nixos-config/dotfiles/zsh-init.sh

162 lines
4.1 KiB
Bash

# vim: ft=zsh
setopt appendhistory notify
unsetopt beep nomatch
#
# 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
}
# terminal integration
# https://codeberg.org/dnkl/foot/wiki#zsh
function osc7-pwd() {
emulate -L zsh # also sets localoptions for us
setopt extendedglob
local LC_ALL=C
printf '\e]7;file://%s%s\e\' $HOST ${PWD//(#m)([^@-Za-z&-;_~])/%${(l:2::0:)$(([##16]#MATCH))}}
}
function chpwd-osc7-pwd() {
(( ZSH_SUBSHELL )) || osc7-pwd
}
function precmd-osc133-marker() {
if ! builtin zle; then
print -n "\e]133;D\e\\"
fi
print -Pn "\e]133;A\e\\"
}
function preexec-osc133-marker() {
print -Pn "\e]133;B\e\\"
print -n "\e]133;C\e\\"
}
autoload -Uz add-zsh-hook
add-zsh-hook -Uz chpwd chpwd-osc7-pwd
add-zsh-hook -Uz precmd precmd-osc133-marker
add-zsh-hook -Uz preexec preexec-osc133-marker
# 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
# Extremely funny
if [[ -z "$TMUX" ]] && false; then
if [[ -n "$IN_VIM" ]]; then
vim() {
if [[ -z "$1" ]]; then
# plain vim. get splashed.
command vim --server /tmp/nvim.sock --remote-send "<C-\\><C-n>:split<CR><C-w>k:resize 1<CR><C-w>j:lcd $PWD<CR>:lua splash()<CR>"
elif [[ "$1" = -* ]]; then
# some sort of flag. do not touch
command vim "$@"
else
# vim with files. shove all of these into the buffer list, do a split, minimize the original window, and advance the new window in the buflist
command vim --server /tmp/nvim.sock --remote-send "<C-\\><C-n>:split<CR><C-w>k:resize 1<CR><C-w>j:lcd $PWD<CR>:arglocal $*<CR>"
fi
}
else
# not in vim. connect that bad boy
vim() {
if [[ -z "$1" ]]; then
# plain vim. connect
command vim --server /tmp/nvim.sock --remote-send "<C-\\><C-n>:lua newsplash(\"$PWD\")<CR>"
command vim --server /tmp/nvim.sock --remote-ui
elif [[ "$1" = -* ]]; then
# some sort of flag. do not touch
command vim "$@"
else
# vim with files. open them all in a new tab
command vim --server /tmp/nvim.sock --remote-send "<C-\\><C-n>:lua newfiles(\"$PWD\", \"$*\")<CR>"
command vim --server /tmp/nvim.sock --remote-ui
fi
}
fi
fi