Blob


1 # sh/ksh initialization
3 [ -f $HOME/.kshrc.private ] && . $HOME/.kshrc.private
5 #############################################################################
6 # ALIASES
7 #############################################################################
9 if command -v colorls > /dev/null ; then
10 LS='colorls'
11 export CLICOLOR=1
12 else
13 LS='ls'
14 fi
16 if [[ $(uname -s) == "Linux" ]]; then
17 alias ls='ls --color=auto -h --file-type -s'
18 alias ll='ls -l'
19 alias rm='rm --preserve-root --one-file-system -I'
20 alias cps='sync ; cvs up ; sync'
21 if [ -x /usr/bin/dircolors ]; then
22 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
23 fi
24 bind -m '^L'=clear'^J'
25 elif [[ $(uname -s) == "OpenBSD" ]]; then
26 alias ls='$LS'
27 alias ll='$LS -Flho'
28 alias cps='sync ; opencvs up ; sync'
30 bind '^L'=clear-screen
32 # Enable SIGINFO with ^T
33 stty status ^T
34 elif [[ $(uname -s) == "Darwin" ]]; then
35 alias ls='$LS -G'
36 alias ll='$LS -GFlho'
38 bind '^L'=clear-screen
39 export MACPATH=/opt/homebrew/bin/:/opt/homebrew/sbin
40 elif [[ $(uname -s) == "FreeBSD" ]]; then
41 alias ls='$LS -G'
42 alias ll='$LS -GFlho'
44 bind '^L'=clear-screen
46 # Enable SIGINFO with ^T
47 stty status ^T
49 export TERM=xterm-256color
50 fi
52 if [[ $HIGHDPI -eq 1 ]]; then
53 alias mpv='mpv --no-audio-display --audio-channels=stereo --geometry=50%+1430+300 --sub-scale=0.5'
54 else
55 alias mpv='mpv --no-audio-display --audio-channels=stereo --geometry=50%+950+200 --sub-scale=0.7'
56 fi
58 alias .2='cds ../..'
59 alias .3='cds ../../..'
60 alias .4='cds ../../../..'
61 alias j='jump'
62 alias c='cvs'
63 alias h='history -60 | sort -k2 | uniq -f2 | sort -bn'
64 alias sudo='sudo -H'
65 alias mc='mc --color'
66 alias pwgen='pwgen -s'
67 alias pkg_add='pkg_add -V'
68 alias dt='dtoggle'
69 alias tty-clock='tty-clock -s -c'
70 alias chromium='chromium --disk-cache-dir=/tmp'
71 alias open="xdg-open"
72 alias ffplay='ffplay -hide_banner'
73 alias gps='sync ; git pull ; sync'
74 alias cal='cal -m -w'
75 alias ed='ed -p*'
76 alias qw='pkill -9 xidle'
77 alias cc='cc -fdiagnostics-color'
78 alias weather='curl http://wttr.in/Karlsruhe'
79 alias dig='dig +noall +answer +comment'
80 alias gdb='gdb -q'
81 alias egdb='egdb -q'
83 #############################################################################
84 # FUNCTIONS
85 #############################################################################
87 # Following three functions from "shell FU by Isaac Levy" presentation
88 yell() { echo "$0: $*" >&2; }
89 die() { yell "$*"; exit 111; }
90 try() { "$@" || die "cannot $*"; }
92 # Quick n dirty hack to generate a RSA key and a X509 cert
93 gennewcert() {
94 [[ -z "$1" ]] && echo "No alias provided" && return
96 openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
97 -keyout $1.key.pem -new -subj /CN=$1 -out $1.cert.pem \
98 -addext subjectAltName=DNS:$1
99 }
101 # Neat trick from https://github.com/lf94/peek-for-tmux/blob/master/README.md
102 p() {
103 tmux split-window -p 33 more $@ || exit;
106 g() {
107 if [ -d ".got" ]; then
108 got "$@"
109 else
110 git "$@"
111 fi
114 enablevideoconf() {
115 echo "[+] Enable video recording"
116 doas sysctl kern.video.record=1
117 echo "[+] Enable audio recording"
118 doas sysctl kern.audio.record=1
119 echo "[+] Set mic to mic2"
120 doas mixerctl record.adc-0:1_source=mic2
121 echo "[+] Adjust input level"
122 sndioctl input.level=0.25
125 disablevideoconf() {
126 echo "[+] Disable video recording"
127 doas sysctl kern.video.record=0
128 echo "[+] Disable audio recording"
129 doas sysctl kern.audio.record=0
132 # Connect to a vmm(4) VM which uses the "local interface" networking scheme
133 vmmssh() {
134 if [ -z "$1" ]; then
135 echo "Usage: vmmssh <name of the vmm VM> [user]"
136 echo ""
137 echo "If no user name is provided, root is used"
138 return
139 fi
141 local _user="${2:-root}"
142 local _id=$(vmctl show $1| tail -1 | awk '{ print $1}')
144 if [ "${_id}" = "ID" ]; then
145 echo "No VM named $1 found"
146 return
147 fi
149 ssh -o StrictHostKeyChecking=no -l ${_user} 100.64.${_id}.3
152 checklatestsnap() {
153 ftp -MVo- http://172.23.5.36/pub/OpenBSD/snapshots/$(uname -m)/BUILDINFO
154 ftp -MVo- "$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)/BUILDINFO"
155 ftp -MVo- http://ftp.openbsd.org/pub/OpenBSD/snapshots/$(uname -m)/BUILDINFO
158 lostandfoundcheck() {
159 for mp in $(mount -t ffs | cut -d ' ' -f 3); do
160 [[ -d ${mp}/lost+found ]] && echo "${mp}/lost+found exists"
161 done
164 kshrc() {
165 . $HOME/.kshrc
168 updatesrc() {
169 if [ ! -d "/var/git" ]; then
170 local _oldpwd="$PWD"
171 cd /usr/src && {
172 sync && git pull ; sync
174 cd /usr/ports && {
175 sync && git pull ; sync
177 cd "$_oldpwd"
178 else
179 local _oldpwd="$PWD"
180 cd /var/git/src.git && {
181 sync && got fetch; sync
183 cd /usr/src && {
184 sync && got up && sync
186 cd /var/git/ports.git && {
187 sync && got fetch; sync
189 cd /usr/ports && {
190 sync && got up && sync
192 cd "$_oldpwd"
193 fi
196 updatepkgs() {
197 local _option="" _args=$1
198 if [[ -n $(sysctl -n kern.version | cut -d ' ' -f2 | grep beta) ]]; then
199 _option="-D snap"
200 fi
202 sync && doas pkg_add -ui $_option $_args
203 sync
205 return
207 if [ ! -d /var/pkg_mirror ]; then
208 doas mkdir /var/pkg_mirror
209 fi
210 doas mount_nfs -s -a 4 -T -r 32768 -w 32768 -o noatime,ro,intr mx.xosc.net:/pkg /var/pkg_mirror
211 doas umount /var/pkg_mirror/
214 getbsdrd() {
215 local _mirror="$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)"
217 ftp -V -o /tmp/bsd.rd "$_mirror/bsd.rd" || return 1
218 ftp -V -o /tmp/SHA256.sig "$_mirror/SHA256.sig" || return 1
220 cd /tmp && signify -C -p "/etc/signify/openbsd-$(uname -r | tr -d '.')-base.pub" -x /tmp/SHA256.sig bsd.rd
223 getbsdvm() {
224 local _mirror="$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)"
226 ftp -V -o /tmp/bsd "$_mirror/bsd" || return 1
227 ftp -V -o /tmp/SHA256.sig "$_mirror/SHA256.sig" || return 1
229 cd /tmp && signify -C -p "/etc/signify/openbsd-$(uname -r | tr -d '.')-base.pub" -x /tmp/SHA256.sig bsd && doas mv /tmp/bsd /bsd.vm
232 openports() {
233 local _filter1='internet'
234 local _filter2='icmp|raw|<-|->'
236 if [ "$1" = "-4" ]; then
237 _filter2="internet6|${_filter2}"
238 elif [ "$1" = "-6" ]; then
239 _filter1="internet6"
240 fi
242 printf "%-10s %-13s %-5s %-5s %-9s %s\n" "USER" "COMMAND" "PID" "PROTO" "FAMILY" "LOCAL ADDRESS"
243 fstat | grep "${_filter1}" | grep -vE "${_filter2}" | awk '{ printf("%-10s %-13s %5s %-5s %-9s %s\n", $1,$2,$3,$7,$5,$9); }' | sort -u
246 psearch() {
247 local pt="/usr/ports"
249 [[ -z ${1} ]] && echo "Need a keyword to search for" && return
250 [[ ! -d ${pt} ]] && echo "Cannot find ports tree" && return
252 cd ${pt} && make search name=${1}
255 sshopen() {
256 # The following is only needed on WSL not on other OSes
257 [ -f "/proc/version" ] && grep -qE "(Microsoft|WSL)" /proc/version 2> /dev/null
258 if [ $? -eq 0 ]; then
259 /usr/bin/keychain -q --nogui $HOME/.ssh/id_ed25519
260 return
261 fi
263 # Find all public keys...
264 for i in $(find $HOME/.ssh/ -maxdepth 1 -name "*.pub"); do
265 # ... and strip the .pub suffix
266 _key=`echo $i | sed -e 's/\.pub//'`
267 command ssh-add $_key
268 done
271 cvspsdiff() {
272 [ -z "$1" ] && return
273 cvsps -q -s "$1" -g | cdiff
276 psg() {
277 ps aux | grep "$@" | grep -v "grep $@"
280 qrcodegen() {
281 # Idea from https://dataswamp.org/~solene/2021-03-25-computer-to-phone-text.html
282 xclip -o | qrencode -o - > ~/qrclip.png && sxiv -g 600x600 ~/qrclip.png && rm ~/qrclip.png
285 cds() {
286 test -z "$1" && cd $HOME && ls -lh && return
287 cd "$1" && ls -lah
290 pkg_search() {
291 pkglocate "$1" | cut -d ':' -f 1 | sort -u | fzf
294 mkcd() {
295 [[ -n $1 ]] || return 0
296 [[ -d $1 ]] || mkdir -p "$1"
297 [[ -d $1 ]] && builtin cd "$1"
300 cget() {
301 curl -OL --compressed "$@"
304 showwifi() {
305 ifconfig | grep ieee | awk {'print $3'}
308 # Show infos about my external IP address
309 showmyipaddress() {
310 echo "My external IPv4 : $(ftp -4 -M -o - http://icanhazip.com 2> /dev/null)"
311 echo "My external IPv6 : $(ftp -6 -M -o - http://icanhazip.com 2> /dev/null)"
312 echo "My external PTR : $(ftp -M -o - http://icanhazptr.com 2> /dev/null)"
315 calc() {
316 echo "scale=3;$@" | bc -l
319 # Copied from https://github.com/agkozak/polyglot/ MIT license
320 _polyglot_branch_status() {
321 POLYGLOT_REF="$(env git symbolic-ref --quiet HEAD 2> /dev/null)"
322 case $? in # See what the exit code is.
323 0) ;; # $POLYGLOT_REF contains the name of a checked-out branch.
324 128) return ;; # No Git repository here.
325 # Otherwise, see if HEAD is in a detached state.
326 *) POLYGLOT_REF="$(env git rev-parse --short HEAD 2> /dev/null)" || return ;;
327 esac
329 if [ -n "$POLYGLOT_REF" ]; then
330 if [ "${POLYGLOT_SHOW_UNTRACKED:-1}" -eq 0 ]; then
331 POLYGLOT_GIT_STATUS=$(LC_ALL=C GIT_OPTIONAL_LOCKS=0 env git status -uno 2>&1)
332 else
333 POLYGLOT_GIT_STATUS=$(LC_ALL=C GIT_OPTIONAL_LOCKS=0 env git status 2>&1)
334 fi
336 POLYGLOT_SYMBOLS=''
338 case $POLYGLOT_GIT_STATUS in
339 *' have diverged,'*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}&*" ;;
340 esac
341 case $POLYGLOT_GIT_STATUS in
342 *'Your branch is behind '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}&" ;;
343 esac
344 case $POLYGLOT_GIT_STATUS in
345 *'Your branch is ahead of '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}*" ;;
346 esac
347 case $POLYGLOT_GIT_STATUS in
348 *'new file: '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}+" ;;
349 esac
350 case $POLYGLOT_GIT_STATUS in
351 *'deleted: '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}x" ;;
352 esac
353 case $POLYGLOT_GIT_STATUS in
354 *'modified: '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}!!" ;;
355 esac
356 case $POLYGLOT_GIT_STATUS in
357 *'renamed: '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}->" ;;
358 esac
359 case $POLYGLOT_GIT_STATUS in
360 *'Untracked files:'*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}?" ;;
361 esac
363 [ -n "$POLYGLOT_SYMBOLS" ] && POLYGLOT_SYMBOLS=" $POLYGLOT_SYMBOLS"
365 printf ' (%s%s)' "${POLYGLOT_REF#refs/heads/}" "$POLYGLOT_SYMBOLS"
366 fi
368 unset POLYGLOT_REF POLYGLOT_GIT_STATUS POLYGLOT_SYMBOLS
371 lpf() {
372 local _pf=${1:-/etc/pf.conf}
373 doas pfctl -n -f ${_pf} && doas pfctl -F rules && doas pfctl -f ${_pf}
376 # Disable history logging for this shell
377 nohistory() {
378 HISTFILE=/dev/null
381 tog() {
382 TOG_COLORS=1 TERM=xterm /usr/local/bin/tog "$@"
385 # http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
387 export MARKPATH=$HOME/.marks
388 jump() {
389 cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
391 mark() {
392 mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
394 unmark() {
395 rm -i "$MARKPATH/$1"
397 marks() {
398 ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
401 #############################################################################
402 # COMPLETIONS
403 #############################################################################
405 # Mostly copied from
406 # https://github.com/qbit/dotfiles/blob/master/common/dot_ksh_completions
408 if [ -d ~/.password-store ]; then
409 PASS_LIST=$(
410 cd ~/.password-store
411 find . -type f -name \*.gpg | sed 's/^\.\///' | sed 's/\.gpg$//g'
414 set -A complete_tpm_1 -- $PASS_LIST usage
415 set -A complete_tpm_2 -- $PASS_LIST edit insert show rm
416 fi
418 set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
420 set -A complete_ifconfig_1 -- $(ifconfig | grep ^[a-z] | cut -d: -f1)
422 if [ -d /var/db/pkg ]; then
423 PKG_LIST=$(/bin/ls -1 /var/db/pkg)
424 set -A complete_pkg_info -- $PKG_LIST
426 alias dpkgdel="doas pkg_delete"
427 set -A complete_dpkgdel_1 -- $PKG_LIST
428 fi
430 # relayctl completion. Second level only for 'show'
431 set -A complete_relayctl_1 -- monitor show load poll reload stop redirect table host log
432 set -A complete_relayctl_2 -- summary hosts redirects relays routers sessions
434 set -A complete_unwindctl_1 -- reload log status
436 if [ -d /etc/rc.d ]; then
437 RCD_LIST=$(/bin/ls /etc/rc.d)
438 set -A complete_rcctl_1 -- get getdef set check reload restart stop start disable enable order ls
439 set -A complete_rcctl_2 -- $RCD_LIST
441 alias drcctl="doas rcctl"
442 set -A complete_drcctl_1 -- get getdef set check reload restart stop start disable enable order ls
443 set -A complete_drcctl_2 -- $RCD_LIST
444 fi
446 set -A complete_tarsnap_1 -- --list-archives --print-stats --fsck --fsck-prune --nuke --verify-config --version --checkpoint-bytes --configfile --dry-run --exclude --humanize-numbers --keyfile --totals
448 set -A complete_got_1 -- $(got -h 2>&1 | sed -n s/commands://p)
450 # /tmp/.man-list is generated upon boot by /etc/rc.local with
451 # find /usr/share/man/ -type f | sed -e 's/.*\///' -e 's/\.[0-9]//' | sort -u
452 [[ -f /tmp/.man-list ]] && set -A complete_man -- $(cat /tmp/.man-list)
454 [[ -d $HOME/.marks ]] && set -A complete_j -- $(/bin/ls $HOME/.marks)
456 #############################################################################
457 # PROMPT
458 #############################################################################
460 _error_code() {
461 local _temp=$?
462 if [ $_temp -ne 0 ]; then
463 echo -n "\033[38;5;1m [$_temp]\033[m"
464 fi
467 if [ -z "$SSH_CLIENT" ]; then
468 PS1_TRENNER="!!"
469 else
470 PS1_TRENNER="@"
471 fi
473 if [[ $(id -u) -eq 0 ]]; then
474 PS1='\h$PS1_TRENNER\\033[0;101m\u\\033[0m \w [$?]\n\$ '
475 elif [[ $(whoami) = "xhr" ]] || [[ $(whoami) = "matthiaschmidt" ]]; then
476 PS1='\h \w$(_polyglot_branch_status)$(_error_code) \033[38;5;11m\$\033[m '
477 else
478 PS1='\h$PS1_TRENNER\u \w$(_polyglot_branch_status) [$?]\n\$ '
479 fi
481 #############################################################################
482 # MISC SETTINGS
483 #############################################################################
485 set bell-style none
487 #############################################################################
488 # VARIABLES
489 #############################################################################
491 [ -d $HOME/.keychain ] && . $HOME/.keychain/LWKA-6Q7VRN2-sh
493 LSCOLORS=Dxfxcxdxbxegedabagacad
494 HISTSIZE=30000
495 HISTFILE=$HOME/.sh_history
496 HISTCONTROL=ignoredups:ignorespace
497 BLOCKSIZE=M
498 PATH=$HOME/Documents/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:$MACPATH
499 LESSSECURE=1
500 PAGER='less -JWAceX'
501 LESS='-Xa'
502 PKG_PATH=http://172.23.5.36/pub/OpenBSD/%c/packages/%a:http://ftp.hostserver.de/pub/OpenBSD/%c/packages/%a
504 # better two-finger touchpad scrolling
505 export MOZ_USE_XINPUT2=1
506 # opengl acceleration
507 export MOZ_ACCELERATED=1
508 # force webrender to enable
509 export MOZ_WEBRENDER=1
511 export PATH HOME TERM LSCOLORS HISTSIZE BLOCKSIZE PAGER LESSSECURE PKG_PATH FETCH_CMD