唠唠闲话

sudo 权限和非 sudo 权限配置 zsh 的方法,参考教程:无 root 或 sudo 权限安装 zsh


安装 zsh

sudo 用户直接运行

1
sudo apt install zsh -y

这里介绍非 sudo 用户的安装方法。

  1. 下载安装包并解压

    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 # 解压
  2. 编译安装

    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 依赖错误,可以按一下步骤解决

  1. 设置环境变量,在 .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 的安装路径,根据自己的需要修改。

  2. 安装 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

安装完成后,重新执行上一小节的编译安装步骤。

配置 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
    [ -z "`grep "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

    第一行 [ -z ] 用于判断字符串是否为空,等价地,可以直接添加 autoload -U compinit && compinit~/.zshrc 文件中。
    第二行规则与前边一样,找到 plugins= 所在行,将其改为 plugins=(git sudo z zsh-syntax-highlighting zsh-autosuggestions zsh-completions)

  4. 修改主题为 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="agnoster"

配置主题p10k

使用 powerlevel10k 主题后,还需如下配置。

  1. 下载安装四种字体(推荐):

    等价地,可以用 curl 命令行下载

    1
    2
    3
    4
    curl -L https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf -o MesloLGS\ NF\ Regular.ttf
    curl -L https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf -o MesloLGS\ NF\ Bold.ttf
    curl -L https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf -o MesloLGS\ NF\ Italic.ttf
    curl -L https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf -o MesloLGS\ NF\ Bold\ Italic.ttf

    双击文件进行安装(命令行安装暂时没找到靠谱的方式)

  2. 通过 git 下载主题

    1
    git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  3. 重启 shell 进行配置,根据提示进行,如下图。
    20230720135917

更改默认 shell

.bash_profile(如果文件不存在则手动创建)或 .bashrc 里添加

1
echo "exec `which zsh` -l" >> ~/.bash_profile

留意一点,以下是个人测试结果:通过 ssh 连接启动 shell 时,会先执行 .bash_profile 内容;但从本机直接打开 shell 则不会,这导致前一方法开启的 shell 还是 bash。所以只能通过 .bashrc 追加 exec $ZSH_PATH -l 来更改默认 shell。但这种更改方式有个缺陷,如果你突然想用 bash 了,就没法了,因为执行 bash 会经过 .bashrc 重新转回 zsh,除非手动注释掉这行代码。