Oh-My-Zsh 配置教程
唠唠闲话
sudo 权限和非 sudo 权限配置 zsh 的方法,参考教程:无 root 或 sudo 权限安装 zsh。
安装 zsh
sudo 用户直接运行
1 | sudo apt install zsh -y |
这里介绍非 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
3cd zsh-5.9 # 进入解压后的目录
./configure --prefix=$HOME/software/zsh # 配置安装路径
make -j$(nproc) && make install # 编译安装这里
--prefix
用于指定安装路径,根据自己的需要修改。configure
阶段经常会提示无ncurses
依赖1
2
3
4
5configure: 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
6export 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_HOME
为ncurses
的安装路径,根据自己的需要修改。 -
安装
ncurses
1
2
3
4
5wget 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
git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh
-
将
templates
目录下的配置文件拷贝至.zshrc
即可1
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
-
配置高亮,自动提示和补全的插件
1
2
3
4ZSH_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)
。 -
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 主题后,还需如下配置。
-
下载安装四种字体(推荐):
等价地,可以用 curl 命令行下载
1
2
3
4curl -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双击文件进行安装(命令行安装暂时没找到靠谱的方式)
-
通过 git 下载主题
1
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
-
重启 shell 进行配置,根据提示进行,如下图。
更改默认 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,除非手动注释掉这行代码。