zshrc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Device-specifig setup (ignored by git)
  2. # source local config first to overwrite default theme if wanted
  3. source ~/.zsh_local
  4. # start tmux in a nice way, if available
  5. t () {
  6. if command -v tmux>/dev/null; then
  7. [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && tmux new-session -A -s main
  8. fi
  9. }
  10. # Source Prezto.
  11. if [[ -s "${ZDOTDIR:-$HOME}/.zprezto/init.zsh" ]]; then
  12. source "${ZDOTDIR:-$HOME}/.zprezto/init.zsh"
  13. fi
  14. ### U S E R C O N F I G ###
  15. # Override agnosters prompt_dir
  16. # Using prompt_sorin's abbreviation
  17. prompt_dir() {
  18. local pwd="${PWD/#$HOME/~}"
  19. if [[ "$pwd" == (#m)[/~] ]]; then
  20. prompt="$MATCH"
  21. unset MATCH
  22. else
  23. prompt="${${${${(@j:/:M)${(@s:/:)pwd}##.#?}:h}%/}//\%/%%}/${${pwd:t}//\%/%%}"
  24. fi
  25. prompt_segment blue $PRIMARY_FG " $prompt "
  26. }
  27. # Display current virtual environment
  28. # edit: also fro conda environments
  29. prompt_virtualenv() {
  30. if [[ -n $VIRTUAL_ENV ]]; then
  31. color=cyan
  32. prompt_segment $color $PRIMARY_FG
  33. print -Pn " $(basename $VIRTUAL_ENV) "
  34. elif [[ -n $CONDA_DEFAULT_ENV ]]; then
  35. color=cyan
  36. prompt_segment $color $PRIMARY_FG
  37. print -Pn " $(basename $CONDA_DEFAULT_ENV) "
  38. fi
  39. }
  40. # adjust %m to $HOSTNAME to prevent annoying conda stuff
  41. export HOSTNAME_S=$(hostname -s)
  42. prompt_context() {
  43. local user=`whoami`
  44. if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
  45. prompt_segment $PRIMARY_FG default " %(!.%{%F{yellow}%}.)$user@$HOSTNAME_S "
  46. fi
  47. }
  48. export PATH="$HOME/go/bin:$PATH"
  49. # random string function
  50. random-string()
  51. {
  52. LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w ${1:-32} | head -n 1
  53. }
  54. # make code printable with pandocs
  55. # printable-code filename.ext [forced-extension]
  56. printable-code()
  57. {
  58. body=`cat ${1}`
  59. [[ ${1} =~ "([^.]+).([^.]+)" ]] && name=$match[1] && ext=$match[2]
  60. ext=`test -n "${2}" && echo ${2} || echo $ext`
  61. doc="# ${1}\n\`\`\`$ext\n$body\n\`\`\`"
  62. oformat=`test -n "${3}" && echo ${3} || echo "pdf"`
  63. echo $doc | pandoc -o "$name.$oformat"
  64. }
  65. # mount a remote's ($1) host dir ($2) at $3/$2 or ~/mounts/$2 if $3 is not set
  66. mountremote () {
  67. # set the root mount dir
  68. mountroot="${3:-$HOME/mounts}";
  69. if [ -z "$2" ]; then
  70. mountpoint=$1-home
  71. else
  72. mountpoint=$1-`echo $2 | sed -E "s/\///g"`
  73. fi
  74. mkdir -p $mountroot/$mountpoint
  75. if [[ $(uname -a) == *"Darwin"* ]]; then
  76. # following line is apple-specific
  77. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,volname=$mountpoint,no_readahead,noappledouble,nolocalcaches
  78. else
  79. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,no_readahead
  80. fi
  81. unset mountroot
  82. unset mountpoint
  83. }
  84. # check number of tmux sessions running on list of ssh hosts
  85. function tmux-num-sessions () {
  86. if (( $# == 0 )) then;
  87. echo usage: tmux-num-sessions ssh-host-1 ssh-host-2 ...
  88. fi
  89. hosts=$@
  90. for i; do
  91. sessions=$(ssh $i tmux ls 2>/dev/null)
  92. if (( $? )) then;
  93. echo $i: No sessions
  94. else
  95. echo $i: $(echo $sessions | wc -l) sessions
  96. fi
  97. done
  98. }
  99. # help creating links to emails
  100. function murl () {
  101. echo message://"%3c"$@"%3e"
  102. }
  103. # make the clipboard working on remote
  104. if [[ -n "$SSH_CLIENT" ]]; then
  105. SSH_IP=$(echo $SSH_CLIENT | awk '{print $1}')
  106. alias pbcopy="ssh $SSH_IP pbcopy"
  107. fi
  108. #
  109. # Aliases
  110. #
  111. # Git
  112. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  113. 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"
  114. alias imgcat="~/.dotfiles/imgcat"
  115. # Utility
  116. alias rm="nocorrect rm"
  117. #
  118. # Python
  119. #
  120. # virtualenvwrapper
  121. if [ -f ~/virtualenvwrapper.sh ]; then
  122. source ~/virtualenvwrapper.sh
  123. export VIRTUAL_ENV_DISABLE_PROMT=yes
  124. fi
  125. if [[ -n "$ITERM_INTEGRATION" && -f ~/.iterm2_shell_integration.zsh ]]; then
  126. test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
  127. fi
  128. # a new attempt to forward ssh sockets to tmux
  129. if [[ -n "$SSH_AUTH_SOCK" ]]; then
  130. # based on/using http://stackoverflow.com/questions/21378569 and
  131. # https://gist.github.com/martijnvermaat/8070533
  132. # Fix SSH auth socket location so agent forwarding works with tmux
  133. if [[ -z "$TMUX" ]] ; then
  134. ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
  135. else
  136. export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
  137. fi
  138. fi
  139. # taskwarrior setup, stolen from https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/taskwarrior/taskwarrior.plugin.zsh
  140. zstyle ':completion:*:*:task:*' verbose yes
  141. zstyle ':completion:*:*:task:*:descriptions' format '%U%B%d%b%u'
  142. zstyle ':completion:*:*:task:*' group-name ''
  143. alias t=task
  144. compdef _task t=task