zshrc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. export PATH="$HOME/go/bin:$PATH"
  16. # random string function
  17. random-string()
  18. {
  19. LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w ${1:-32} | head -n 1
  20. }
  21. # make code printable with pandocs
  22. # printable-code filename.ext [forced-extension]
  23. printable-code()
  24. {
  25. body=`cat ${1}`
  26. [[ ${1} =~ "([^.]+).([^.]+)" ]] && name=$match[1] && ext=$match[2]
  27. ext=`test -n "${2}" && echo ${2} || echo $ext`
  28. doc="# ${1}\n\`\`\`$ext\n$body\n\`\`\`"
  29. oformat=`test -n "${3}" && echo ${3} || echo "pdf"`
  30. echo $doc | pandoc -o "$name.$oformat"
  31. }
  32. # mount a remote's ($1) host dir ($2) at $3/$2 or ~/mounts/$2 if $3 is not set
  33. mountremote () {
  34. # set the root mount dir
  35. mountroot="${3:-$HOME/mounts}";
  36. if [ -z "$2" ]; then
  37. mountpoint=$1-home
  38. else
  39. mountpoint=$1-`echo $2 | sed -E "s/\///g"`
  40. fi
  41. mkdir -p $mountroot/$mountpoint
  42. sshfs $1:$2 "$mountroot/$mountpoint" -o auto_cache,reconnect,volname=$mountpoint,no_readahead,noappledouble,nolocalcaches
  43. unset mountroot
  44. unset mountpoint
  45. }
  46. # check number of tmux sessions running on list of ssh hosts
  47. function tmux-num-sessions () {
  48. if (( $# == 0 )) then;
  49. echo usage: tmux-ssh-ls ssh-host-1 ssh-host 2 ...
  50. fi
  51. hosts=$@
  52. for i; do
  53. sessions=$(ssh $i tmux ls 2>/dev/null)
  54. if (( $? )) then;
  55. echo $i: No sessions
  56. else
  57. echo $i: $(echo $sessions | wc -l) sessions
  58. fi
  59. done
  60. }
  61. # make the clipboard working on remote
  62. if [[ -n "$SSH_CLIENT" ]]; then
  63. SSH_IP=$(echo $SSH_CLIENT | awk '{print $1}')
  64. alias pbcopy="ssh $SSH_IP pbcopy"
  65. fi
  66. #
  67. # Aliases
  68. #
  69. # Git
  70. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  71. 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"
  72. alias imgcat="~/.dotfiles/imgcat"
  73. # Utility
  74. alias rm="nocorrect rm"
  75. #
  76. # Python
  77. #
  78. # virtualenvwrapper
  79. if [ -f ~/virtualenvwrapper.sh ]; then
  80. source ~/virtualenvwrapper.sh
  81. export VIRTUAL_ENV_DISABLE_PROMT=yes
  82. fi
  83. if [[ -n "$ITERM_INTEGRATION" && -f ~/.iterm2_shell_integration.zsh ]]; then
  84. test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
  85. fi
  86. # a new attempt to forward ssh sockets to tmux
  87. if [[ -n "$SSH_AUTH_SOCK" ]]; then
  88. # based on/using http://stackoverflow.com/questions/21378569 and
  89. # https://gist.github.com/martijnvermaat/8070533
  90. # Fix SSH auth socket location so agent forwarding works with tmux
  91. if [[ -z "$TMUX" ]] ; then
  92. ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
  93. else
  94. export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
  95. fi
  96. fi