简介

  • Zsh 是一款功能强大的终端(shell)软件,既可以作为一个交互式终端,也可以作为一个脚本解释器。它在兼容 Bash 的同时 (默认不兼容,除非设置成 emulate sh) 还有提供了很多改进。
  • oh-my-zsh是基于zsh的功能做了一个扩展,方便的插件管理、主题自定义,以及漂亮的自动完成效果。

本文以wsl ubuntu为例,其他发行版同理;

准备

查看当前环境shell

1
echo $SHELL

查看系统自带哪些shell

1
cat /etc/shells

安装

安装zsh

1
apt install zsh

安装oh-my-zsh

官方链接

自动安装

1
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

手动安装

1
2
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

第一种会报错,推荐这一种,简单!

真-手动安装

在oh-my-zsh的github主页,手动将zip包下载下来。
将zip包解压,拷贝至~/.oh-my-zsh目录。此处省略拷贝的操作步骤。
执行

1
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

三选一即可,适合各种环境下的安装,然后需要source ~/.zshrc将配置生效。以后修改了.zshrc文件之后,都执行一下这个命令。

切换shell

注意:切换完shell,记得将前一个shell的rc文件中的内容按需加到新的shell的rc文件中(主要是环境变量等)!

推荐方法

直接去修改/etc/passwd文件:

1
2
3
vim /etc/passwd
修改root那一行的bash为zsh
source .zshrc

其他方法

可以在安装zsh之后,将以下命令写到.bashrc文件中,手动切换当前shell为为zsh:

1
bash -c zsh

这种只是将当前shell临时设置为zsh,而系统配置中(/etc/passwd),当前用户的shell依然为bash!

永久切换:

1
chsh -s zsh

chsh实际上是去修改/etc/passwd文件,有的时候会出一些莫名其妙的问题,推荐直接自己去修改该文件中root用户对应的那一行即可;

oh-my-zsh主题设置

主题说明:https://github.com/ohmyzsh/ohmyzsh/wiki/themes

主题切换

默认的主题显示的不是全路径,不太友好,推荐主题af-magic,在.zshrc中将这一项改变:

1
ZSH_THEME="af-magic"

wsl不推荐agnoster主题,因为字体的问题,折腾起来比较麻烦,字体装了,乱码问题也没解决

插件推荐

插件依然需要打开~/.zshrc,找到plugins=,然后在里面写需要的插件名,有些插件可能还需要安装。 !!!! 注意,只要改了此文件,重启终端后有效 或 使用 source ~/.zshrc 更新配置。

这边插件推荐 : autojump、zsh-autosuggestion 以及 zsh-syntax-highlighting!

所有插件,也可以直接使用apt安装!但是除过第一个,剩下的推荐使用git,没什么问题;

autojump

此插件推荐使用包管理命令安装即可;
功能:实现目录间快速跳转,想去哪个目录直接 j + 目录名,不用在频繁的 cd 了!

history | grep "git clone"这个命令就能找到近期 clone 了哪些库,省却了写一堆代码的功夫。
autojump 就是通过记录你在 history 中的行为把你访问过的文件夹路径都 cache 下来,当你输入路径名的时候会模糊匹配你之前cd过的目录路径,配合后面的自动提示插件,无敌了!!!

我先cd到一些目录,然后就可以j快速切换,用jo快速在finder里打开文件夹

1
2
3
4
5
6
7
8
9
10
#安装
git clone git://github.com/joelthelion/autojump.git
cd autojump
./install.py
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(autojump)
# 在文件末尾添加
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh
source ~/.zshrc

使用教程:https://linux.cn/article-3401-1.html

zsh-autosuggestion

功能:输入命令时可提示自动补全(灰色部分),然后按键盘 → (上下左右的右键,不是tab键)即可补全

1
2
3
4
5
6
7
8
9
# 安装
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(
autojump
zsh-autosuggestions
)
source ~/.zshrc

zsh-syntax-highlighting

功能:日常用的命令会高亮显示,命令错误显示红色

1
2
3
4
5
6
7
8
9
10
# 安装
git clone git://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(
autojump
zsh-autosuggestions
zsh-syntax-highlighting
)
source ~/.zshrc

可能遇到的问题

WSL可能遇到的问题

问题1:加载zsh报错

安装完zsh和ohmyzsh后,发现启动终端时并不能成功切换到zsh,有如下报错:

1
2
3
4
$ source .zshrc
autoload: command not found
bash: /home/mahy/.oh-my-zsh/oh-my-zsh.sh: line 41: syntax error near unexpected token `('
bash: /home/mahy/.oh-my-zsh/oh-my-zsh.sh: line 41: `for plugin ($plugins); do'

