Zsh 与 Oh My Zsh 安装配置
Zsh 简介
Zsh(Z Shell)是一个功能强大的交互式 shell,比 Bash 更强大,具有以下优势:
- 更智能的自动补全
- 更强大的插件系统
- 更丰富的配色方案
- 可自定义主题
安装 Zsh
Ubuntu / Debian
# 安装
sudo apt update
sudo apt install -y zsh
# 验证安装
zsh --version
# 设置为默认 shell
chsh -s $(which zsh)
CentOS / RHEL / Fedora
sudo dnf install -y zsh
chsh -s /bin/zsh
macOS
# macOS 默认已安装 zsh,如需最新版
brew install zsh
# 设置默认
chsh -s /bin/zsh
安装 Oh My Zsh
Oh My Zsh 是一个开源、社区驱动的框架,用于管理 Zsh 配置。
方式一:curl(推荐)
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
方式二:wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
如遇网络问题,可先配置代理:
export http_proxy="socks5://127.0.0.1:1080" export https_proxy="socks5://127.0.0.1:1080" sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" unset http_proxy https_proxy
安装成功确认
安装完成后会显示:
- Zsh 设置为默认 shell
- 主题变为
robbyrussell(默认主题)
配置 Oh My Zsh
配置文件位置
~/.zshrc # 主要配置文件
~/.oh-my-zsh/ # 安装目录
常用配置项
编辑 ~/.zshrc:
nano ~/.zshrc
# 或
code ~/.zshrc
主题配置:
# ZSH_THEME="robbyrussell" # 默认主题
ZSH_THEME="agnoster" # 更推荐的主题
插件配置:
# 启用插件(每行一个)
plugins=(
git
docker
node
npm
vscode
sudo # 双击 Esc 添加 sudo
z # 快速跳转目录(需要 z 命令)
extract # 解压所有格式
autojump # 快速跳转(需安装 autojump)
)
自动更新:
# 关闭自动更新提示(可选)
DISABLE_UPDATE_PROMPT=true
# 或禁用自动更新
DISABLE_AUTO_UPDATE=true
手动更新 Oh My Zsh
omz update
推荐插件
内置插件(无需额外安装)
| 插件 | 功能 |
|---|---|
git | Git 命令别名(如 gs = git status) |
docker | Docker 命令补全 |
node / npm | Node.js 相关补全 |
pip | Python pip 补全 |
sudo | Esc 键快速加 sudo |
extract | x 解压任意格式 |
z | 快速跳转历史目录 |
第三方插件
1. zsh-autosuggestions(命令自动建议)
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
在 ~/.zshrc 中添加:
plugins=(... zsh-autosuggestions)
2. zsh-syntax-highlighting(命令高亮)
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
在 ~/.zshrc 中添加(必须放在最后):
plugins=(... zsh-syntax-highlighting)
3. fzf(模糊搜索)
# 安装 fzf
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
# 添加到 plugins
plugins=(... fzf)
4. autojump(快速跳转)
# Ubuntu
sudo apt install autojump
# macOS
brew install autojump
# 添加到 plugins
plugins=(... autojump)
推荐主题
1. Powerlevel10k(最流行)
# 安装
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
# 设置主题
ZSH_THEME="powerlevel10k/powerlevel10k"
首次使用会弹出配置向导,按提示选择即可。
推荐配置选项:
- 风格:Rainbow(彩色)
- 字符:Unicode
- 显示头信息:是
- 并行线:是
2. Agnoster
# 内置主题,直接修改
ZSH_THEME="agnoster"
需要安装 Powerline 字体才能正常显示:
# Ubuntu sudo apt install fonts-powerline # macOS brew install powerline-fonts
3. Spaceship
git clone https://github.com/spaceship-prompt/spaceship-prompt.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/spaceship-prompt
ln -s ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/spaceship-prompt/spaceship.zsh-theme ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/spaceship.zsh-theme
ZSH_THEME="spaceship"
常用别名
在 ~/.zshrc 末尾添加:
# 文件操作
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
# Git
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph'
alias gd='git diff'
# Docker
alias d='docker'
alias dc='docker-compose'
alias dps='docker ps'
alias dpa='docker ps -a'
# 系统
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# 网络
alias ping='ping -c 5' # 默认 ping 5 次
alias wget='wget -c' # 断点续传
进阶配置
环境变量
# Java
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$PATH:$JAVA_HOME/bin
# Go
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
# Node
export PATH=$PATH:$(npm config get prefix)/bin
历史记录配置
# 历史记录设置
HIST_STAMPS="yyyy-mm-dd"
HISTSIZE=10000
SAVEHIST=10000
# 共享历史记录(多个终端同步)
setopt SHARE_HISTORY
# 忽略重复命令
setopt HIST_IGNORE_DUPS
# 去掉空格开头的命令
setopt HIST_IGNOREDUPS
命令补全
# 启用更多补全功能
autoload -Uz compinit
compinit
# 补全菜单选择
zstyle ':completion:*' menu select
# 大小写不敏感
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
快速命令参考
| 命令 | 说明 |
|---|---|
omz update | 更新 Oh My Zsh |
omz reload | 重载配置 |
omz pr | 播放随机音乐(Oh My Zsh 内置) |
常见问题
1. 主题显示乱码
安装对应字体后执行:
# 重新加载
source ~/.zshrc
# 或设置默认字体
2. 切换回 Bash
bash
3. 完全卸载 Oh My Zsh
uninstall_oh_my_zsh
4. 恢复默认配置
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
source ~/.zshrc