在 CentOS 7 上安装和配置 zsh 的步骤如下:
1. 安装 zsh
# 更新系统包
sudo yum update -y
# 安装 zsh
sudo yum install -y zsh
2. 安装 oh-my-zsh(可选但推荐)
# 方法1:使用 curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 方法2:使用 wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 如果遇到网络问题,可以尝试国内镜像
sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"
3. 将 zsh 设为默认 shell
# 查看当前 shell
echo $SHELL
# 查看系统可用 shell
cat /etc/shells
# 更改默认 shell 为 zsh
chsh -s $(which zsh)
# 或指定完整路径
chsh -s /bin/zsh
注意: 更改后需要重新登录或重启终端才能生效。
4. 安装 powerlevel10k 主题(可选)
# 安装字体(推荐)
sudo yum install -y epel-release
sudo yum install -y powerline-fonts
# 安装 powerlevel10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
# 修改 ~/.zshrc 中的主题设置
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc
# 重新加载配置
source ~/.zshrc
5. 安装自动补全插件
# zsh-autosuggestions(历史命令建议)
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 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=(git zsh-autosuggestions zsh-syntax-highlighting)
6. 配置 ~/.zshrc(常用设置)
# 编辑配置文件
vim ~/.zshrc
# 常用配置示例:
# 启用自动补全
autoload -U compinit && compinit
# 启用颜色支持
autoload -U colors && colors
# 设置别名
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias zshconfig="vim ~/.zshrc"
alias ohmyzsh="vim ~/.oh-my-zsh"
# 应用配置
source ~/.zshrc
7. 安装其他有用工具
# 安装命令补全增强
sudo yum install -y util-linux-user
# 安装语法高亮相关依赖
sudo yum install -y ncurses-devel
8. 验证安装
# 查看当前 shell
echo $SHELL
# 查看 zsh 版本
zsh --version
常见问题解决
- 如果 chsh 命令不生效:
# 直接修改 /etc/passwd 文件中的用户 shell
sudo usermod -s /bin/zsh $(whoami)
- oh-my-zsh 安装失败:
# 手动安装
wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
chmod +x install.sh
# 编辑 install.sh,将 REPO 改为国内镜像
# REPO=${REPO:-mirrors/oh-my-zsh}
# REMOTE=${REMOTE:-https://gitee.com/${REPO}.git}
./install.sh
- 安装后命令补全异常:
# 删除旧的补全缓存
rm -f ~/.zcompdump
# 重新生成
compinit
重启终端后,你应该就能使用功能强大的 zsh 了!
