forked from sorin-ionescu/prezto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zshrc
262 lines (238 loc) · 12.4 KB
/
zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#
# Executes commands at the start of an interactive session.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Source Prezto.
if [[ -s "${ZDOTDIR:-$HOME}/.zprezto/init.zsh" ]]; then
source "${ZDOTDIR:-$HOME}/.zprezto/init.zsh"
fi
# the following should be in zpreztorc but bindkey has no effect there
# https://github.com/zsh-users/zsh-autosuggestions#key-bindings
# use the suggested '^ ' since I need ^M (CR, enter key) and ^J (LF) for accept-line and ^O for accept+execute menu completion
bindkey '^ ' autosuggest-execute
bindkey '^X^I' glob-alias # '^ ' was glob-alias, which is useful to completely expand aliases and variables
bindkey '^X^T' autosuggest-toggle
# macOS
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
# debian/ubuntu
if [[ -e $commands[apt] ]]; then
alias agi='sudo apt install'
alias agx='sudo apt remove' # keeps config files
alias agX='sudo apt purge' # also removes config files
# alias agu='sudo apt-get upgrade -u'
alias agu='sudo apt update && sudo apt upgrade && sudo apt autoremove'
alias agU='sudo apt update && sudo apt full-upgrade && sudo apt autoremove' # full-upgrade may also remove packages
alias ags='apt search' # `apt-cache search` for single-line results
alias agd='apt show'
fi
# shell
alias v=nvim
# alias vf='v $(fzf -m --preview="bat --color=always {}")' # opens v without args on ctrl-c
# alias vf='fzf -m --preview="bat --color=always {}" | xargs -r nvim' # xargs can't use alias v
function vf() {
results=$(fzf -m --preview="bat --color=always {}")
[ ! -z "$results" ] && v $(expr $results) # need to split string again via expr
}
# alias c=pygmentize
alias c=bat
function mvp(){ # create parent directories before move
dir=$(dirname -- "${@: -1}".); dir=${dir%.} # fix dirname not respecting trailing slashes by appending a . and removing it after!
if [ ! -z "$dir" ]; then # for . and options like --help, $dir is empty and mkdir would complain
mkdir --parents --verbose -- "$dir"
fi
command mv $@
}
function dus() { du -h -d1 $1 | sort -h }
function mkcd(){ mkdir -p $1; cd $1 }
function rmcwd(){ x=`pwd`; if [ $x = `cd; pwd` ]; then echo "in home!"; else cd ..; rm -rf $x; fi }
function cwdsh(){ if [[ -z $2 ]]; then eval $1; else pu $2; eval $1; po; fi }
# prezto's ll is just 'ls -lh'
# alias le='eza --long --header --icons --git --group-directories-first'
alias le='eza --long --icons --git --group-directories-first'
alias lh='cwdsh "ls -d .*"' # only ls hidden files
alias lah='cwdsh "ls -lhd .*"' # as above but long
alias uniqc='sort | uniq -c | sort'
function rgd() { rg --json $@ | delta } # syntax-highlighted search results, https://dandavison.github.io/delta/grep.html
alias topm='\top -o %MEM'
function psmem(){ # e.g. psmem chrome
\top -bn 1 | grep $1 | cut -c32-36 - | awk '{s+=$1} END {print s}'
}
compdef _pids vmrss # pid completion for ~/dotfiles/bin/vmrss
# https://unix.stackexchange.com/a/81699/37512
alias wanip='dig @resolver4.opendns.com myip.opendns.com +short'
alias wanip4='dig @resolver4.opendns.com myip.opendns.com +short -4'
alias wanip6='dig @resolver1.ipv6-sandbox.opendns.com AAAA myip.opendns.com +short -6'
# alias newip='[[ -z $FRITZ_PASSWORD ]] && echo "Enter FritzBox password:" && read -s FRITZ_PASSWORD; wanip6 && fritzconnection --reconnect' # password was just needed for fritzhosts, but is also not checked, so `FRITZ_PASSWORD=foo fritzhosts` also works
alias newip='wanip6 && fritzconnection --reconnect; until ping -c1 google.de >/dev/null 2>&1; do sleep 1; done; wanip6' # pip install fritzconnection
alias ts="ts '%Y-%m-%d %H:%M:%S'" # prepends date & time as e.g. '2023-04-20 14:47:56' instead of the default 'Apr 20 14:47:56', example: `mosquitto_sub -h rpi3 -t 'sensors/mh-z19b' | ts`
# if brew command command-not-found-init > /dev/null 2>&1; then eval "$(brew command-not-found-init)"; fi # this takes >1s! do not use!
# mv $1 to $2 (e.g. /etc/foo to $2/etc/foo), ln -s and git add it
# function gln(){ TODO }
function http-status(){ curl -sI $1 | head -n1 | cut -d' ' -f2 }
function curl-ua() { curl -s $1 -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' --compressed }
alias co2w='watch "mosquitto_sub -h rpi3 -t sensors/mh-z19b -C 1 | jq .co2 | figlet -f doh"'
# convert all timestamps (10 digits) in a file or stdin to human-readable date and time
# https://stackoverflow.com/questions/2371248/how-to-convert-timestamps-to-dates-in-bash
# https://stackoverflow.com/questions/14036116/convert-timestamp-to-datetime-with-sed
# single timestamp: `date -r 1681993846` (macOS date), `date -d @1681993846` (GNU date)
# macOS awk has no strftime, would need `brew install gawk` -> use perl (can do `echo 1681993846 | ts2date` or `ts2date ~/.zsh_history`):
alias ts2date="perl -Mposix -pe 's/([0-9]{10})/POSIX::strftime( \"%Y-%m-%d %H:%M:%S\", gmtime(\$1))/eg'"
# searchable history with human-readable dates (tac reverses lines (most recent on top)):
# https://stackoverflow.com/questions/34150889/what-is-the-format-of-shell-history-files
# : <beginning time>:<elapsed seconds>;<command>
# Note that <elapsed seconds> is 0 most of the time since SHARE_HISTORY is set by prezto which is incompatible with INC_APPEND_HISTORY_TIME. Also see modules/history.
alias zsh-history="cat ~/.zsh_history | sed 's/^: //; s/:/ /; s/;/\t/' | ts2date | tac | less"
# git
if [[ -e $commands[hub] ]]; then
alias git=hub # https://github.com/cli/cli/blob/trunk/docs/gh-vs-hub.md
fi
alias lg=lazygit
alias gwd="git diff --no-ext-diff -- ':!/**package-lock.json' ':!**yarn.lock' ':!**pnpm-lock.yaml'" # git diff: don't show changes in lock files
alias gwD='git diff --no-ext-diff --word-diff=color' # prezto has mode plain (uses color and delimiters [-removed-] and {+added+}) as default
alias giD='git diff --no-ext-diff --cached --word-diff=color'
alias glr='git reflog --date=iso --pretty=medium' # show reflog with date of ref-change and of commit; same as `git log --walk-reflogs --date=iso` just with short commit hash
function gfcd(){
git clone $1 && cd $(basename $1 | sed 's/\.git//')
}
alias gwc='git clean -d -n' # list
alias gwC='git clean -d -f' # remove
function gac(){ gia ${1-"."}; gc ${1-"."} }
function gcA(){ gia .; gc }
alias gbxr='git push origin --delete' # delete remote branch
alias gbv='git branch -vv' # instead of just -v: also show remote-tracking branches
alias glg='git log --all --graph' # instead of git log --topo-order --graph --pretty=format:"${_git_log_oneline_format}"
function glcb(){ # git log for current/child branch commits only
parent=${1-$(git parent)} # see alias in ~/.gitconfig
if [ -z "$parent" ]; then
echo "No parent for current branch!"
else
git log --pretty=format:"$_git_log_medium_format" --no-merges $parent..
fi
}
alias gsw='git stash push --keep-index' # replace prezto's gsw (push (instead of save) allows for pathspecs (since git 2.13), message with -m)
alias gsW='git stash push --keep-index --include-untracked'
# TODO list stashes and unpushed changes
# should only list all **/.git/logs/refs/stash
# find -name stash -print0 | xargs -0 cat
function git-add-all-submodules(){
dir=${1-'.'} # will search in $1 but add in cwd!
for x in $(find $dir -type d -mindepth 1) ; do if [ -d "${x}/.git" ] ; then cd "${x}" ; origin="$(git config --get remote.origin.url)" ; cd - 1>/dev/null; echo "$x - $origin"; echo -n " "; git submodule add "${origin}" "${x}" ; fi ; done
}
# find all .git directories in $1 or .
function git-repos(){
dir=${1-'.'}
if [[ -e $commands[fd] ]]; then
# https://github.com/sharkdp/fd is 2-5x faster than find for this query
fd --type d --hidden --no-ignore '^\.git$' $dir
else
find $dir -type d -name ".git"
fi
}
# list my repos on github
alias gh-repos='gh repo list vogler -L999'
# list PR that introduced current commit in browser
alias gh-pr='gh pr list --state all --search $(git rev-parse HEAD) -w'
# git svn
# git svn doesn't know autostash...
function autostash(){ git stash | grep 'Saved working directory' && $* && git stash pop -q || $* }
alias sgfr='autostash git svn rebase'
alias sgc='git commit --verbose && git svn dcommit'
# docker
function docker-volumes () {
for volume in $(docker volume ls --format '{{.Name}}'); do
echo $volume
docker ps -a --filter volume="$volume" --format '{{.Names}}' | sed 's/^/ /'
done
}
# C
function gcc-run(){
gcc $1 && ./a.out && rm -f a.out
}
function gcc-eval(){ # needs double escape: gcc-eval 'printf("INT_MAX: %d\\n", INT_MAX)'
echo "#include <stdlib.h>\n #include <stdio.h>\n #include <assert.h>\n\
#include <limits.h>\n #include <stdint.h>\n #include <math.h>\n\
int main(){ $1; }" >! /tmp/gcc-eval.c &&
gcc-run /tmp/gcc-eval.c
}
# OCaml, opam
test -r /Users/voglerr/.opam/opam-init/init.zsh && . /Users/voglerr/.opam/opam-init/init.zsh > /dev/null 2> /dev/null || true
export OPAMKEEPBUILDDIR=true # support Merlin's Locate on all libs
export OCAMLPARAM="_,bin-annot=1"
export OCAMLRUNPARAM=b # enable backtraces on error
export OPAMWITHDOC=true # opam install --with-doc ...
alias utop='utop -init ~/dotfiles/.ocamlinit-utop -implicit-bindings -require batteries -require goblint-cil'
alias ocaml='rlwrap -H ~/.ocaml_history -D 2 -i -s 10000 ocaml'
function ocr(){
ocamlc $1 && ./a.out && rm -f a.out
rm -f $(basename $1 .ml).cm?
}
# Python
alias ipython='ipython --no-confirm-exit' # don't want to commit .ipython/ and its prompt is annoying
function archive(){ # save file(s) as (encrypted) archive to dropbox
dst=~/Dropbox/$1.7z
rm -f $dst
7z a -mhe=on -p -l $dst $* # if a password is given, the header will also be encrypted. resolves symlinks.
}
# if [[ "$OSTYPE" == darwin* ]]; then
# alias google-chrome='open -a "Google Chrome"'
# alias subl='open -a "Sublime Text"'
# else
# [ -f /home/linuxbrew/.linuxbrew/bin/brew ] && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
# [ -f ~/.linuxbrew/bin/brew ] && eval $(~/.linuxbrew/bin/brew shellenv)
# fi
# alias cya='~/gitdrop.sh save && sudo pm-suspend'
# function clip(){
# echo $* | xclip -selection clipboard
# }
# alias pingor='ping -c 1 google.com || sudo systemctl restart dhcpcd@enp0s3.service'
# eval "$(thefuck --alias)"
# function fsw(){ fswatch -o $1 | xargs -n1 "$2" }
# alias c='pygmentize -O style=monokai -f console256 -g'
if [[ -e $commands[gtm] ]]; then
source "${ZDOTDIR:-$HOME}/.zprezto/gtm-terminal-plugin/gtm-plugin.sh" # git time metric
fi
# f for fff (file manager, https://github.com/dylanaraps/fff) with cd on exit (q), use ctrl-c to exit w/o cd
f() {
fff "$@"
cd "$(cat "${XDG_CACHE_HOME:=${HOME}/.cache}/fff/.fff_d")"
}
# https://github.com/Canop/broot - function needed to cd into directory on alt+enter
# `broot -- install` modified too much
# via `broot --print-shell-function zsh`:
function br {
local cmd cmd_file code
cmd_file=$(mktemp)
if broot --outcmd "$cmd_file" "$@"; then
cmd=$(<"$cmd_file")
command rm -f "$cmd_file"
eval "$cmd"
else
code=$?
command rm -f "$cmd_file"
return "$code"
fi
}
# Powerlevel10k prompt: run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# fzf fuzzy finder: ** for completion, ctrl-t for files, alt-c for cd, ctrl-r for history
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# $(brew --prefix)/opt/fzf/install generated ~/.fzf.zsh but install script had prompts and file is only for macOS. Inlined:
if [[ "$OSTYPE" == darwin* ]]; then # macOS
source "/opt/homebrew/opt/fzf/shell/completion.zsh" 2> /dev/null
source "/opt/homebrew/opt/fzf/shell/key-bindings.zsh"
else # Debian: files as per `dpkg -L fzf`
source "/usr/share/doc/fzf/examples/completion.zsh" 2> /dev/null
source "/usr/share/doc/fzf/examples/key-bindings.zsh"
fi
# atuin: records shell history in sqlite with cwd, hostname etc., TUI for interactive search on ctrl-r
[[ -e $commands[atuin] ]] && eval "$(atuin init zsh)"
# node version manager via https://github.com/nvm-sh/nvm#install--update-script - nvm.sh already loaded in zprofile
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion