关于zsh的优点,请翻阅该链接。
为什么说zsh是shell中的极品?知乎

安装

安装zsh

  1. $ sudo apt-get install zsh

安装 oh my zsh 插件

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

但是由于GIT的缘故,可能会出现下载失败。
或者是直接到官网中提供的git仓库里下载。
oh my zsh仓库
文章末尾附带了安装插件的脚本,直接复制到系统中,新建一个install.sh,粘贴进去后运行即可。

配置

oh my zsh的配置文件在 ~/.zshrc 文件里。

插件

zsh-syntax-highlighting

语法高亮插件。当输入的指令是正确的时,输入的内容会变成绿色,否则显示红色。
image.png
image.png
执行以下指令下载安装。

  1. git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

下载成功后,打开配置文件。

  1. $ sudo vim ~/.zshrc

找到 plugins=,这里都是生成的插件配置,在括号里添加 zsh-syntax-highlighting
保存退出,source后生效。

  1. $source ~/.zshrc

zsh-autosuggestions

插件可以记录历史输入过的指令,然后给出建议。
image.png
执行以下指令下载安装。

  1. git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

下载成功后,打开配置文件。

  1. $ sudo vim ~/.zshrc

找到 plugins=,这里都是生成的插件配置,在括号里添加 zsh-autosuggestions
保存退出,source后生效。

  1. $source ~/.zshrc

主题

主题的字段为

  1. # ZSH_THEME="robbyrussell" /* 默认主题 */
  2. # ZSH_THEME="agnoster"
  3. ZSH_THEME="ys"

如上所示,使用的主题为 robbyrussell,修改引号内的内容,指定相应的主题的名称,即可修改zsh的主题。只能有一个主题,不用的主题需注释掉。
保存退出,source后生效。

  1. $source ~/.zshrc

