zshrc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,volname=$mountpoint,no_readahead,noappledouble,nolocalcaches
  76. unset mountroot
  77. unset mountpoint
  78. }
  79. # check number of tmux sessions running on list of ssh hosts
  80. function tmux-num-sessions () {
  81. if (( $# == 0 )) then;
  82. echo usage: tmux-num-sessions ssh-host-1 ssh-host-2 ...
  83. fi
  84. hosts=$@
  85. for i; do
  86. sessions=$(ssh $i tmux ls 2>/dev/null)
  87. if (( $? )) then;
  88. echo $i: No sessions
  89. else
  90. echo $i: $(echo $sessions | wc -l) sessions
  91. fi
  92. done
  93. }
  94. # help creating links to emails
  95. function murl () {
  96. echo message://"%3c"$@"%3e"
  97. }
  98. # make the clipboard working on remote
  99. if [[ -n "$SSH_CLIENT" ]]; then
  100. SSH_IP=$(echo $SSH_CLIENT | awk '{print $1}')
  101. alias pbcopy="ssh $SSH_IP pbcopy"
  102. fi
  103. #
  104. # Aliases
  105. #
  106. # Git
  107. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  108. 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"
  109. alias imgcat="~/.dotfiles/imgcat"
  110. # Utility
  111. alias rm="nocorrect rm"
  112. #
  113. # Python
  114. #
  115. # virtualenvwrapper
  116. if [ -f ~/virtualenvwrapper.sh ]; then
  117. source ~/virtualenvwrapper.sh
  118. export VIRTUAL_ENV_DISABLE_PROMT=yes
  119. fi
  120. if [[ -n "$ITERM_INTEGRATION" && -f ~/.iterm2_shell_integration.zsh ]]; then
  121. test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
  122. fi
  123. # a new attempt to forward ssh sockets to tmux
  124. if [[ -n "$SSH_AUTH_SOCK" ]]; then
  125. # based on/using http://stackoverflow.com/questions/21378569 and
  126. # https://gist.github.com/martijnvermaat/8070533
  127. # Fix SSH auth socket location so agent forwarding works with tmux
  128. if [[ -z "$TMUX" ]] ; then
  129. ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
  130. else
  131. export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
  132. fi
  133. fi
  134. # taskwarrior setup, stolen from https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/taskwarrior/taskwarrior.plugin.zsh
  135. zstyle ':completion:*:*:task:*' verbose yes
  136. zstyle ':completion:*:*:task:*:descriptions' format '%U%B%d%b%u'
  137. zstyle ':completion:*:*:task:*' group-name ''
  138. alias t=task
  139. compdef _task t=task