唠唠闲话

Oh My Zsh是一个开源的、社区驱动的 Zsh 配置框架,其提供各种功能强大的插件、主题和工具以增强 Zsh 的使用体验。

Dec-28-2023 09-26-03

本篇介绍 zsh 安装及配置方法,其中无 root 安装 zsh 的部分参考了教程《无 root 或 sudo 权限安装 zsh》。

以下内容已写成脚本,可以通过下边命令一键安装

1
curl -sSL https://qiniu.wzhecnu.cn/scripts/oh-my-zsh-p10k.sh | bash

安装 zsh

输入 which zsh 检查是否已安装 zsh,若已安装则跳过本节。

sudo 用户直接运行

1
sudo apt install zsh

非 sudo 用户的安装比较繁琐,需手动编译,具体步骤如下。

下载安装包并解压

1
2
3
4
# cd download # 进入待下载目录
wget -O zsh.tar.xz https://sourceforge.net/projects/zsh/files/latest/download
xz -d zsh.tar.xz # 解压
tar -xvf zsh.tar # 解压

编译安装

1
2
3
cd zsh-5.9 # 进入解压后的目录
./configure --prefix=$HOME/software/zsh # 配置安装路径
make -j$(nproc) && make install # 编译安装

这里 --prefix 用于指定安装路径,根据自己的需要修改。然而第二步 configure 通常会提示无 ncurses 依赖

1
2
3
4
5
configure: error: "No terminal handling library was found on your system.
This is probably a library called 'curses' or 'ncurses'. You may
need to install a package called 'curses-devel' or 'ncurses-devel' on your
system."
See `config.log' for more details

如果出现 ncurses 依赖错误,需按下边步骤将 ncurses 装上:

设置环境变量,在 .bashrc 中添加变量,并执行 source ~/.bashrc 更新

1
2
3
4
5
6
export NCURSES_HOME=$HOME/software/ncurses
export CXXFLAGS="-fPIC"
export CFLAGS="-fPIC"
export PATH=$NCURSES_HOME/bin:$PATH
export LD_LIBRARY_PATH=$NCURSES_HOME/lib:$LD_LIBRARY_PATH
export CPPFLAGS="-I$NCURSES_HOME/include" LDFLAGS="-L$NCURSES_HOME/lib"

其中 NCURSES_HOMEncurses 的安装路径,根据自己的需要修改。

安装 ncurses

1
2
3
4
5
wget https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.2.tar.gz
tar -zxvf ncurses-6.2.tar.gz
cd ncurses-6.2
./configure --prefix=$NCURSES_HOME --with-shared --without-debug --enable-widec
make -j$(nproc) && make install

安装完成后,重新执行编译安装:

1
2
3
cd zsh-5.9
./configure --prefix=$HOME/software/zsh
make -j$(nproc) && make install

配置 oh-my-zsh

  1. 克隆仓库到主目录

    1
    git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh
  2. templates 目录下的配置文件拷贝至 .zshrc 即可

    1
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
  3. 配置高亮,自动提示和补全的插件

    1
    2
    3
    4
    ZSH_CUSTOM=~/.oh-my-zsh/custom
    git clone https://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
    git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
    git clone https://github.com/zsh-users/zsh-completions $ZSH_CUSTOM/plugins/zsh-completions

    然后执行

    1
    2
    ! grep -q "autoload -U compinit && compinit" ~/.zshrc && echo "autoload -U compinit && compinit" >> ~/.zshrc
    sed -i '/^plugins=/c\plugins=(git sudo z zsh-syntax-highlighting zsh-autosuggestions zsh-completions)' ~/.zshrc

    第一行判断并添加 autoload -U compinit && compinit~/.zshrc 文件中。
    第二行先找到 plugins= 所在行,将其改为 plugins=(git sudo z zsh-syntax-highlighting zsh-autosuggestions zsh-completions)

配置主题p10k

个人常用主题是 powerlevel10k,就以它为例子,其他主题的配置方法类似。

通过 git 下载主题

1
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

修改主题为 p10k

1
sed -i '/^ZSH_THEME=/c\ZSH_THEME="powerlevel10k/powerlevel10k"' ~/.zshrc

注:这里用了 sed 流编辑器(stream editor):

  • -i 表示直接修改文件
  • /^ZSH_THEME=/ 表示匹配以 ZSH_THEME= 开头的行
  • c\ 紧接要替换的内容

等价地,可以直接打开 ~/.zshrc 文件,找到 ZSH_THEME= 这一行,将其修改为 ZSH_THEME="powerlevel10k/powerlevel10k"

重新运行 zsh 触发配置,根据提示进行,如下图。
20230720135917

更改默认 shell

.bash_profile 里添加 exec $(which zsh) -l

1
2
3
4
changeshell="exec $(which zsh) -l"
if ! grep -q $changeshell ~/.bash_profile; then
echo "exec `which zsh` -l" >> ~/.bash_profile
fi

注意事项:用 ssh 连接服务器时,通常会先执行 .bash_profile 再执行 .bashrc 并启动 bash。但一些情况,比如用 vscode 运行 shell 时,可能不经过 .bash_profile 直接启动的 bash。这时可以直接在 .bashrc 里添加 exec $(which zsh) -l,这一来每次启动 bash 都会转成 ZSH。另一种方式是,在 vscode 新建终端里直接修改默认终端(推荐)。

20231111183647

插件配置

其他插件配置,比如 thefuck, autojump,后续补充。


以上,配置遇到问题欢迎讨论~~