install.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #!/bin/sh
  2. #
  3. # This script should be run via curl:
  4. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  5. # or via wget:
  6. # sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  7. # or via fetch:
  8. # sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  9. #
  10. # As an alternative, you can first download the install script and run it afterwards:
  11. # wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
  12. # sh install.sh
  13. #
  14. # You can tweak the install behavior by setting variables when running the script. For
  15. # example, to change the path to the Oh My Zsh repository:
  16. # ZSH=~/.zsh sh install.sh
  17. #
  18. # Respects the following environment variables:
  19. # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
  20. # REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
  21. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  22. # BRANCH - branch to check out immediately after install (default: master)
  23. #
  24. # Other options:
  25. # CHSH - 'no' means the installer will not change the default shell (default: yes)
  26. # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
  27. # KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
  28. #
  29. # You can also pass some arguments to the install script to set some these options:
  30. # --skip-chsh: has the same behavior as setting CHSH to 'no'
  31. # --unattended: sets both CHSH and RUNZSH to 'no'
  32. # --keep-zshrc: sets KEEP_ZSHRC to 'yes'
  33. # For example:
  34. # sh install.sh --unattended
  35. # or:
  36. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
  37. #
  38. set -e
  39. # Track if $ZSH was provided
  40. custom_zsh=${ZSH:+yes}
  41. # Default settings
  42. ZSH=${ZSH:-~/.oh-my-zsh}
  43. REPO=${REPO:-ohmyzsh/ohmyzsh}
  44. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  45. BRANCH=${BRANCH:-master}
  46. # Other options
  47. CHSH=${CHSH:-yes}
  48. RUNZSH=${RUNZSH:-yes}
  49. KEEP_ZSHRC=${KEEP_ZSHRC:-no}
  50. command_exists() {
  51. command -v "$@" >/dev/null 2>&1
  52. }
  53. # The [ -t 1 ] check only works when the function is not called from
  54. # a subshell (like in `$(...)` or `(...)`, so this hack redefines the
  55. # function at the top level to always return false when stdout is not
  56. # a tty.
  57. if [ -t 1 ]; then
  58. is_tty() {
  59. true
  60. }
  61. else
  62. is_tty() {
  63. false
  64. }
  65. fi
  66. # This function uses the logic from supports-hyperlinks[1][2], which is
  67. # made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
  68. # [1] https://github.com/zkat/supports-hyperlinks
  69. # [2] https://crates.io/crates/supports-hyperlinks
  70. #
  71. # Copyright (c) 2021 Kat Marchán
  72. #
  73. # Licensed under the Apache License, Version 2.0 (the "License");
  74. # you may not use this file except in compliance with the License.
  75. # You may obtain a copy of the License at
  76. #
  77. # http://www.apache.org/licenses/LICENSE-2.0
  78. #
  79. # Unless required by applicable law or agreed to in writing, software
  80. # distributed under the License is distributed on an "AS IS" BASIS,
  81. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  82. # See the License for the specific language governing permissions and
  83. # limitations under the License.
  84. supports_hyperlinks() {
  85. # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
  86. if [ -n "$FORCE_HYPERLINK" ]; then
  87. [ "$FORCE_HYPERLINK" != 0 ]
  88. return $?
  89. fi
  90. # If stdout is not a tty, it doesn't support hyperlinks
  91. is_tty || return 1
  92. # DomTerm terminal emulator (domterm.org)
  93. if [ -n "$DOMTERM" ]; then
  94. return 0
  95. fi
  96. # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
  97. if [ -n "$VTE_VERSION" ]; then
  98. [ $VTE_VERSION -ge 5000 ]
  99. return $?
  100. fi
  101. # If $TERM_PROGRAM is set, these terminals support hyperlinks
  102. case "$TERM_PROGRAM" in
  103. Hyper|iTerm.app|terminology|WezTerm) return 0 ;;
  104. esac
  105. # kitty supports hyperlinks
  106. if [ "$TERM" = xterm-kitty ]; then
  107. return 0
  108. fi
  109. # Windows Terminal or Konsole also support hyperlinks
  110. if [ -n "$WT_SESSION" ] || [ -n "$KONSOLE_VERSION" ]; then
  111. return 0
  112. fi
  113. return 1
  114. }
  115. fmt_link() {
  116. # $1: text, $2: url, $3: fallback mode
  117. if supports_hyperlinks; then
  118. printf '\033]8;;%s\a%s\033]8;;\a\n' "$2" "$1"
  119. return
  120. fi
  121. case "$3" in
  122. --text) printf '%s\n' "$1" ;;
  123. --url|*) fmt_underline "$2" ;;
  124. esac
  125. }
  126. fmt_underline() {
  127. is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
  128. }
  129. # shellcheck disable=SC2016 # backtick in single-quote
  130. fmt_code() {
  131. is_tty && printf '`\033[2m%s\033[22m`\n' "$*" || printf '`%s`\n' "$*"
  132. }
  133. fmt_error() {
  134. printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2
  135. }
  136. setup_color() {
  137. # Only use colors if connected to a terminal
  138. if is_tty; then
  139. RAINBOW="
  140. $(printf '\033[38;5;196m')
  141. $(printf '\033[38;5;202m')
  142. $(printf '\033[38;5;226m')
  143. $(printf '\033[38;5;082m')
  144. $(printf '\033[38;5;021m')
  145. $(printf '\033[38;5;093m')
  146. $(printf '\033[38;5;163m')
  147. "
  148. RED=$(printf '\033[31m')
  149. GREEN=$(printf '\033[32m')
  150. YELLOW=$(printf '\033[33m')
  151. BLUE=$(printf '\033[34m')
  152. BOLD=$(printf '\033[1m')
  153. RESET=$(printf '\033[m')
  154. else
  155. RAINBOW=""
  156. RED=""
  157. GREEN=""
  158. YELLOW=""
  159. BLUE=""
  160. BOLD=""
  161. RESET=""
  162. fi
  163. }
  164. setup_ohmyzsh() {
  165. # Prevent the cloned repository from having insecure permissions. Failing to do
  166. # so causes compinit() calls to fail with "command not found: compdef" errors
  167. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  168. # that this will be ignored under Cygwin by default, as Windows ACLs take
  169. # precedence over umasks except for filesystems mounted with option "noacl".
  170. umask g-w,o-w
  171. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  172. command_exists git || {
  173. fmt_error "git is not installed"
  174. exit 1
  175. }
  176. ostype=$(uname)
  177. if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then
  178. fmt_error "Windows/MSYS Git is not supported on Cygwin"
  179. fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  180. exit 1
  181. fi
  182. git clone -c core.eol=lf -c core.autocrlf=false \
  183. -c fsck.zeroPaddedFilemode=ignore \
  184. -c fetch.fsck.zeroPaddedFilemode=ignore \
  185. -c receive.fsck.zeroPaddedFilemode=ignore \
  186. -c oh-my-zsh.remote=origin \
  187. -c oh-my-zsh.branch="$BRANCH" \
  188. --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  189. fmt_error "git clone of oh-my-zsh repo failed"
  190. exit 1
  191. }
  192. echo
  193. }
  194. setup_zshrc() {
  195. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  196. # with datestamp of installation that moved them aside, so we never actually
  197. # destroy a user's original zshrc
  198. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  199. # Must use this exact name so uninstall.sh can find it
  200. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  201. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  202. # Skip this if the user doesn't want to replace an existing .zshrc
  203. if [ "$KEEP_ZSHRC" = yes ]; then
  204. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
  205. return
  206. fi
  207. if [ -e "$OLD_ZSHRC" ]; then
  208. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  209. if [ -e "$OLD_OLD_ZSHRC" ]; then
  210. fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  211. fmt_error "re-run the installer again in a couple of seconds"
  212. exit 1
  213. fi
  214. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  215. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  216. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  217. fi
  218. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  219. mv ~/.zshrc "$OLD_ZSHRC"
  220. fi
  221. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  222. sed "/^export ZSH=/ c\\
  223. export ZSH=\"$ZSH\"
  224. " "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
  225. mv -f ~/.zshrc-omztemp ~/.zshrc
  226. echo
  227. }
  228. setup_shell() {
  229. # Skip setup if the user wants or stdin is closed (not running interactively).
  230. if [ "$CHSH" = no ]; then
  231. return
  232. fi
  233. # If this user's login shell is already "zsh", do not attempt to switch.
  234. if [ "$(basename -- "$SHELL")" = "zsh" ]; then
  235. return
  236. fi
  237. # If this platform doesn't provide a "chsh" command, bail out.
  238. if ! command_exists chsh; then
  239. cat <<EOF
  240. I can't change your shell automatically because this system does not have chsh.
  241. ${BLUE}Please manually change your default shell to zsh${RESET}
  242. EOF
  243. return
  244. fi
  245. echo "${BLUE}Time to change your default shell to zsh:${RESET}"
  246. # Prompt for user choice on changing the default login shell
  247. printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \
  248. "$YELLOW" "$RESET"
  249. read -r opt
  250. case $opt in
  251. y*|Y*|"") echo "Changing the shell..." ;;
  252. n*|N*) echo "Shell change skipped."; return ;;
  253. *) echo "Invalid choice. Shell change skipped."; return ;;
  254. esac
  255. # Check if we're running on Termux
  256. case "$PREFIX" in
  257. *com.termux*) termux=true; zsh=zsh ;;
  258. *) termux=false ;;
  259. esac
  260. if [ "$termux" != true ]; then
  261. # Test for the right location of the "shells" file
  262. if [ -f /etc/shells ]; then
  263. shells_file=/etc/shells
  264. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  265. shells_file=/usr/share/defaults/etc/shells
  266. else
  267. fmt_error "could not find /etc/shells file. Change your default shell manually."
  268. return
  269. fi
  270. # Get the path to the right zsh binary
  271. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  272. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  273. if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then
  274. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  275. fmt_error "no zsh binary found or not present in '$shells_file'"
  276. fmt_error "change your default shell manually."
  277. return
  278. fi
  279. fi
  280. fi
  281. # We're going to change the default shell, so back up the current one
  282. if [ -n "$SHELL" ]; then
  283. echo "$SHELL" > ~/.shell.pre-oh-my-zsh
  284. else
  285. grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  286. fi
  287. # Actually change the default shell to zsh
  288. if ! chsh -s "$zsh"; then
  289. fmt_error "chsh command unsuccessful. Change your default shell manually."
  290. else
  291. export SHELL="$zsh"
  292. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  293. fi
  294. echo
  295. }
  296. # shellcheck disable=SC2183 # printf string has more %s than arguments ($RAINBOW expands to multiple arguments)
  297. print_success() {
  298. printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET
  299. printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET
  300. printf '%s / __ \%s/ __ \ %s / __ `__ \%s/ / / / %s /_ / %s/ ___/%s __ \ %s\n' $RAINBOW $RESET
  301. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET
  302. printf '%s\____/%s_/ /_/ %s /_/ /_/ /_/%s\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET
  303. printf '%s %s %s %s /____/ %s %s %s %s....is now installed!%s\n' $RAINBOW $GREEN $RESET
  304. printf '\n'
  305. printf '\n'
  306. printf "%s %s %s\n" "Before you scream ${BOLD}${YELLOW}Oh My Zsh!${RESET} look over the" \
  307. "$(fmt_code "$(fmt_link ".zshrc" "file://$HOME/.zshrc" --text)")" \
  308. "file to select plugins, themes, and options."
  309. printf '\n'
  310. printf '%s\n' "• Follow us on Twitter: $(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)"
  311. printf '%s\n' "• Join our Discord community: $(fmt_link "Discord server" https://discord.gg/ohmyzsh)"
  312. printf '%s\n' "• Get stickers, t-shirts, coffee mugs and more: $(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)"
  313. printf '%s\n' $RESET
  314. }
  315. main() {
  316. # Run as unattended if stdin is not a tty
  317. if [ ! -t 0 ]; then
  318. RUNZSH=no
  319. CHSH=no
  320. fi
  321. # Parse arguments
  322. while [ $# -gt 0 ]; do
  323. case $1 in
  324. --unattended) RUNZSH=no; CHSH=no ;;
  325. --skip-chsh) CHSH=no ;;
  326. --keep-zshrc) KEEP_ZSHRC=yes ;;
  327. esac
  328. shift
  329. done
  330. setup_color
  331. if ! command_exists zsh; then
  332. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  333. exit 1
  334. fi
  335. if [ -d "$ZSH" ]; then
  336. echo "${YELLOW}The \$ZSH folder already exists ($ZSH).${RESET}"
  337. if [ "$custom_zsh" = yes ]; then
  338. cat <<EOF
  339. You ran the installer with the \$ZSH setting or the \$ZSH variable is
  340. exported. You have 3 options:
  341. 1. Unset the ZSH variable when calling the installer:
  342. $(fmt_code "ZSH= sh install.sh")
  343. 2. Install Oh My Zsh to a directory that doesn't exist yet:
  344. $(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")
  345. 3. (Caution) If the folder doesn't contain important information,
  346. you can just remove it with $(fmt_code "rm -r $ZSH")
  347. EOF
  348. else
  349. echo "You'll need to remove it if you want to reinstall."
  350. fi
  351. exit 1
  352. fi
  353. setup_ohmyzsh
  354. setup_zshrc
  355. setup_shell
  356. print_success
  357. if [ $RUNZSH = no ]; then
  358. echo "${YELLOW}Run zsh to try it out.${RESET}"
  359. exit
  360. fi
  361. exec zsh -l
  362. }
  363. main "$@"