zshrc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Device-specifig setup (ignored by git)
  2. # source local config first to overwrite default theme if wanted
  3. source ~/.zsh_local
  4. # activate plugin manager
  5. export ZPLUG_HOME=$HOME/.dotfiles/external/zplug
  6. source $ZPLUG_HOME/init.zsh
  7. zplug "romkatv/powerlevel10k", as:theme, use:"*10k.zsh-theme", depth:1
  8. zplug "zsh-users/zsh-syntax-highlighting", defer:2
  9. zplug "zsh-users/zsh-history-substring-search", defer:3
  10. zplug "junegunn/fzf", use:"shell/*.zsh"
  11. zplug "modules/git", from:prezto
  12. # Install plugins if there are plugins that have not been installed
  13. if ! zplug check --verbose; then
  14. printf "Install? [y/N]: "
  15. if read -q; then
  16. echo; zplug install
  17. fi
  18. fi
  19. zplug load
  20. # use up and down keys for substring search
  21. bindkey '^[[A' history-substring-search-up
  22. bindkey '^[[B' history-substring-search-down
  23. ### U S E R C O N F I G ###
  24. zmodload zsh/complist
  25. zstyle ':completion:*' menu select
  26. zstyle ':completion:*' matcher-list '' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' '+l:|?=** r:|?=**'
  27. # vi mode
  28. bindkey -v
  29. export KEYTIMEOUT=1
  30. # Use vim keys in tab complete menu:
  31. bindkey -M menuselect 'h' vi-backward-char
  32. bindkey -M menuselect 'k' vi-up-line-or-history
  33. bindkey -M menuselect 'l' vi-forward-char
  34. bindkey -M menuselect 'j' vi-down-line-or-history
  35. bindkey -v '^?' backward-delete-char
  36. # random string function
  37. random-string()
  38. {
  39. LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w ${1:-32} | head -n 1
  40. }
  41. # make code printable with pandocs
  42. # printable-code filename.ext [forced-extension]
  43. printable-code()
  44. {
  45. body=`cat ${1}`
  46. [[ ${1} =~ "([^.]+).([^.]+)" ]] && name=$match[1] && ext=$match[2]
  47. ext=`test -n "${2}" && echo ${2} || echo $ext`
  48. doc="# ${1}\n\`\`\`$ext\n$body\n\`\`\`"
  49. oformat=`test -n "${3}" && echo ${3} || echo "pdf"`
  50. echo $doc | pandoc -o "$name.$oformat"
  51. }
  52. # mount a remote's ($1) host dir ($2) at $3/$2 or ~/mounts/$2 if $3 is not set
  53. mountremote () {
  54. # set the root mount dir
  55. mountroot="${3:-$HOME/mounts}";
  56. if [ -z "$2" ]; then
  57. mountpoint=$1-home
  58. else
  59. mountpoint=$1-`echo $2 | sed -E "s/\///g"`
  60. fi
  61. mkdir -p $mountroot/$mountpoint
  62. if [[ $(uname -a) == *"Darwin"* ]]; then
  63. # following line is apple-specific
  64. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,volname=$mountpoint,no_readahead,noappledouble,nolocalcaches
  65. else
  66. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,no_readahead
  67. fi
  68. unset mountroot
  69. unset mountpoint
  70. }
  71. # check number of tmux sessions running on list of ssh hosts
  72. function tmux-num-sessions () {
  73. if (( $# == 0 )) then;
  74. echo usage: tmux-num-sessions ssh-host-1 ssh-host-2 ...
  75. fi
  76. hosts=$@
  77. for i; do
  78. sessions=$(ssh $i tmux ls 2>/dev/null)
  79. if (( $? )) then;
  80. echo $i: No sessions
  81. else
  82. echo $i: $(echo $sessions | wc -l) sessions
  83. fi
  84. done
  85. }
  86. # help creating links to emails
  87. # in apple mail, go to viewing → Show message headers → Custom... → Add "Message-ID"
  88. function murl () {
  89. echo message://"%3c"$@"%3e"
  90. }
  91. # make the clipboard working on remote
  92. if [[ -n "$SSH_CLIENT" ]]; then
  93. SSH_IP=$(echo $SSH_CLIENT | awk '{print $1}')
  94. alias pbcopy="ssh $SSH_IP pbcopy"
  95. fi
  96. #
  97. # Aliases
  98. #
  99. # Git
  100. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  101. 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"
  102. alias imgcat="~/.dotfiles/imgcat"
  103. # Utility
  104. alias rm="nocorrect rm"
  105. #
  106. # Python
  107. #
  108. # virtualenvwrapper
  109. if [ -f ~/virtualenvwrapper.sh ]; then
  110. source ~/virtualenvwrapper.sh
  111. export VIRTUAL_ENV_DISABLE_PROMT=yes
  112. fi
  113. if [[ -n "$ITERM_INTEGRATION" && -f ~/.iterm2_shell_integration.zsh ]]; then
  114. test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
  115. fi
  116. # a new attempt to forward ssh sockets to tmux
  117. if [[ -n "$SSH_AUTH_SOCK" ]]; then
  118. # based on/using http://stackoverflow.com/questions/21378569 and
  119. # https://gist.github.com/martijnvermaat/8070533
  120. # Fix SSH auth socket location so agent forwarding works with tmux
  121. if [[ -z "$TMUX" ]] ; then
  122. ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
  123. else
  124. export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
  125. fi
  126. fi
  127. # taskwarrior setup, stolen from https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/taskwarrior/taskwarrior.plugin.zsh
  128. zstyle ':completion:*:*:task:*' verbose yes
  129. zstyle ':completion:*:*:task:*:descriptions' format '%U%B%d%b%u'
  130. zstyle ':completion:*:*:task:*' group-name ''
  131. alias t=task
  132. compdef _task t=task
  133. # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
  134. [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh