1
0
mirror of https://github.com/artizirk/dotfiles.git synced 2024-11-10 03:30:59 +02:00
dotfiles/.zshrc

373 lines
10 KiB
Bash
Raw Normal View History

2020-04-18 14:46:16 +03:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
2020-12-02 12:14:20 +02:00
# Use a REAL regex engine
zmodload zsh/pcre
2018-11-12 16:14:14 +02:00
# Virtualenv support
2022-12-22 20:53:57 +02:00
function _virtual_env_prompt () {
# new pyvenv has a seperate variable for custom prompt value
REPLY=${VIRTUAL_ENV_PROMPT+${VIRTUAL_ENV_PROMPT}}
# Try to read the prompt name form pyvenv.cfg
if [[ -z "${REPLY}" && -f "$VIRTUAL_ENV/pyvenv.cfg" ]]; then
# Matches lines with following syntax
# prompt = 'cool prompt'
# prompt = "cool prompt"
# prompt = cool
# prompt=cool prompt
# heredoc is used here so that I don't have to
# manually escape the regex 🤮
local prompt_re
read -r prompt_re <<-'EOF'
^prompt\s*=\s*['"]?(.+?)['"]?$
EOF
# -m turns on multiline support, this makes ^ and $ anchor work in multiline string
pcre_compile -m ${prompt_re}
# And now its supper easy to get the value
if pcre_match -- "$(<$VIRTUAL_ENV/pyvenv.cfg)"
then
REPLY="(${match[1]}) "
fi
fi
# support old-school virtualenv
if [[ -z "${REPLY}" ]]; then
REPLY=${VIRTUAL_ENV+(${VIRTUAL_ENV:t}) }
fi
2018-11-12 16:14:14 +02:00
}
2022-12-22 20:53:57 +02:00
grml_theme_add_token virtual-env -f _virtual_env_prompt '%F{magenta}' '%f'
2020-04-18 14:46:16 +03:00
2022-12-22 20:53:57 +02:00
function _config_env_prompt () {
2020-04-18 14:46:16 +03:00
REPLY=${CONFIG_ENV+(${CONFIG_ENV:t}) }
}
2022-12-22 20:53:57 +02:00
grml_theme_add_token config-env -f _config_env_prompt
2020-04-18 14:46:16 +03:00
zstyle ':prompt:grml:left:setup' items rc config-env virtual-env change-root user at host path vcs percent
2019-04-02 17:10:34 +03:00
# Disable right side sad smiley, works nicer with resized terminal
zstyle ':prompt:grml:right:setup' use-rprompt false
2018-11-12 16:14:14 +02:00
2023-09-26 12:00:10 +03:00
# disable PROMPT_EOL_MARK or also known as % on end of line
# https://unix.stackexchange.com/a/302710
#set +o prompt_cr +o prompt_sp
# Tell to the terminal about our current working directory
# NB: turns out that alacritty snopps that info from /proc/PID/cwd
xsource ~/.config/zsh/functions/osc7-pwd.zsh
2023-09-26 12:01:35 +03:00
# enable OSC 133 shell prompt start/end reporting
# so that the terminal can scroll jump between prompts
2023-09-26 13:02:47 +03:00
xsource ~/.config/zsh/functions/semantic-prompt.zsh
2023-09-26 12:01:35 +03:00
2020-04-18 21:16:57 +03:00
# Git based dotfiles setup start
2020-04-18 14:46:16 +03:00
function _config_activate {
export GIT_DIR=$HOME/.cfg/
export GIT_WORK_TREE=$HOME
CONFIG_ENV="config"
}
function _config_deactivate {
unset GIT_DIR GIT_WORK_TREE CONFIG_ENV
}
2018-11-12 16:14:14 +02:00
function config_status {
# Borrowed from https://mitxela.com/projects/dotfiles_management
(
_config_activate
for i in $(git ls-files); do
echo -n "$(git -c color.status=always status $i -s | sed "s#$i##")"
echo -e "¬/$i¬\e[0;33m$(git -c color.ui=always log -1 --format="%s" -- $i)\e[0m"
done
) | column -t --separator=¬ -T2
}
2020-04-18 14:46:16 +03:00
function config {
if [[ -n "$1" ]]; then
_config_activate
git $@
_config_deactivate
return
elif [[ -z "${CONFIG_ENV}" ]]; then
_config_activate
else
_config_deactivate
fi
}
2020-04-18 21:16:57 +03:00
# git based dotfiles setup end
# GRML profiles
2020-12-01 16:35:19 +02:00
zstyle ':chpwd:profiles:/home/arti/code/krakul(|/|/*)' profile krakul
function chpwd_profile_krakul() {
2020-04-18 21:16:57 +03:00
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
2020-12-01 16:35:19 +02:00
export GIT_AUTHOR_EMAIL="arti@krakul.eu"
export GIT_COMMITTER_EMAIL="arti@krakul.eu"
2020-04-18 21:16:57 +03:00
}
2020-12-01 16:35:19 +02:00
function chpwd_leave_profile_krakul() {
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
unset GIT_AUTHOR_EMAIL \
GIT_COMMITTER_EMAIL
2020-04-18 21:16:57 +03:00
}
chpwd_profiles
2022-01-18 07:56:57 +02:00
# for some reason zsh does not pick up git-subrepo custom command
# I have to add it manually to git completion
zstyle ':completion:*:*:git:*' user-commands subrepo:'perform git-subrepo operations'
# First clear grml-zsh-config provided directory hashes
hash -d -r
# shorten dir name in prompt
hash -d krakul=~/code/krakul
function hash_projects() {
# hash everything under krakul projects
local project_folder
for project_folder in ~/code/krakul/*; do
hash -d "${project_folder:t}"="${project_folder}"
done
unset project_folder
}
hash_projects
# remove need to add ~ in front of hashed dirs
setopt cdable_vars
2020-04-18 21:16:57 +03:00
# Enable or disable python virtual env
function chpwd_auto_python_venv() {
local venv_activation_script=((../)#(.|)(v|)env/bin/activate(#qN.omY1:a))
# dont check for (current_venv != found_venv) here because
# we want to support swtiching between venvs
if [[ -n "${venv_activation_script}" && -z "${VIRTUAL_ENV}" ]]; then
# we found venv dir that is not yet activated or is different from currently active one
source "${venv_activation_script}"
elif [[ -z "${venv_activation_script}" && -n "${VIRTUAL_ENV}" ]]; then
# we have activated virtual env but we cant find venv folder anymore
deactivate
fi
2020-04-18 21:16:57 +03:00
}
chpwd_functions+=(chpwd_auto_python_venv)
2021-03-18 11:46:40 +02:00
chpwd_auto_python_venv # Try to enter venv on shell startup
2020-04-18 14:46:16 +03:00
# https://github.com/Tarrasch/zsh-autoenv
xsource ~/.config/zsh/zsh-autoenv/autoenv.zsh
2021-03-18 11:46:40 +02:00
# gitflow-zshcompletion-avh
xsource /usr/share/zsh/site-functions/git-flow-completion.zsh
# pkg not found
2021-03-18 11:46:40 +02:00
xsource /usr/share/doc/pkgfile/command-not-found.zsh
2018-11-12 16:14:14 +02:00
# default is 40%, and that is too much, limit to less lines
FZF_TMUX_HEIGHT=6
# other shortcuts already use reverse, except history search for some reason..
FZF_CTRL_R_OPTS="--reverse"
# Arch Linux
xsource /usr/share/fzf/key-bindings.zsh
xsource /usr/share/fzf/completion.zsh
# Debian
xsource /usr/share/doc/fzf/examples/key-bindings.zsh
# fzf history search with support for Ctrl+E history edit like with zaw widget before
if typeset -f fzf-history-widget > /dev/null; then
# Copied form key-bindings.zsh
# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort --expect=ctrl-e $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
local ret=$?
if [ -n "$selected" ]; then
local accept=0
if [[ $selected[1] == ctrl-e ]]; then
accept=1
shift selected
fi
num=$selected[1]
if [ -n "$num" ]; then
zle vi-fetch-history -n $num
[[ $accept = 0 ]] && zle accept-line
fi
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
2020-04-18 14:46:16 +03:00
fi
2018-11-12 16:14:14 +02:00
2021-03-18 11:46:40 +02:00
# Paste handling
autoload -Uz bracketed-paste-magic
zle -N bracketed-paste bracketed-paste-magic
autoload -Uz bracketed-paste-url-magic
zle -N bracketed-paste bracketed-paste-url-magic
# Reverse `cd -<TAB>` history menu
setopt pushdminus
#
# Utility functions
function new-scratch {
2023-06-21 20:58:08 +03:00
local cur_dir="$HOME/scratch"
local new_dir="$HOME/tmp/scratch-`date +'%s'`"
2021-03-18 11:46:40 +02:00
mkdir -p $new_dir
ln -nfs $new_dir $cur_dir
cd $cur_dir
echo "New scratch dir ready for grinding ;>"
}
histsearch() { fc -lim "*$@*" 1 }
alias pacman="sudo pacman"
alias py="python"
alias py2="python2"
alias ll="ls -lh"
alias la="ls -a"
alias dmesg="dmesg -L"
alias disapprove="firefox 'data:text/html;base64,PGRpdiBzdHlsZT0idGV4dC1hbGlnbjpjZW50ZXI7Zm9udC1zaXplOjU1dm1pbiI+JiMzMjMyO18mIzMyMzI7PC9kaXY+Cg=='"
alias ipy="ipython"
alias htop="htop -d 10"
alias ip="ip -color=auto"
alias cp="cp --reflink=auto"
alias gitg="LANG=en_US.UTF-8 gitg"
export EDITOR=nvim
export VISUAL=nvim
2020-05-05 17:26:22 +03:00
MAILCHECK=0
MAIL=~/Mail
2018-11-12 16:14:14 +02:00
2019-01-01 23:29:32 +02:00
# iostat colors
export S_COLORS=auto
if command -v ruby > /dev/null; then
2022-04-07 10:51:33 +03:00
ruby_gem_user_dir="$(ruby -e 'print Gem.user_dir')/bin"
else
ruby_gem_user_dir=''
fi
# ZSH allows $PATH to be used as an array
path=(
~/.bin
~/.local/bin
/usr/bin/site_perl
/usr/bin/vendor_perl
/usr/bin/core_perl
"${ruby_gem_user_dir}"
$path
)
2018-11-12 16:14:14 +02:00
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
2018-11-12 16:14:14 +02:00
HISTSIZE=100000
SAVEHIST=100000
unsetopt share_history
setopt INC_APPEND_HISTORY_TIME
# by default show 3 months of calendar with week number
# but also allow to override it
function cal {
local CALENDAR=$(which -p cal)
if [[ $# -eq 0 ]]; then
$CALENDAR -m -w3
else
$CALENDAR -m "$@"
fi
}
2018-11-12 16:14:14 +02:00
# this fixes gedit python plugins that otherwise are broken under venv
2018-11-12 16:14:14 +02:00
function gedit {
local REAL_PATH=$PATH;
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
REAL_PATH=$_OLD_VIRTUAL_PATH;
fi
PATH=$REAL_PATH env -u VIRTUAL_ENV /usr/bin/gedit $@;
}
2019-03-08 14:45:36 +02:00
2023-06-28 20:01:45 +03:00
# NodeJS people really love to erase the terminal scrollback buffer
# running npm through cat usually solves it
# https://nodejs.org/api/tty.html#tty
# https://github.com/facebook/create-react-app/issues/2495
function npm {
(
export COLOR=1
export REACT_APP_NO_CLEAR_CONSOLE=1
exec /usr/bin/npm $@ | cat
)
}
function ssh {
(
2023-09-26 13:37:17 +03:00
if [[ "$TERM" == "foot" ]]; then
export TERM=xterm-256color
fi
exec /usr/bin/ssh $@
2023-06-28 20:01:45 +03:00
)
}
2019-03-08 14:45:36 +02:00
# i don't like that systemd by default uses a pager
export SYSTEMD_PAGER=''
2019-06-20 13:01:14 +03:00
# set man max width
export MANWIDTH=80
2020-04-14 17:41:39 +03:00
# make npm gyp faster
export JOBS=8
2020-10-20 16:05:49 +03:00
2021-05-18 17:16:58 +03:00
# under tmux less needs this to support scrolling
if [[ "$TERM" == "" ]]; then
export LESS='--mouse --wheel-lines=3'
fi
2021-05-18 17:16:58 +03:00
2020-10-20 16:05:49 +03:00
# https://neovim.io/doc/user/various.html#less
function vless {
/usr/share/nvim/runtime/macros/less.sh $@
}
2021-06-02 16:41:10 +03:00
# If running under windows with pageagent then use it
# https://github.com/benpye/wsl-ssh-pageant
if [[ -e /mnt/c/wsl-ssh-pageant/ssh-agent.sock ]]; then
export SSH_AUTH_SOCK=/mnt/c/wsl-ssh-pageant/ssh-agent.sock
fi
2021-06-03 15:09:23 +03:00
# We are running under WSL
if [[ -e /dev/lxss ]]; then
# Create pretty serial device names
reg.exe query HKLM\\HARDWARE\\DEVICEMAP\\SERIALCOMM | awk '/Device/ {gsub("\r","",$3); print "/dev/ttyS" substr($3,4), "/dev/tty" substr($1,9)}' | sudo xargs -n2 ln -sf
fi
function nitroid {
# Switch Nitrokey Start Identity
# https://github.com/Nitrokey/pynitrokey/tree/70398d9d87d002c14402591dfa6e4fe9e7182351#switching-id
local ID=$1
if ! [[ "$ID" =~ [0-2] ]]; then
echo "Usage: $0 ID"
echo "Where identity ID is in range 0-2"
return 1
fi
if gpg-connect-agent --hex "scd apdu 00 85 00 0$ID" /bye | grep -q ERR; then
sleep 1
gpg-connect-agent --decode "scd serialno" /bye
else
echo "already set"
fi
}
compdef 'compadd -X "Identity" 0 1 2' nitroid
2023-09-19 20:09:13 +03:00
function cpr {
rsync --archive -hh --partial --info=stats1,progress2 "$@"
}