zshrc 4.4 KB

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