zshrc 4.9 KB

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