zshrc 4.4 KB

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