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。如果系统版本较旧或需要更新:
# 使用 Homebrew 安装最新版 Zsh
brew install zsh
# 验证安装
zsh --versionLinux(Ubuntu/Debian)
# 更新软件包索引
sudo apt update
# 安装 zsh
sudo apt install zsh -y
# 验证安装
zsh --versionLinux(CentOS/RHEL/Fedora)
# CentOS/RHEL
sudo yum install zsh -y
# Fedora
sudo dnf install zsh -y设置 Zsh 为默认 Shell
# 将 zsh 设为默认 shell
chsh -s $(which zsh)
# 或手动指定路径
chsh -s /bin/zsh注意:执行
chsh后需要重新登录或重启终端才能生效。
安装 Oh My Zsh(国内镜像加速)
由于网络原因,官方安装脚本在国内可能下载缓慢或失败。以下是通过清华 TUNA 镜像安装的步骤:
1. 克隆 Oh My Zsh 镜像仓库
# 从清华镜像克隆
git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git
cd ohmyzsh/tools2. 修改安装脚本中的仓库地址
编辑 install.sh 文件,将官方地址替换为镜像地址:
vim install.sh找到以下两行(通常在文件开头附近):
REPO=${REPO:-ohmyzsh}
REMOTE=${REMOTE:-https://github.com/${REPO}/${REPO}.git}修改为:
REPO=${REPO:-ohmyzsh}
REMOTE=${REMOTE:-https://mirrors.tuna.tsinghua.edu.cn/git/${REPO}.git}3. 执行安装脚本
# 直接运行修改后的脚本
sh install.sh
# 或使用以下方式指定 REMOTE 变量(无需修改文件)
REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git sh install.sh安装完成后,会提示你是否要将 Zsh 设为默认 shell(如果之前没设置的话)。
备用方案:Gitee 镜像
如果 TUNA 镜像速度仍不理想,也可以使用 Gitee 镜像:
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
# 从 Gitee 镜像克隆(国内更快)
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k启用主题
编辑 ~/.zshrc:
vim ~/.zshrc修改主题配置:
ZSH_THEME="powerlevel10k/powerlevel10k"保存后执行:
source ~/.zshrc首次运行 Powerlevel10k 会启动配置向导,根据提示选择喜欢的样式即可。之后可以通过以下命令重新配置:
p10k configure推荐插件
编辑 ~/.zshrc,找到 plugins=(git) 这一行,添加需要的插件:
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
autojump
extract
history
)常用插件安装
zsh-autosuggestions(命令自动建议)
git clone https://gitee.com/mirrors/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestionszsh-syntax-highlighting(命令语法高亮)
git clone https://gitee.com/mirrors/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlightingautojump(目录快速跳转)
# macOS
brew install autojump
# Ubuntu/Debian
sudo apt install autojump -y安装完插件后,记得执行 source ~/.zshrc 使配置生效。
配置镜像源(可选)
如果你后续需要更新 Oh My Zsh,可以将远程仓库地址改为镜像地址:
# 进入 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):
# 使用 Homebrew 安装(macOS)
brew tap homebrew/cask-fonts
brew install --cask font-meslo-lg-nerd-font
# 然后在终端设置中将字体改为 MesloLGS NF2. Oh My Zsh 启动变慢
可以通过以下命令诊断启动耗时:
# 查看各插件加载时间
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"; done3. 恢复默认 Bash
如果不喜欢 Zsh,可以恢复默认 Shell:
chsh -s /bin/bash然后卸载 Oh My Zsh:
uninstall_oh_my_zsh