logo

dotfiles

My dotfiles, one branch per machine, rebased on base git clone https://anongit.hacktivis.me/git/dotfiles.git/

.common.sh (8097B)


  1. #!/bin/sh
  2. # per-shell exports
  3. export GPG_TTY="$(tty)"
  4. # Aliases
  5. unalias bash; alias bash="SHELL=/bin/bash bash -l"
  6. unalias bc; alias bc="bc ~/Sources/git/hacktivis.me/git/blog/notes/bc.txt"
  7. unalias fuck; alias fuck='TF_ALIAS=fuck eval $(thefuck $(fc -ln -1))'
  8. unalias fuu; alias fuu='su -c "$(fc -ln -1)"'
  9. unalias git-st; alias git-st="git status --short --branch"
  10. #alias git-sel='git checkout $(git branch --format "%(refname:lstrip=2)" | dmenu -i -l 10)'
  11. unalias git-sel; alias git-sel='select branch in $(git branch --format "%(refname:lstrip=2)"); do git checkout $branch; break; done'
  12. unalias git-story; alias git-story="git log --graph --abbrev-commit --decorate --format=format:'%C(cyan)%h%C(reset) %C(green)(%ad)%C(reset) %G?: %s %C(red)- %an %C(reset)%C(yellow)%d%C(reset)'"
  13. unalias j; alias j='jobs -l'
  14. unalias ls; alias ls='ls -hFA'
  15. unalias mv; alias mv='mv -i'
  16. unalias pscpu; alias pscpu='ps -eo pid,user,pcpu,pmem,comm --sort -pcpu | head -20'
  17. unalias psmem; alias psmem='ps -eo pid,user,pcpu,pmem,comm --sort -pmem | head -20'
  18. unalias rm; alias rm='rm -ri'
  19. unalias sleep; alias sleep="/bin/sleep" # avoid sleep as a builtin
  20. unalias xstart; alias xstart='exec startx -- $(tty | sed "s/\/dev\/tty/vt/")'
  21. unalias .; alias .='cd .'
  22. unalias ..; alias ..='cd ..'
  23. unalias ...; alias ...='cd ../..'
  24. unalias zzz; alias zzz='locker & (sleep 1 ; memsys)'
  25. unalias ytcp; alias ytcp='yt-dlp --sub-langs all,-live_chat --embed-subs --embed-thumbnail --add-metadata --embed-chapters --compat-options filename --concurrent-fragments 10'
  26. unalias ytcp-autosubs; alias ytcp-autosubs='yt-dlp --sub-langs all,-live_chat --write-subs --write-auto-subs --embed-subs --embed-thumbnail --add-metadata --embed-chapters --compat-options filename --concurrent-fragments 10'
  27. unalias deposix; alias deposix='unset POSIXLY_CORRECT POSIX_ME_HARDER'
  28. # List all possible readings; Convert all to romaji
  29. unalias kks; alias kks='kakasi -p -Ha -Ka -Ja -Ea -ka -i utf8 -o utf8'
  30. # because the latin alphabet is a mess: https://lojban.org/publications/cll/cll_v1.1_xhtml-chapter-chunks/chapter-phonology.html#section-oddball-orthographies
  31. # Character-based: Plan9(Port) tr(1); GNU sed(1) y-command
  32. # Byte-based: POSIX, GNU, BusyBox, … tr(1); BusyBox sed(1) y-command
  33. unalias lojban_cyrillic; alias lojban_cyrillic='9 tr "abcdefgijklmnoprstuvxyzABCDEFGIJKLMNOPRSTUVXYZ" \
  34. "абшдефгижклмнопрстувхъзАБШДЕФГИЖКЛМНОПРСТУВХЪЗ"'
  35. unalias cyrillic_lojban; alias cyrillic_lojban='9 tr "абшдефгижклмнопрстувхъзАБШДЕФГИЖКЛМНОПРСТУВХЪЗ" \
  36. "abcdefgijklmnoprstuvxyzABCDEFGIJKLMNOPRSTUVXYZ"'
  37. unalias cmd; alias cmd=command
  38. unalias wol-cutievault; alias wol-cutievault="wol 74:d4:35:b0:15:3e"
  39. # = functions =
  40. # Note: POSIX only allows [0-9a-z_A-Z] in function names
  41. get_git_branch() {
  42. if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
  43. case "$branch" in
  44. "HEAD")
  45. branch='detached*'
  46. ;;
  47. "$(hostname)")
  48. branch='@.'
  49. ;;
  50. esac
  51. if git notes list HEAD >/dev/null 2>&1
  52. then
  53. notes="🖉"
  54. else
  55. notes=""
  56. fi
  57. printf -- ' %s%s' "$notes" "$branch"
  58. else
  59. echo ''
  60. fi
  61. unset branch
  62. }
  63. get_working_directory() {
  64. pwd | sed "s|^$HOME|~|"
  65. }
  66. # == prompt setup ==
  67. case $(hostname) in
  68. NightmareMoon) host_ansi="33" ;;
  69. hp2125) host_ansi="37" ;;
  70. *) host_ansi="31" ;;
  71. esac
  72. jobs_pp() {
  73. job_count="$(jobs | wc -l)"
  74. if [[ "${job_count}" != "0" ]]; then
  75. echo -n "${job_count}⏾"
  76. fi
  77. }
  78. precmd() {
  79. # https://codeberg.org/dnkl/foot#jumping-between-prompts
  80. # \e → 
  81. printf ']133;A\\'
  82. }
  83. osc_title() {
  84. printf ']0;%s\\' "$*"
  85. }
  86. # Note: PS1/…/PS4 variables are shell specific and therefore shouldn't be exported
  87. PS4='$ '
  88. case $SHELL in
  89. *ksh*)
  90. # hack derived from ksh88(1), works in mksh, doesn't works in bash
  91. PS1='$(jobs_pp)$?$(date +w%wT%T)${USER}[${host_ansi}m@$(hostname)$(get_working_directory)$(get_git_branch)$(precmd) '
  92. ;;
  93. *bash*)
  94. PS1='\[\e[47;30m\]$(jobs_pp)$?\[\e[37;100m\]\[\e[100;30m\]$(date +w%wT%T)\[\e[40;90m\]\[\e[40;32m\]\u\[\e[${host_ansi}m\]@\h\]\[\e[30;46m\]$(get_working_directory)\[\e[0;36m\]\[\e[0m$(precmd)\] '
  95. ;;
  96. *)
  97. # Don't even try escape codes, and so no powerline-style
  98. PS1='$? $(date +w%wT%T) $USER@${HOSTNAME:=$(hostname)} $(get_working_directory) > '
  99. ;;
  100. esac
  101. # = helpers =
  102. ssh_add_all() {
  103. # Make ssh-add call ${SSH_ASK_PASS} even outside of X11
  104. export DISPLAY="${DISPLAY:-dummy}"
  105. ssh-add $(find ~/.ssh -name 'id_*' -a \( \! -name '*.pub' \))
  106. }
  107. ssh_start_agent() {
  108. code="$(ssh-add -l; echo $?)"
  109. case "${code}" in
  110. 0) echo 'Managed to list keys, SSH agent appear to be active' ;;
  111. 1) echo 'Failed to list keys' ;;
  112. 2)
  113. rm -fr "$SSH_AUTH_SOCK"
  114. eval $(ssh-agent -a "$SSH_AUTH_SOCK")
  115. echo "$SSH_AGENT_PID" >|~/.ssh/agent.pid
  116. return 0
  117. ;;
  118. *) echo 'Unknown return code: '${code} ;;
  119. esac
  120. return 1
  121. }
  122. ssh_tunnel() {
  123. local port=${1:-443}
  124. local server=${2:-hacktivis.me}
  125. local target_port=${3:-22$port}
  126. local target_host=${4:-localhost}
  127. echo "ssh -NL $target_port:$target_host:$port $server"
  128. ssh -NL $target_port:$target_host:$port $server
  129. }
  130. xdg_desktop_menu() {
  131. echo $1 $2 $3
  132. if [$1 = 'install']; then
  133. cp $2 ${XDG_DESKTOP_DIR:-~/Desktop}
  134. fi
  135. }
  136. ssh_socks() {
  137. local server=${1:-hacktivis.me}
  138. local port=${2:-22443}
  139. echo "ssh -ND $port $server"
  140. ssh -ND $port $server
  141. }
  142. pmounts() {
  143. awk 'BEGIN { print "Mountpoint Type Device Options" } { print $2 " " $3 " " $1 " " $4 | "sort"}' /proc/mounts | column -t -s\
  144. }
  145. ucd_grep() {
  146. grep "$1" /usr/share/unicode-data/UnicodeData.txt | awk -F\; '{ system("unicode -t "$1); print FS "U+"$1 FS $2 }' | column -s\; -t
  147. }
  148. ucd_chars() {
  149. unicode -n "$*" | tr 'abcdef' 'ABCDEF' | while read -r char; do ucd_code "$char"; done
  150. }
  151. ucd_code() {
  152. awk -F\; '{ if($1 == "'"${1}"'"){print "U+"$1 FS $2} }' /usr/share/unicode-data/UnicodeData.txt | grep . || echo "$1"
  153. }
  154. log_cmd() {
  155. echo "$(date -u '+%FT%TZ') START $@" >> $HOME/.log_cmd_"$1".log
  156. "$@"
  157. echo "$(date -u '+%FT%TZ') EXITED[$?] $@" >> $HOME/.log_cmd_"$1".log
  158. }
  159. # Wayland wrapper
  160. wstart() {
  161. [ -z "${XDG_RUNTIME_DIR}" ] && ( echo "$0"': Error: XDG_RUNTIME_DIR undefined, exiting…' 1>&2; false )
  162. export GDK_BACKEND=wayland
  163. export QT_QPA_PLATFORM=wayland-egl
  164. export CLUTTER_BACKEND=wayland
  165. export SDL_VIDEODRIVER=wayland
  166. export MY_LOCKER=swaylock
  167. # Default to sway when unspecified
  168. compositor=${1:-sway}
  169. name="$(sed 's/[^a-z]//g' <<<$compositor)"
  170. uptime="$(cut -d' ' -f1 /proc/uptime)"
  171. echo 'wstart_'"$uptime"'_'"$name"'.log' >&2
  172. "${@:-sway}" 2>&1 | tee "${XDG_RUNTIME_DIR}"'/wstart_'"$uptime"'_'"$name"'.log' 2>&1
  173. # Quit the login shell for security
  174. exit
  175. }
  176. mkcd() {
  177. mkdir -p "$1"
  178. cd "$1"
  179. }
  180. h() {
  181. mkcd "${HOME}/tmp/$(date +%Y-%m)"
  182. }
  183. ver_bump() {
  184. if [ $# -ne 2 ]; then
  185. echo "usage: $0 <source ebuild path> <destination ebuild path>"
  186. return
  187. fi
  188. cp -v "${1}" "${2}"
  189. git add "${2}"
  190. ekeyword ~all "${2}"
  191. ebuild "${2}" manifest
  192. git add "${2}/../Manifest"
  193. }
  194. # Unicode-aware variant inspired by https://qntm.org/files/rot13/rot13.html
  195. rot13() {
  196. uconv -f utf-8 -t utf-8 -x '::nfd;' \
  197. | tr \
  198. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
  199. 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
  200. }
  201. gitclone() {
  202. url="$1"
  203. dir=$(echo "$1" | sed -E -e 's;^(https?://|git:);'$HOME'/Sources/git/;' -e 's;.git$;;')
  204. test -e "$dir" || git clone "$1" "$dir"
  205. cd "$dir"
  206. }
  207. # https://team-cymru.com/community-services/ip-asn-mapping/
  208. ip2asn_stdin() {
  209. ( echo begin ; cat - ; echo end ) | nc whois.cymru.com 43 | sort -n
  210. }
  211. ip2asn_arg() {
  212. echo "$@" | nc whois.cymru.com 43
  213. }
  214. if ! command -v finger
  215. then
  216. finger() {
  217. echo "${1//@*}" | dial -e "tcp!${1//*@}!finger"
  218. }
  219. fi
  220. # startup
  221. #stty sane # I can haz urandom
  222. stty -ixon # Disable stop (^S) / start (^Q)
  223. stty -ixoff # Disable sending of start/stop characters
  224. osc_title "$LOGNAME@$(hostname):$(tty)"
  225. h