Skip to content
0

macOS / Linux 通过国内镜像安装 Zsh 和 Oh My Zsh ​

什么是 Zsh 和 Oh My Zsh ​

Zsh(Z Shell) 是一款功能强大的命令行 shell,兼容 Bash,但提供了更多高级特性,如:

  • 更强大的自动补全(支持模糊匹配、菜单选择)
  • 更灵活的主题和提示符定制
  • 共享命令历史
  • 更智能的拼写纠正
  • 丰富的插件生态

Oh My Zsh 是 Zsh 最流行的配置框架,集成了数百个插件和主题,让你开箱即用地享受 Zsh 的强大功能。

安装 Zsh ​

macOS ​

macOS 从 Catalina (10.15) 开始已默认使用 Zsh 作为登录 shell。如果系统版本较旧或需要更新:

shell
# 使用 Homebrew 安装最新版 Zsh
brew install zsh

# 验证安装
zsh --version

Linux(Ubuntu/Debian) ​

shell
# 更新软件包索引
sudo apt update

# 安装 zsh
sudo apt install zsh -y

# 验证安装
zsh --version

Linux(CentOS/RHEL/Fedora) ​

shell
# CentOS/RHEL
sudo yum install zsh -y

# Fedora
sudo dnf install zsh -y

设置 Zsh 为默认 Shell ​

shell
# 将 zsh 设为默认 shell
chsh -s $(which zsh)

# 或手动指定路径
chsh -s /bin/zsh

注意:执行 chsh 后需要重新登录或重启终端才能生效。

安装 Oh My Zsh(国内镜像加速) ​

由于网络原因,官方安装脚本在国内可能下载缓慢或失败。以下是通过清华 TUNA 镜像安装的步骤:

1. 克隆 Oh My Zsh 镜像仓库 ​

shell
# 从清华镜像克隆
git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git

cd ohmyzsh/tools

2. 修改安装脚本中的仓库地址 ​

编辑 install.sh 文件,将官方地址替换为镜像地址:

shell
vim install.sh

找到以下两行(通常在文件开头附近):

shell
REPO=${REPO:-ohmyzsh}
REMOTE=${REMOTE:-https://github.com/${REPO}/${REPO}.git}

修改为:

shell
REPO=${REPO:-ohmyzsh}
REMOTE=${REMOTE:-https://mirrors.tuna.tsinghua.edu.cn/git/${REPO}.git}

3. 执行安装脚本 ​

shell
# 直接运行修改后的脚本
sh install.sh

# 或使用以下方式指定 REMOTE 变量(无需修改文件)
REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git sh install.sh

安装完成后,会提示你是否要将 Zsh 设为默认 shell(如果之前没设置的话)。

备用方案:Gitee 镜像 ​

如果 TUNA 镜像速度仍不理想,也可以使用 Gitee 镜像:

shell
REMOTE=https://gitee.com/mirrors/oh-my-zsh.git sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"

配置主题 ​

Oh My Zsh 默认使用 robbyrussell 主题。以下是安装流行的 Powerlevel10k 主题的步骤:

安装 Powerlevel10k ​

shell
# 从 Gitee 镜像克隆(国内更快)
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

启用主题 ​

编辑 ~/.zshrc:

shell
vim ~/.zshrc

修改主题配置:

shell
ZSH_THEME="powerlevel10k/powerlevel10k"

保存后执行:

shell
source ~/.zshrc

首次运行 Powerlevel10k 会启动配置向导,根据提示选择喜欢的样式即可。之后可以通过以下命令重新配置:

shell
p10k configure

推荐插件 ​

编辑 ~/.zshrc,找到 plugins=(git) 这一行,添加需要的插件:

shell
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    autojump
    extract
    history
)

常用插件安装 ​

zsh-autosuggestions(命令自动建议)

shell
git clone https://gitee.com/mirrors/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

zsh-syntax-highlighting(命令语法高亮)

shell
git clone https://gitee.com/mirrors/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

autojump(目录快速跳转)

shell
# macOS
brew install autojump

# Ubuntu/Debian
sudo apt install autojump -y

安装完插件后,记得执行 source ~/.zshrc 使配置生效。

配置镜像源(可选) ​

如果你后续需要更新 Oh My Zsh,可以将远程仓库地址改为镜像地址:

shell
# 进入 Oh My Zsh 目录
cd ~/.oh-my-zsh

# 查看当前远程地址
git remote -v

# 修改为清华镜像
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git

# 或修改为 Gitee 镜像
git remote set-url origin https://gitee.com/mirrors/oh-my-zsh.git

常用 Oh My Zsh 快捷键 ​

快捷键功能
Tab自动补全(按两次显示所有选项)
↑ / ↓搜索历史命令
Ctrl + R模糊搜索历史命令
Ctrl + A / Ctrl + E光标移到行首 / 行尾
Esc + .插入上一条命令的最后一个参数
!!执行上一条命令
!$上一条命令的最后一个参数

常见问题 ​

1. 安装后终端显示异常 ​

可能是终端字体不支持 Powerline 符号。建议安装 Nerd Fonts(如 MesloLGS NF):

shell
# 使用 Homebrew 安装(macOS)
brew tap homebrew/cask-fonts
brew install --cask font-meslo-lg-nerd-font

# 然后在终端设置中将字体改为 MesloLGS NF

2. Oh My Zsh 启动变慢 ​

可以通过以下命令诊断启动耗时:

shell
# 查看各插件加载时间
for plugin ($plugins); do; timer=$(($(date +%s%N)/1000000)); autoload -U +X bashcompinit && bashcompinit; source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh 2>/dev/null || source $ZSH/plugins/$plugin/$plugin.plugin.zsh; echo "$plugin: $(($(($(date +%s%N)/1000000))-$timer))ms"; done

3. 恢复默认 Bash ​

如果不喜欢 Zsh,可以恢复默认 Shell:

shell
chsh -s /bin/bash

然后卸载 Oh My Zsh:

shell
uninstall_oh_my_zsh
最近更新