官网:https://ohmyz.sh

安装

使用官方提供的脚本安装:

  1. $ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

因为国内网络问题,有可能下载不了安装脚本,我把脚本内容放在下面了:

  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 -n 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 "$@"

手动把上面脚本保存到 shell 脚本文件并执行。随后根据提示把 zsh 设置为默认 shell 即可:
image.png

安装插件

~/.zshrc中找到 plucin=(git),这里配置启用的插件,格式如下:plugin=(插件1 插件2 插件3),修改之后用命令重载配置:source ~/.zshrc,插件的安装路径:~/.oh-my-zsh/plugins

zsh-autosuggestions

官网,非常好用的一个插件,会记录你之前输入过的所有命令,并且自动匹配你可能想要输入命令,然后按→补全,安装命令:git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

zsh-syntax-highlighting

官网,命令太多,有时候记不住,等输入完了才知道命令输错了,这个插件直接在输入过程中就会提示你,当前命令是否正确,错误红色,正确绿色,安装命令:git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

修改主题

官方主题库:https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
修改主题方法:编辑 ~/.zshrc 文件的 ZSH_THEME="xxx",重启终端即可生效。
推荐自带主题af-magic
2022-07-02.201744.png