1
0

zshrc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Device-specifig setup (ignored by git)
  2. # source local config first to overwrite default theme if wanted
  3. [[ ! -f ~/.zsh_local ]] || source ~/.zsh_local
  4. ### U S E R C O N F I G ###
  5. # random string function
  6. random-string()
  7. {
  8. LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w ${1:-32} | head -n 1
  9. }
  10. # make code printable with pandocs
  11. # printable-code filename.ext [forced-extension]
  12. printable-code()
  13. {
  14. body=`cat ${1}`
  15. [[ ${1} =~ "([^.]+).([^.]+)" ]] && name=$match[1] && ext=$match[2]
  16. ext=`test -n "${2}" && echo ${2} || echo $ext`
  17. doc="# ${1}\n\`\`\`$ext\n$body\n\`\`\`"
  18. oformat=`test -n "${3}" && echo ${3} || echo "pdf"`
  19. echo $doc | pandoc -o "$name.$oformat"
  20. }
  21. # help creating links to emails
  22. # in apple mail, go to viewing → Show message headers → Custom... → Add "Message-ID"
  23. function murl () {
  24. echo message://"%3c"$@"%3e"
  25. }
  26. #
  27. # Aliases
  28. #
  29. # Git
  30. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  31. alias glola="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"
  32. alias imgcat="~/.dotfiles/imgcat"
  33. # Utility
  34. alias rm="nocorrect rm"
  35. # bitwarden: ensure a valid BW_SESSION, caching it in the macOS keychain.
  36. # reads cached session, validates, and unlocks/logs in only if needed.
  37. bwl() {
  38. local svc="BW_SESSION"
  39. export BW_SESSION="$(security find-generic-password -a "$USER" -s "$svc" -w 2>/dev/null)"
  40. local bw_status
  41. bw_status="$(bw status --session "$BW_SESSION" 2>/dev/null | jq -r '.status')"
  42. case "$bw_status" in
  43. unlocked)
  44. echo "bw session valid" ;;
  45. locked|"")
  46. BW_SESSION="$(bw unlock --raw)" || return 1
  47. security add-generic-password -U -a "$USER" -s "$svc" -w "$BW_SESSION"
  48. export BW_SESSION
  49. echo "bw session unlocked + cached" ;;
  50. unauthenticated)
  51. BW_SESSION="$(bw login --raw)" || return 1
  52. security add-generic-password -U -a "$USER" -s "$svc" -w "$BW_SESSION"
  53. export BW_SESSION
  54. echo "bw session logged in + cached" ;;
  55. esac
  56. }
  57. #
  58. # Python
  59. #
  60. alias pip_update_all="pip install -r <(pip freeze | sed 's|==.*||') -U"
  61. #
  62. # direnv
  63. #
  64. eval "$(direnv hook zsh)"
  65. # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
  66. [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
  67. # load homebrew autocompletions
  68. if [[ -d /opt/homebrew/share/zsh/site-functions ]]; then
  69. fpath=(/opt/homebrew/share/zsh/site-functions $fpath)
  70. fi
  71. # load custom completions
  72. fpath=(~/.zfunc $fpath)
  73. zmodload zsh/complist
  74. # zplug load
  75. if which sheldon > /dev/null; then
  76. eval "$(sheldon source)"
  77. fi
  78. zstyle ':completion:*' menu select
  79. [[ ! -f ~/.zsh_local_completions ]] || source ~/.zsh_local_completions
  80. # Better cp -- must go after compinit and stuff
  81. eval "$(zoxide init zsh)"
  82. # vi mode
  83. bindkey -v
  84. export KEYTIMEOUT=1
  85. # Use vim keys in tab complete menu:
  86. bindkey -M menuselect 'h' vi-backward-char
  87. bindkey -M menuselect 'k' vi-up-line-or-history
  88. bindkey -M menuselect 'l' vi-forward-char
  89. bindkey -M menuselect 'j' vi-down-line-or-history
  90. bindkey -v '^?' backward-delete-char
  91. # Store if light or dark mode is active
  92. if [[ $(defaults read -g AppleInterfaceStyle 2&> /dev/null) == "Dark" ]]
  93. then
  94. UIMODE="dark"
  95. else
  96. UIMODE="light"
  97. fi
  98. export TASKRC=~/.taskrc-${UIMODE}