oh-my-zsh.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # If ZSH is not defined, use the current script's directory.
  2. [[ -z "$ZSH" ]] && export ZSH="${${(%):-%x}:a:h}"
  3. # Set ZSH_CACHE_DIR to the path where cache files should be created
  4. # or else we will use the default cache/
  5. if [[ -z "$ZSH_CACHE_DIR" ]]; then
  6. ZSH_CACHE_DIR="$ZSH/cache"
  7. fi
  8. # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
  9. if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
  10. ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
  11. fi
  12. # Create cache and completions dir and add to $fpath
  13. mkdir -p "$ZSH_CACHE_DIR/completions"
  14. (( ${fpath[(Ie)$ZSH_CACHE_DIR/completions]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  15. # Check for updates on initial load...
  16. if [ "$DISABLE_AUTO_UPDATE" != "true" ]; then
  17. source $ZSH/tools/check_for_upgrade.sh
  18. fi
  19. # Initializes Oh My Zsh
  20. # add a function path
  21. fpath=($ZSH/functions $ZSH/completions $fpath)
  22. # Load all stock functions (from $fpath files) called below.
  23. autoload -U compaudit compinit
  24. # Set ZSH_CUSTOM to the path where your custom config files
  25. # and plugins exists, or else we will use the default custom/
  26. if [[ -z "$ZSH_CUSTOM" ]]; then
  27. ZSH_CUSTOM="$ZSH/custom"
  28. fi
  29. is_plugin() {
  30. local base_dir=$1
  31. local name=$2
  32. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  33. || builtin test -f $base_dir/plugins/$name/_$name
  34. }
  35. # Add all defined plugins to fpath. This must be done
  36. # before running compinit.
  37. for plugin ($plugins); do
  38. if is_plugin $ZSH_CUSTOM $plugin; then
  39. fpath=($ZSH_CUSTOM/plugins/$plugin $fpath)
  40. elif is_plugin $ZSH $plugin; then
  41. fpath=($ZSH/plugins/$plugin $fpath)
  42. else
  43. echo "[oh-my-zsh] plugin '$plugin' not found"
  44. fi
  45. done
  46. # Figure out the SHORT hostname
  47. if [[ "$OSTYPE" = darwin* ]]; then
  48. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  49. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST=${HOST/.*/}
  50. else
  51. SHORT_HOST=${HOST/.*/}
  52. fi
  53. # Save the location of the current completion dump file.
  54. if [ -z "$ZSH_COMPDUMP" ]; then
  55. ZSH_COMPDUMP="${ZDOTDIR:-${HOME}}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  56. fi
  57. # Construct zcompdump OMZ metadata
  58. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  59. zcompdump_fpath="#omz fpath: $fpath"
  60. # Delete the zcompdump file if OMZ zcompdump metadata changed
  61. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  62. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  63. command rm -f "$ZSH_COMPDUMP"
  64. zcompdump_refresh=1
  65. fi
  66. if [[ $ZSH_DISABLE_COMPFIX != true ]]; then
  67. source $ZSH/lib/compfix.zsh
  68. # If completion insecurities exist, warn the user
  69. handle_completion_insecurities
  70. # Load only from secure directories
  71. compinit -i -C -d "${ZSH_COMPDUMP}"
  72. else
  73. # If the user wants it, load from all found directories
  74. compinit -u -C -d "${ZSH_COMPDUMP}"
  75. fi
  76. # Append zcompdump metadata if missing
  77. if (( $zcompdump_refresh )); then
  78. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  79. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  80. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  81. $zcompdump_revision
  82. $zcompdump_fpath
  83. EOF
  84. fi
  85. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  86. # Load all of the config files in ~/oh-my-zsh that end in .zsh
  87. # TIP: Add files you don't want in git to .gitignore
  88. for config_file ($ZSH/lib/*.zsh); do
  89. custom_config_file="${ZSH_CUSTOM}/lib/${config_file:t}"
  90. [ -f "${custom_config_file}" ] && config_file=${custom_config_file}
  91. source $config_file
  92. done
  93. # Load all of the plugins that were defined in ~/.zshrc
  94. for plugin ($plugins); do
  95. if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
  96. source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
  97. elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
  98. source $ZSH/plugins/$plugin/$plugin.plugin.zsh
  99. fi
  100. done
  101. # Load all of your custom configurations from custom/
  102. for config_file ($ZSH_CUSTOM/*.zsh(N)); do
  103. source $config_file
  104. done
  105. unset config_file
  106. # Load the theme
  107. if [ ! "$ZSH_THEME" = "" ]; then
  108. if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then
  109. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  110. elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then
  111. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  112. else
  113. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  114. fi
  115. fi