zshrc 8.2 KB

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