zshrc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. export GIT_AUTHOR_NAME="${LC_SSH_FULLNAME}"
  100. export GIT_COMMITTER_NAME="${GIT_AUTHOR_NAME}"
  101. fi
  102. if [[ -n "${LC_SSH_EMAIL}" ]]; then
  103. GIT_OPTIONS+=(-c "user.email=${LC_SSH_EMAIL}")
  104. export GIT_AUTHOR_EMAIL="${LC_SSH_EMAIL}"
  105. export GIT_COMMITTER_EMAIL="${GIT_AUTHOR_EMAIL}"
  106. fi
  107. export GIT_OPTIONS
  108. git() {
  109. (
  110. export GIT_FAKE_HOME="$(readlink -f "${ZDOTDIR}/../tools/git")"
  111. if [[ -f "${GIT_FAKE_HOME}/.gitconfig" ]]; then
  112. export HOME="${GIT_FAKE_HOME}"
  113. fi
  114. export LC_MESSAGES='en_US.UTF-8'
  115. =git ${GIT_OPTIONS} $@
  116. )
  117. }
  118. alias g='git'
  119. if (( $+commands[lazygit] )); then
  120. alias lg='lazygit'
  121. fi
  122. #
  123. # Tmux
  124. #
  125. TMUX_OPTS="-2"
  126. TMUX_CONF="$(readlink -f "${ZDOTDIR}/../tools/tmux/tmux.conf")"
  127. if [[ -f "${TMUX_CONF}" ]]; then
  128. TMUX_OPTS="${TMUX_OPTS} -f ${TMUX_CONF}"
  129. fi
  130. alias tmux="TMPDIR= tmux ${TMUX_OPTS}"
  131. unset TMUX_OPTS TMUX_CONF
  132. #
  133. # Common aliases / functions
  134. #
  135. alias ls='ls -h --color=auto'
  136. alias l='ls -lh --color=auto'
  137. alias rm='rm -i'
  138. alias mv='mv -i'
  139. alias cp='cp -i'
  140. alias mgrep="grep --color=auto -rnisH"
  141. alias rsu="sudo -E zsh"
  142. alias reloadsh="exec zsh"
  143. alias md="mkdir -p"
  144. mcd() { mkdir -p "$@" && cd "$@" }
  145. alias aug='sudo apt-get update && sudo apt-get upgrade'
  146. if (( $+commands[ccze] )) ; then
  147. alias alog='tail -n 300 -F /var/log/auth.log | ccze'
  148. alias mlog='tail -n 300 -F /var/log/mail.log | ccze'
  149. alias slog='tail -n 300 -F /var/log/syslog | ccze'
  150. else
  151. alias alog='tail -n 300 -F /var/log/auth.log'
  152. alias mlog='tail -n 300 -F /var/log/mail.log'
  153. alias slog='tail -n 300 -F /var/log/syslog'
  154. fi
  155. alias sscp_nosec='scp -o '\''StrictHostKeyChecking=no'\'' -o '\''UserKnownHostsFile=/dev/null'\'
  156. alias ssh_nosec='ssh -o '\''StrictHostKeyChecking=no'\'' -o '\''UserKnownHostsFile=/dev/null'\'
  157. #
  158. # History options
  159. #
  160. setopt extendedhistory # add a timestamp and the duration of each command
  161. setopt sharehistory # _all_ zsh sessions share the same history files
  162. setopt histignorealldups # ignores duplications
  163. export HISTSIZE=1000000
  164. export SAVEHIST=1000000
  165. export HISTFILE="${TMPDIR}/.zhistory"
  166. #
  167. # Zsh options
  168. #
  169. export LISTPROMPT # in order to scroll if completion list is too big
  170. setopt clobber # Allows > redirection to truncate existing files, and >> to create files
  171. setopt checkjobs # Report the status of background and suspended jobs before exiting a shell
  172. setopt auto_cd # a command like % /usr/local is equivalent to cd /usr/local
  173. setopt nohup # don't send HUP signal when closing term session
  174. setopt extended_glob # in order to use #, ~ and ^ for filename generation
  175. setopt always_to_end # move to cursor to the end after completion
  176. setopt notify # report the status of backgrounds jobs immediately
  177. setopt correct # try to correct the spelling if possible
  178. setopt noflowcontrol # disable xon/xoff
  179. setopt histnostore # don't store history command in history
  180. setopt nobanghist # don't care of '!' in commands
  181. setopt emacs # zle in emacs mode
  182. setopt pushdignoredups # ignore dups in pushd stack
  183. setopt auto_continue # send SIGCONT to jobs disowned
  184. setopt auto_list # list choice on ambiguous command
  185. setopt auto_pushd # cd = pushd
  186. setopt pushd_ignore_dups # ignore dups in pushd
  187. setopt pushd_silent # don't print stack after push/pop
  188. setopt bang_hist # yeah ! expansion (use bash !$ for example)
  189. setopt chase_links # cd to a symlink is in fact cd to the true dir
  190. setopt ksh_option_print # modify setopt output
  191. setopt long_list_jobs # list jobs in long format
  192. setopt no_rm_star_wait # dont't idle 10 seconds
  193. #
  194. # Zle config
  195. #
  196. # autoescape specials chars with urls
  197. autoload -U url-quote-magic
  198. zle -N self-insert url-quote-magic
  199. # insert sudo at beggining of current command
  200. insert-sudo-prefix () {
  201. local prefix
  202. prefix='sudo'
  203. if [ "${BUFFER:0:${#prefix}}" != "$prefix" ]; then
  204. BUFFER="$prefix $BUFFER"
  205. CURSOR=$(($CURSOR + $#prefix + 1))
  206. fi
  207. }
  208. zle -N insert-sudo-prefix
  209. bindkey "^b" insert-sudo-prefix
  210. # make home and end keys work with tmux
  211. bindkey "^[[H" beginning-of-line
  212. bindkey "^[[F" end-of-line
  213. #
  214. # MOTD
  215. #
  216. if [[ "${LC_SSH_VARS}" = 1 ]]; then
  217. if [[ -z "${TMUX}" ]] && [[ -z "${SUDO_UID}" ]]; then
  218. if [[ -z "${LC_SSH_EMAIL}" || -z "${LC_SSH_FULLNAME}" ]]; then
  219. 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"
  220. else
  221. 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"
  222. fi
  223. if [[ ! -e "${SSH_AUTH_SOCK}" ]]; then
  224. printf "${COLOR_LIGHT_YELLOW}Be warned that your ssh agent is not forwarded${COLOR_RESET}\n"
  225. fi
  226. fi
  227. fi
  228. #
  229. # NVM
  230. #
  231. export NVM_DIR="/usr/local/nvm"
  232. if [ -s "$NVM_DIR/nvm.sh" ]; then
  233. . "$NVM_DIR/nvm.sh"
  234. fi
  235. #
  236. # Node JS related tools
  237. #
  238. export NPM_CONFIG_USERCONFIG="${XDG_CONFIG_HOME}/npm/config"
  239. export NPM_CONFIG_CACHE="${XDG_CACHE_HOME}/npm"
  240. export NPM_CONFIG_TMP="${XDG_RUNTIME_DIR}/npm"
  241. # yarn is not yet able to use XDG-* directories, workaround:
  242. alias yarn="HOME=${TMPDIR} yarn"
  243. # Sourcing ~/.zshrc
  244. ZSHRC="${HOMEDIR}/.zshrc"
  245. if [ -f "${ZSHRC}" ]; then
  246. . "${ZSHRC}"
  247. fi
  248. # Cleanup
  249. unset ZSHRC
  250. unset HOMEDIR