跳到主要内容

07 oh-my-zsh

在Linux系统上配置Zsh并安装Oh My Zsh和Powerlevel10k(p10k)主题,可以按照以下步骤进行:

1. 安装Zsh

首先,确保你的系统上已经安装了Zsh。如果没有,你可以使用以下命令安装:

  • Debian/Ubuntu系统

    sudo apt-get update
    sudo apt-get install zsh
  • Fedora

    sudo dnf install zsh
  • Arch Linux

    sudo pacman -S zsh

2. 设置Zsh为默认Shell

安装完成后,可以将Zsh设置为你的默认shell:

chsh -s $(which zsh)

你可能需要输入你的用户密码来完成此操作。

3. 安装Oh My Zsh

安装Oh My Zsh来管理你的Zsh配置:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

或使用wget

sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
网络不通,无法下载?
  • 使用科学上网

  • 修改DNS服务器(临时)

    sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf
    sudo echo "nameserver 8.8.4.4" >> /etc/resolv.conf

4. 安装Powerlevel10k

  • 使用Oh My Zsh的插件安装

    打开你的~/.zshrc文件,找到ZSH_THEME这一行,将其改为:

    ZSH_THEME="powerlevel10k/powerlevel10k"

    如果powerlevel10k插件没有自动安装,可以通过以下命令手动安装:

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

    第一次运行Zsh时,Powerlevel10k会提示你进行配置。你可以选择默认配置或者根据提示自定义你的配置。运行:

    p10k configure

    或者直接编辑~/.p10k.zsh文件来手动配置。

5. 启动Zsh

安装和配置完成后,启动一个新的终端窗口或输入zsh来进入Zsh环境。Powerlevel10k的主题应该已经应用。

6. 常用插件

你还可以安装一些常用的Zsh插件来增强你的命令行体验,例如:

  • 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
  • zsh-command-not-found:

    git clone https://github.com/Tarrasch/zsh-command-not-found ~/.oh-my-zsh/custom/plugins/command-not-found

在你的~/.zshrc文件中,添加这些插件到plugins列表:

plugins=(git zsh-autosuggestions zsh-syntax-highlighting zsh-command-not-found)

7. 源化配置文件

最后,确保你已经源化了你的~/.zshrc文件来应用所有更改:

source ~/.zshrc

注意事项

  • 如果你使用的是某些特定的Linux发行版(如Manjaro),可能已经内置了Zsh和Oh My Zsh,你只需要更改主题和安装插件。
  • 每次更新~/.zshrc文件后,都需要重新启动终端或使用source ~/.zshrc来应用更改。
  • 记得备份你的配置文件(如~/.zshrc~/.p10k.zsh),以便在需要时可以快速恢复设置。

配置完成后,你应该拥有一个功能强大、美观的命令行环境。


修复 command-not-found

在Zsh环境下,command-not-found功能可能不会自动启用,因为它通常是Bash特有的功能。以下是一些可能的原因和解决方案:

  1. Zsh没有默认支持

    • command-not-found是Bash的一个功能,在Zsh中没有默认启用。
  2. 没有安装相应的包

    • 确保你已经安装了command-not-found相关的软件包。在Debian/Ubuntu系统中,这个包通常是command-not-foundcommand-not-found-zsh
  3. Zsh配置文件

    • 检查你的.zshrc文件是否加载了相应的插件或函数来提供command-not-found功能。

解决方案:

  • 安装相应的软件包

    sudo apt-get install command-not-found
  • 使用Zsh插件: 如果你使用Oh-My-Zsh等框架,可以安装插件来提供类似的功能。例如,安装command-not-found插件:

    git clone https://github.com/Tarrasch/zsh-command-not-found ~/.oh-my-zsh/custom/plugins/command-not-found

    然后在.zshrc中启用该插件:

    plugins=(... command-not-found)

    重新启动Zsh或运行source ~/.zshrc来使其生效。

请注意,具体的配置可能会因你的Zsh版本和系统而有所不同。如果上述方法不奏效,建议查阅Zsh的官方文档或社区论坛以获取更多信息。