解决方案:
根据报错可以搜索到oh-my-zsh官方仓库早在2017年就有人反馈这个问题了(issues/6405) ,按上面的解决方案执行,问题依然存在,最后查出了是wsl的问题。

这个问题可以在windows/wsl 上找到解决办法,简单来说,就是在bash的配置文件如~/.bashrc中写入bash -c zsh, 手动切换当前shell为zsh。 然后执行source .bashrc, 或者重启终端即可解决。

问题2:[oh-my-zsh] 提示检测到不安全的完成相关目录

加入之后,会有这一串乱七八糟的提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxrwx 1 root root 4096 May 2 01:28 /root/.oh-my-zsh
drwxrwxrwx 1 root root 4096 May 2 01:28 /root/.oh-my-zsh/plugins
drwxrwxrwx 1 root root 4096 May 2 01:28 /root/.oh-my-zsh/plugins/git

[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.

[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w

[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.

解决办法:

如果是在.bashrc中加载zsh,那么,按照说明,在.bashrc中加载zsh语句之前加上:

1
export ZSH_DISABLE_COMPFIX=true

如果是直接修改/etc/passwd中root的shell为zsh,那么直接在.zshrc开始处加入:

1
ZSH_DISABLE_COMPFIX=true

参考:https://www.jianshu.com/p/7251819d790e

问题3:切换bash认证问题

使用

1
chsh -s zsh

切换之后,想切换回bash,结果需要输入密码验证,输入怎样的密码都是错误的,认证失败,如下:

1
2
3
# chsh -s /bin/bash
Password:
chsh: PAM: Authentication failure

解决办法:
直接去修改/etc/passwd文件即可;

参考 : https://blog.nofile.cc/555.html

chsh实际上是更改/etc/passwd文件,所以自己手动去改这个文件是最好的;

CentOS7遇到的问题

yum安装autojump之后找不到命令

没有j命令并且打开新的终端时报以下提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed May 6 10:55:06 2020 from 10.0.200.31
/root/.autojump/share/autojump/autojump.zsh:22: command not found: uname
/usr/share/autojump/autojump.zsh:13: command not found: uname
/usr/libexec/grepconf.sh: line 5: grep: command not found
/root/.oh-my-zsh/oh-my-zsh.sh:14: command not found: env
/root/.oh-my-zsh/oh-my-zsh.sh:72: compinit: function definition file not found
detect-clipboard:33: command not found: uname
/root/.oh-my-zsh/lib/completion.zsh:76: bashcompinit: function definition file not found
/root/.oh-my-zsh/lib/directories.zsh:32: command not found: compdef
/root/.oh-my-zsh/lib/termsupport.zsh:109: add-zsh-hook: function definition file not found
/root/.oh-my-zsh/lib/termsupport.zsh:110: add-zsh-hook: function definition file not found
/root/.oh-my-zsh/lib/theme-and-appearance.zsh:2: colors: function definition file not found
/root/.autojump/share/autojump/autojump.zsh:22: command not found: uname
/root/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh:858: add-zsh-hook: function definition file not found
/root/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh:59: is-at-least: function definition file not found
zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.
zsh-syntax-highlighting: failed loading add-zsh-hook.
/root/.zsh-syntax-highlighting/zsh-syntax-highlighting.zsh:59: is-at-least: function definition file not found
zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.
zsh-syntax-highlighting: failed loading add-zsh-hook.
/root/.autojump/share/autojump/autojump.zsh:22: command not found: uname

原因:
如果你是仿照各种博文里的说法,用yum install autojump安装的,那么在使用的时候会发现autojump xxx命令没有做任何事,而如果你想使用缩写j xxx,会收到提示

1
2
zsh: j: command not found...
zsh: command not found: j

一般人们会提醒你在.zshrc里加入一行plugins=(git autojump),不过如果你看一下.oh-my-zsh/plugins/autojump/autojump.plugin.zsh这个文件里的内容,会发现zsh试图载入一个叫/etc/profile.d/autojump.zsh的文件,但这个文件并不存在,这才是问题所在-_-||

查看autojump的github主页,发现文档里有这么一句话:“RedHat, Fedora, CentOS (install autojump-zsh for zsh, autojump-fish for fish, etc.)”,所以光安装autojump是不够的,还要安装autojump-zsh这个rpm包才行。

解决办法:
最好参考autojump官方文档安装:
autojump官方文档:https://github.com/wting/autojump

安装autojump之后没有jo命令

1
jo:11: command not found: xdg-open

原因
没有安装xdg-utils

解决方案
yum install xdg-utils

安装完成后就不会报错了

评论