zshrc 7.7 KB

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