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特有的功能。以下是一些可能的原因和解决方案:
-
Zsh没有默认支持:
command-not-found
是Bash的一个功能,在Zsh中没有默认启用。
-
没有安装相应的包:
- 确保你已经安装了
command-not-found
相关的软件包。在Debian/Ubuntu系统中,这个包通常是command-not-found
或command-not-found-zsh
。
- 确保你已经安装了
-
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)