本文介绍了如何在Ubuntu系统上安装和配置zsh和oh-my-zsh,包括基本安装步骤、配置插件等操作。提高终端的使用效率和定制性。

步骤一:安装 Zsh

安装Zsh前,需要更新Ubuntu的包列表:
 1sudo apt update
 2
接下来,安装Zsh
 1sudo apt install zsh -y
 2
验证安装:
 1zsh --version
 2

步骤二: 安装 oh my zsh

 1# 下载所需的构建工具和依赖项
 2apt-get -y install build-essential nghttp2 libnghttp2-dev libssl-dev
 3

使用一行命令安装

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

也可以手动脚本安装

 1vi instal.sh
 2
 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# 
38set -e  
39  
40# Track if $ZSH was provided 
41custom_zsh=${ZSH:+yes}  
42  
43# Default settings 
44ZSH=${ZSH:-~/.oh-my-zsh}  
45REPO=${REPO:-ohmyzsh/ohmyzsh}  
46REMOTE=${REMOTE:-https://github.com/${REPO}.git}  
47BRANCH=${BRANCH:-master}  
48  
49# Other options 
50CHSH=${CHSH:-yes}  
51RUNZSH=${RUNZSH:-yes}  
52KEEP_ZSHRC=${KEEP_ZSHRC:-no}  
53  
54  
55command_exists() {  
56    command -v "$@" >/dev/null 2>&1  
57}  
58  
59fmt_error() {  
60  printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2  
61}  
62  
63fmt_underline() {  
64  printf '\033[4m%s\033[24m\n' "$*"  
65}  
66  
67fmt_code() {  
68  # shellcheck disable=SC2016 # backtic in single-quote 
69  printf '`\033[38;5;247m%s%s`\n' "$*" "$RESET"  
70}  
71  
72setup_color() {  
73    # Only use colors if connected to a terminal 
74    if [ -t 1 ]; then  
75        RED=$(printf '\033[31m')  
76        GREEN=$(printf '\033[32m')  
77        YELLOW=$(printf '\033[33m')  
78        BLUE=$(printf '\033[34m')  
79        BOLD=$(printf '\033[1m')  
80        RESET=$(printf '\033[m')  
81    else  
82        RED=""  
83        GREEN=""  
84        YELLOW=""  
85        BLUE=""  
86        BOLD=""  
87        RESET=""  
88    fi  
89}  
90  
91setup_ohmyzsh() {  
92  # Prevent the cloned repository from having insecure permissions. Failing to do 
93  # so causes compinit() calls to fail with "command not found: compdef" errors 
94  # for users with insecure umasks (e.g., "002", allowing group writability). Note 
95  # that this will be ignored under Cygwin by default, as Windows ACLs take 
96  # precedence over umasks except for filesystems mounted with option "noacl". 
97  umask g-w,o-w  
98  
99  echo "${BLUE}Cloning Oh My Zsh...${RESET}"  
100  
101  command_exists git || {  
102    fmt_error "git is not installed"  
103    exit 1  
104  }  
105  
106  ostype=$(uname)  
107  if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then  
108    fmt_error "Windows/MSYS Git is not supported on Cygwin"  
109    fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"  
110    exit 1  
111  fi  
112  
113  git clone -c core.eol=lf -c core.autocrlf=false \  
114    -c fsck.zeroPaddedFilemode=ignore \  
115    -c fetch.fsck.zeroPaddedFilemode=ignore \  
116    -c receive.fsck.zeroPaddedFilemode=ignore \  
117    --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {  
118    fmt_error "git clone of oh-my-zsh repo failed"  
119    exit 1  
120  }  
121  
122  echo  
123}  
124  
125setup_zshrc() {  
126  # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones 
127  # with datestamp of installation that moved them aside, so we never actually 
128  # destroy a user's original zshrc 
129  echo "${BLUE}Looking for an existing zsh config...${RESET}"  
130  
131  # Must use this exact name so uninstall.sh can find it 
132  OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh  
133  if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then  
134    # Skip this if the user doesn't want to replace an existing .zshrc 
135    if [ "$KEEP_ZSHRC" = yes ]; then  
136      echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"  
137      return  
138    fi  
139    if [ -e "$OLD_ZSHRC" ]; then  
140      OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"  
141      if [ -e "$OLD_OLD_ZSHRC" ]; then  
142        fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"  
143        fmt_error "re-run the installer again in a couple of seconds"  
144        exit 1  
145      fi  
146      mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"  
147  
148      echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \  
149        "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"  
150    fi  
151    echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"  
152    mv ~/.zshrc "$OLD_ZSHRC"  
153  fi  
154  
155  echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"  
156  
157  sed "/^export ZSH=/ c\\  
158export ZSH=\"$ZSH\"  
159" "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp  
160    mv -f ~/.zshrc-omztemp ~/.zshrc  
161  
162    echo  
163}  
164  
165setup_shell() {  
166  # Skip setup if the user wants or stdin is closed (not running interactively). 
167  if [ "$CHSH" = no ]; then  
168    return  
169  fi  
170  
171  # If this user's login shell is already "zsh", do not attempt to switch. 
172  if [ "$(basename -- "$SHELL")" = "zsh" ]; then  
173    return  
174  fi  
175  
176  # If this platform doesn't provide a "chsh" command, bail out. 
177  if ! command_exists chsh; then  
178    cat <<EOF  
179I can't change your shell automatically because this system does not have chsh.  
180${BLUE}Please manually change your default shell to zsh${RESET}  
181EOF  
182    return  
183  fi  
184  
185  echo "${BLUE}Time to change your default shell to zsh:${RESET}"  
186  
187  # Prompt for user choice on changing the default login shell 
188  printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \  
189    "$YELLOW" "$RESET"  
190  read -r opt  
191  case $opt in  
192    y*|Y*|"") echo "Changing the shell..." ;;  
193    n*|N*) echo "Shell change skipped."; return ;;  
194    *) echo "Invalid choice. Shell change skipped."; return ;;  
195  esac  
196  
197  # Check if we're running on Termux 
198  case "$PREFIX" in  
199    *com.termux*) termux=true; zsh=zsh ;;  
200    *) termux=false ;;  
201  esac  
202  
203  if [ "$termux" != true ]; then  
204    # Test for the right location of the "shells" file 
205    if [ -f /etc/shells ]; then  
206      shells_file=/etc/shells  
207    elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS 
208      shells_file=/usr/share/defaults/etc/shells  
209    else  
210      fmt_error "could not find /etc/shells file. Change your default shell manually."  
211      return  
212    fi  
213  
214    # Get the path to the right zsh binary 
215    # 1. Use the most preceding one based on $PATH, then check that it's in the shells file 
216    # 2. If that fails, get a zsh path from the shells file, then check it actually exists 
217    if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then  
218      if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then  
219        fmt_error "no zsh binary found or not present in '$shells_file'"  
220        fmt_error "change your default shell manually."  
221        return  
222      fi  
223    fi  
224  fi  
225  
226  # We're going to change the default shell, so back up the current one 
227  if [ -n "$SHELL" ]; then  
228    echo "$SHELL" > ~/.shell.pre-oh-my-zsh  
229  else  
230    grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh  
231  fi  
232  
233  # Actually change the default shell to zsh 
234  if ! chsh -s "$zsh"; then  
235    fmt_error "chsh command unsuccessful. Change your default shell manually."  
236  else  
237    export SHELL="$zsh"  
238    echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"  
239  fi  
240  
241  echo  
242}  
243  
244main() {  
245  # Run as unattended if stdin is not a tty 
246  if [ ! -t 0 ]; then  
247    RUNZSH=no  
248    CHSH=no  
249  fi  
250  
251  # Parse arguments 
252  while [ $# -gt 0 ]; do  
253    case $1 in  
254      --unattended) RUNZSH=no; CHSH=no ;;  
255      --skip-chsh) CHSH=no ;;  
256      --keep-zshrc) KEEP_ZSHRC=yes ;;  
257    esac  
258    shift  
259  done  
260  
261  setup_color  
262  
263  if ! command_exists zsh; then  
264    echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."  
265    exit 1  
266  fi  
267  
268  if [ -d "$ZSH" ]; then  
269    echo "${YELLOW}The \$ZSH folder already exists ($ZSH).${RESET}"  
270    if [ "$custom_zsh" = yes ]; then  
271      cat <<EOF  
272  
273You ran the installer with the \$ZSH setting or the \$ZSH variable is  
274exported. You have 3 options:  
275  
2761. Unset the ZSH variable when calling the installer:  
277   $(fmt_code "ZSH= sh install.sh")  
2782. Install Oh My Zsh to a directory that doesn't exist yet:  
279   $(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")  
2803. (Caution) If the folder doesn't contain important information,  
281   you can just remove it with $(fmt_code "rm -r $ZSH")  
282  
283EOF  
284    else  
285      echo "You'll need to remove it if you want to reinstall."  
286    fi  
287    exit 1  
288  fi  
289  
290  setup_ohmyzsh  
291  setup_zshrc  
292  setup_shell  
293  
294  printf %s "$GREEN"  
295  cat <<'EOF'  
296         __                                     __  
297  ____  / /_     ____ ___  __  __   ____  _____/ /_  
298 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \  
299/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /  
300\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  
301                        /____/                       ....is now installed!  
302  
303  
304EOF  
305  cat <<EOF  
306Before you scream Oh My Zsh! please look over the ~/.zshrc file to select plugins, themes, and options.  
307  
308Follow us on Twitter: $(fmt_underline https://twitter.com/ohmyzsh)  
309Join our Discord server: $(fmt_underline https://discord.gg/ohmyzsh)  
310Get stickers, shirts, coffee mugs and other swag: $(fmt_underline https://shop.planetargon.com/collections/oh-my-zsh)  
311  
312EOF  
313  printf %s "$RESET"  
314  
315  if [ $RUNZSH = no ]; then  
316    echo "${YELLOW}Run zsh to try it out.${RESET}"  
317    exit  
318  fi  
319  
320  exec zsh -l  
321}  
322  
323main "$@"
 1chmod +x install.sh
 2sudo sh install.sh

步骤三: 装插件

安装插件

安装自动补全插件和语法高亮插件以提高命令行的效率和易用性。

 1# 自动补全
 2git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
 3# 语法高亮
 4git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
启用插件

编辑.zshrc文件以启用下载的插件。

 1vi ~/.zshrc

在文件中找到plugins=(git)并替换为以下内容:

 1plugins=(
 2  git
 3  zsh-autosuggestions
 4  docker
 5  docker-compose
 6)

保存并退出,然后应用更改:

 1source ~/.zshrc
个人笔记记录 2021 ~ 2025