install.sh

  1. #!/bin/sh
  2. # This script should be run via curl:
  3. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  4. # or wget:
  5. # sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  6. #
  7. # As an alternative, you can first download the install script and run it afterwards:
  8. # wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
  9. # sh install.sh
  10. #
  11. # You can tweak the install behavior by setting variables when running the script. For
  12. # example, to change the path to the Oh My Zsh repository:
  13. # ZSH=~/.zsh sh install.sh
  14. #
  15. # Respects the following environment variables:
  16. # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
  17. # REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
  18. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  19. # BRANCH - branch to check out immediately after install (default: master)
  20. #
  21. # Other options:
  22. # CHSH - 'no' means the installer will not change the default shell (default: yes)
  23. # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
  24. # KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
  25. #
  26. # You can also pass some arguments to the install script to set some these options:
  27. # --skip-chsh: has the same behavior as setting CHSH to 'no'
  28. # --unattended: sets both CHSH and RUNZSH to 'no'
  29. # --keep-zshrc: sets KEEP_ZSHRC to 'yes'
  30. # For example:
  31. # sh install.sh --unattended
  32. #
  33. set -e
  34. # Default settings
  35. ZSH=${ZSH:-~/.oh-my-zsh}
  36. REPO=${REPO:-ohmyzsh/ohmyzsh}
  37. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  38. BRANCH=${BRANCH:-master}
  39. # Other options
  40. CHSH=${CHSH:-yes}
  41. RUNZSH=${RUNZSH:-yes}
  42. KEEP_ZSHRC=${KEEP_ZSHRC:-no}
  43. command_exists() {
  44. command -v "$@" >/dev/null 2>&1
  45. }
  46. error() {
  47. echo ${RED}"Error: $@"${RESET} >&2
  48. }
  49. setup_color() {
  50. # Only use colors if connected to a terminal
  51. if [ -t 1 ]; then
  52. RED=$(printf '\033[31m')
  53. GREEN=$(printf '\033[32m')
  54. YELLOW=$(printf '\033[33m')
  55. BLUE=$(printf '\033[34m')
  56. BOLD=$(printf '\033[1m')
  57. RESET=$(printf '\033[m')
  58. else
  59. RED=""
  60. GREEN=""
  61. YELLOW=""
  62. BLUE=""
  63. BOLD=""
  64. RESET=""
  65. fi
  66. }
  67. setup_ohmyzsh() {
  68. # Prevent the cloned repository from having insecure permissions. Failing to do
  69. # so causes compinit() calls to fail with "command not found: compdef" errors
  70. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  71. # that this will be ignored under Cygwin by default, as Windows ACLs take
  72. # precedence over umasks except for filesystems mounted with option "noacl".
  73. umask g-w,o-w
  74. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  75. command_exists git || {
  76. error "git is not installed"
  77. exit 1
  78. }
  79. if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
  80. error "Windows/MSYS Git is not supported on Cygwin"
  81. error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  82. exit 1
  83. fi
  84. git clone -c core.eol=lf -c core.autocrlf=false \
  85. -c fsck.zeroPaddedFilemode=ignore \
  86. -c fetch.fsck.zeroPaddedFilemode=ignore \
  87. -c receive.fsck.zeroPaddedFilemode=ignore \
  88. --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  89. error "git clone of oh-my-zsh repo failed"
  90. exit 1
  91. }
  92. echo
  93. }
  94. setup_zshrc() {
  95. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  96. # with datestamp of installation that moved them aside, so we never actually
  97. # destroy a user's original zshrc
  98. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  99. # Must use this exact name so uninstall.sh can find it
  100. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  101. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  102. # Skip this if the user doesn't want to replace an existing .zshrc
  103. if [ $KEEP_ZSHRC = yes ]; then
  104. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
  105. return
  106. fi
  107. if [ -e "$OLD_ZSHRC" ]; then
  108. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  109. if [ -e "$OLD_OLD_ZSHRC" ]; then
  110. error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  111. error "re-run the installer again in a couple of seconds"
  112. exit 1
  113. fi
  114. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  115. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  116. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  117. fi
  118. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  119. mv ~/.zshrc "$OLD_ZSHRC"
  120. fi
  121. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  122. sed "/^export ZSH=/ c\\
  123. export ZSH=\"$ZSH\"
  124. " "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
  125. mv -f ~/.zshrc-omztemp ~/.zshrc
  126. echo
  127. }
  128. setup_shell() {
  129. # Skip setup if the user wants or stdin is closed (not running interactively).
  130. if [ $CHSH = no ]; then
  131. return
  132. fi
  133. # If this user's login shell is already "zsh", do not attempt to switch.
  134. if [ "$(basename "$SHELL")" = "zsh" ]; then
  135. return
  136. fi
  137. # If this platform doesn't provide a "chsh" command, bail out.
  138. if ! command_exists chsh; then
  139. cat <<-EOF
  140. I can't change your shell automatically because this system does not have chsh.
  141. ${BLUE}Please manually change your default shell to zsh${RESET}
  142. EOF
  143. return
  144. fi
  145. echo "${BLUE}Time to change your default shell to zsh:${RESET}"
  146. # Prompt for user choice on changing the default login shell
  147. printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
  148. read opt
  149. case $opt in
  150. y*|Y*|"") echo "Changing the shell..." ;;
  151. n*|N*) echo "Shell change skipped."; return ;;
  152. *) echo "Invalid choice. Shell change skipped."; return ;;
  153. esac
  154. # Check if we're running on Termux
  155. case "$PREFIX" in
  156. *com.termux*) termux=true; zsh=zsh ;;
  157. *) termux=false ;;
  158. esac
  159. if [ "$termux" != true ]; then
  160. # Test for the right location of the "shells" file
  161. if [ -f /etc/shells ]; then
  162. shells_file=/etc/shells
  163. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  164. shells_file=/usr/share/defaults/etc/shells
  165. else
  166. error "could not find /etc/shells file. Change your default shell manually."
  167. return
  168. fi
  169. # Get the path to the right zsh binary
  170. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  171. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  172. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  173. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  174. error "no zsh binary found or not present in '$shells_file'"
  175. error "change your default shell manually."
  176. return
  177. fi
  178. fi
  179. fi
  180. # We're going to change the default shell, so back up the current one
  181. if [ -n "$SHELL" ]; then
  182. echo $SHELL > ~/.shell.pre-oh-my-zsh
  183. else
  184. grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  185. fi
  186. # Actually change the default shell to zsh
  187. if ! chsh -s "$zsh"; then
  188. error "chsh command unsuccessful. Change your default shell manually."
  189. else
  190. export SHELL="$zsh"
  191. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  192. fi
  193. echo
  194. }
  195. main() {
  196. # Run as unattended if stdin is closed
  197. if [ ! -t 0 ]; then
  198. RUNZSH=no
  199. CHSH=no
  200. fi
  201. # Parse arguments
  202. while [ $# -gt 0 ]; do
  203. case $1 in
  204. --unattended) RUNZSH=no; CHSH=no ;;
  205. --skip-chsh) CHSH=no ;;
  206. --keep-zshrc) KEEP_ZSHRC=yes ;;
  207. esac
  208. shift
  209. done
  210. setup_color
  211. if ! command_exists zsh; then
  212. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  213. exit 1
  214. fi
  215. if [ -d "$ZSH" ]; then
  216. cat <<-EOF
  217. ${YELLOW}You already have Oh My Zsh installed.${RESET}
  218. You'll need to remove '$ZSH' if you want to reinstall.
  219. EOF
  220. exit 1
  221. fi
  222. setup_ohmyzsh
  223. setup_zshrc
  224. setup_shell
  225. printf "$GREEN"
  226. cat <<-'EOF'
  227. __ __
  228. ____ / /_ ____ ___ __ __ ____ _____/ /_
  229. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  230. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  231. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  232. /____/ ....is now installed!
  233. Please look over the ~/.zshrc file to select plugins, themes, and options.
  234. p.s. Follow us on https://twitter.com/ohmyzsh
  235. p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
  236. EOF
  237. printf "$RESET"
  238. if [ $RUNZSH = no ]; then
  239. echo "${YELLOW}Run zsh to try it out.${RESET}"
  240. exit
  241. fi
  242. exec zsh -l
  243. }
  244. main "$@"