zshrc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ####################################################
  2. # Customized configuration for ZSH based on prezto #
  3. ####################################################
  4. #
  5. # Exit if asked
  6. #
  7. if [[ "${DISABLE_DOTFILES_SYS}" = 1 ]]; then
  8. return
  9. fi
  10. #
  11. # HOMEDIR
  12. #
  13. export HOMEDIR="$(getent passwd "${USER}" | cut -d: -f6)"
  14. #
  15. # TMPDIR
  16. #
  17. export TMPDIR="${HOMEDIR}/tmp"
  18. if [[ ! -d "${TMPDIR}" ]]; then
  19. mkdir -p "${TMPDIR}"
  20. fi
  21. #
  22. # Using zpresto
  23. #
  24. export ZDOTDIR="$(dirname "$(readlink -f "${0}")")"
  25. if [[ -f "${ZDOTDIR}/prezto/init.zsh" ]]; then
  26. source "${ZDOTDIR}/prezto/init.zsh"
  27. zstyle ':completion::complete:*' cache-path "${TMPDIR}/.zcompcache"
  28. # Remove first '\n' in PS1
  29. export PS1="$(echo "${PS1}" | sed ':a;N;$!ba;s/\n//')"
  30. # Add jobs count at the end of the first line
  31. export PS1="$(echo "${PS1}" | sed ':a;N;$!ba;s/\n/%1(j. with %F{161}%j%f job(s).)\n/')"
  32. fi
  33. #
  34. # Load some colors
  35. #
  36. autoload colors zsh/terminfo
  37. if [[ "${terminfo[colors]}" -ge 8 ]]; then
  38. colors
  39. fi
  40. for color in RED GREEN YELLOW BLUE MAGENTA CYAN ORANGE WHITE; do
  41. eval COLOR_$color='$terminfo[bold]$fg[${(L)color}]'
  42. eval COLOR_LIGHT_$color='$fg[${(L)color}]'
  43. done
  44. COLOR_RESET="$terminfo[sgr0]"
  45. #
  46. # Pager
  47. #
  48. VIMPAGER="$(readlink -f "${ZDOTDIR}/../tools/vimpager/vimpager")"
  49. if (( $+commands[vim] )) && [[ -f "${VIMPAGER}" ]]; then
  50. export PAGER="${VIMPAGER}"
  51. alias less="${PAGER}"
  52. alias more="${PAGER}"
  53. export VIMPAGER_RC="$(readlink -f "${ZDOTDIR}/../tools/vim/vimpagerrc")"
  54. if [[ ! -f "${VIMPAGER_RC}" ]]; then
  55. unset VIMPAGER_RC
  56. fi
  57. elif (( $+commands[less] )); then
  58. export PAGER=less
  59. alias more="${PAGER}"
  60. elif (( $+commands[more] )); then
  61. export PAGER=more
  62. fi
  63. export GIT_PAGER="${PAGER}"
  64. unset VIMPAGER
  65. #
  66. # Vim
  67. #
  68. if (( $+commands[vim] )); then
  69. VIMRC="$(readlink -f "${ZDOTDIR}/../tools/vim/vimrc")"
  70. if [[ -f "${VIMRC}" ]]; then
  71. export VIMINIT="source ${VIMRC}"
  72. fi
  73. unset VIMRC
  74. export EDITOR="vim"
  75. alias v='vim'
  76. fi
  77. #
  78. # Git
  79. #
  80. GIT_OPTIONS=()
  81. if [[ -n "${LC_SSH_FULLNAME}" ]]; then
  82. GIT_OPTIONS+=(-c "user.name=${LC_SSH_FULLNAME}")
  83. fi
  84. if [[ -n "${LC_SSH_EMAIL}" ]]; then
  85. GIT_OPTIONS+=(-c "user.email=${LC_SSH_EMAIL}")
  86. fi
  87. export GIT_OPTIONS
  88. export GIT_FAKE_HOME="$(readlink -f "${ZDOTDIR}/../tools/git")"
  89. if [[ -f "${GIT_FAKE_HOME}/.gitconfig" ]]; then
  90. git() {
  91. (
  92. export LC_MESSAGES='en_US.UTF-8'
  93. export HOME="${GIT_FAKE_HOME}"
  94. =git ${GIT_OPTIONS} $@
  95. )
  96. }
  97. alias g='git'
  98. fi
  99. #
  100. # Tmux
  101. #
  102. TMUX_OPTS="-2"
  103. TMUX_CONF="$(readlink -f "${ZDOTDIR}/../tools/tmux/tmux.conf")"
  104. if [[ -f "${TMUX_CONF}" ]]; then
  105. TMUX_OPTS="${TMUX_OPTS} -f ${TMUX_CONF}"
  106. fi
  107. alias tmux="TMPDIR= tmux ${TMUX_OPTS}"
  108. unset TMUX_OPTS TMUX_CONF
  109. #
  110. # Common aliases / functions
  111. #
  112. alias ls='ls -h --color=auto'
  113. alias l='ls -lh --color=auto'
  114. alias rm='rm -i'
  115. alias mv='mv -i'
  116. alias cp='cp -i'
  117. alias mgrep="grep --color=auto -rnisH"
  118. alias rsu="sudo -E zsh"
  119. alias reloadsh="exec zsh"
  120. alias md="mkdir -p"
  121. mcd() { mkdir -p "$@" && cd "$@" }
  122. alias aug='sudo apt-get update && sudo apt-get upgrade'
  123. if (( $+commands[ccze] )) ; then
  124. alias alog='tail -n 300 -F /var/log/auth.log | ccze'
  125. alias mlog='tail -n 300 -F /var/log/mail.log | ccze'
  126. alias slog='tail -n 300 -F /var/log/syslog | ccze'
  127. else
  128. alias alog='tail -n 300 -F /var/log/auth.log'
  129. alias mlog='tail -n 300 -F /var/log/mail.log'
  130. alias slog='tail -n 300 -F /var/log/syslog'
  131. fi
  132. alias sscp_nosec='scp -o '\''StrictHostKeyChecking=no'\'' -o '\''UserKnownHostsFile=/dev/null'\'
  133. alias ssh_nosec='ssh -o '\''StrictHostKeyChecking=no'\'' -o '\''UserKnownHostsFile=/dev/null'\'
  134. #
  135. # History options
  136. #
  137. setopt extendedhistory # add a timestamp and the duration of each command
  138. setopt sharehistory # _all_ zsh sessions share the same history files
  139. setopt histignorealldups # ignores duplications
  140. export HISTSIZE=1000000
  141. export SAVEHIST=1000000
  142. export HISTFILE="${TMPDIR}/.zhistory"
  143. #
  144. # Zsh options
  145. #
  146. export LISTPROMPT # in order to scroll if completion list is too big
  147. setopt clobber # Allows > redirection to truncate existing files, and >> to create files
  148. setopt checkjobs # Report the status of background and suspended jobs before exiting a shell
  149. setopt auto_cd # a command like % /usr/local is equivalent to cd /usr/local
  150. setopt nohup # don't send HUP signal when closing term session
  151. setopt extended_glob # in order to use #, ~ and ^ for filename generation
  152. setopt always_to_end # move to cursor to the end after completion
  153. setopt notify # report the status of backgrounds jobs immediately
  154. setopt correct # try to correct the spelling if possible
  155. setopt noflowcontrol # disable xon/xoff
  156. setopt histnostore # don't store history command in history
  157. setopt nobanghist # don't care of '!' in commands
  158. setopt emacs # zle in emacs mode
  159. setopt pushdignoredups # ignore dups in pushd stack
  160. setopt auto_continue # send SIGCONT to jobs disowned
  161. setopt auto_list # list choice on ambiguous command
  162. setopt auto_pushd # cd = pushd
  163. setopt pushd_ignore_dups # ignore dups in pushd
  164. setopt pushd_silent # don't print stack after push/pop
  165. setopt bang_hist # yeah ! expansion (use bash !$ for example)
  166. setopt chase_links # cd to a symlink is in fact cd to the true dir
  167. setopt ksh_option_print # modify setopt output
  168. setopt long_list_jobs # list jobs in long format
  169. setopt no_rm_star_wait # dont't idle 10 seconds
  170. #
  171. # Zle config
  172. #
  173. # autoescape specials chars with urls
  174. autoload -U url-quote-magic
  175. zle -N self-insert url-quote-magic
  176. # insert sudo at beggining of current command
  177. insert-sudo-prefix () {
  178. local prefix
  179. prefix='sudo'
  180. if [ "${BUFFER:0:${#prefix}}" != "$prefix" ]; then
  181. BUFFER="$prefix $BUFFER"
  182. CURSOR=$(($CURSOR + $#prefix + 1))
  183. fi
  184. }
  185. zle -N insert-sudo-prefix
  186. bindkey "^b" insert-sudo-prefix
  187. # make home and end keys work with tmux
  188. bindkey "^[[H" beginning-of-line
  189. bindkey "^[[F" end-of-line
  190. #
  191. # MOTD
  192. #
  193. if [[ "${LC_SSH_VARS}" = 1 ]]; then
  194. if [[ -z "${TMUX}" ]] && [[ -z "${SUDO_UID}" ]]; then
  195. if [[ -z "${LC_SSH_EMAIL}" || -z "${LC_SSH_FULLNAME}" ]]; then
  196. printf "\n${COLOR_RED}You environment seems to not be properly configured: LC_SSH_EMAIL and LC_SSH_FULLNAME have to be set and forwarded through the ssh connection.${COLOR_RESET}\n"
  197. else
  198. printf "\n${COLOR_LIGHT_GREEN}You are logged in as ${COLOR_YELLOW}${LC_SSH_FULLNAME}${COLOR_RESET} <${COLOR_YELLOW}${LC_SSH_EMAIL}${COLOR_RESET}>\n"
  199. fi
  200. if [[ ! -e "${SSH_AUTH_SOCK}" ]]; then
  201. printf "${COLOR_LIGHT_YELLOW}Be warned that your ssh agent is not forwarded${COLOR_RESET}\n"
  202. fi
  203. fi
  204. fi
  205. #
  206. # NVM
  207. #
  208. export NVM_DIR="/usr/local/nvm"
  209. if [ -s "$NVM_DIR/nvm.sh" ]; then
  210. . "$NVM_DIR/nvm.sh"
  211. fi
  212. #
  213. # Node JS related tools
  214. #
  215. alias npm="HOME=${HOME}/tmp npm"
  216. alias grunt="HOME=${HOME}/tmp grunt"
  217. alias bower="HOME=${HOME}/tmp bower"
  218. alias gulp="HOME=${HOME}/tmp gulp"
  219. alias yarn="HOME=${HOME}/tmp yarn"
  220. # Sourcing ~/.zshrc
  221. ZSHRC="${HOMEDIR}/.zshrc"
  222. if [ -f "${ZSHRC}" ]; then
  223. . "${ZSHRC}"
  224. fi
  225. # Cleanup
  226. unset ZSHRC
  227. unset HOMEDIR