使用易用性更强的oh-my-zsh代替系统自带的shell。


1、安装zsh并设置默认

debian系

1
sudo apt-get install zsh

Red Hat系

1
sudo yum install zsh

为当前用户设置为默认的shell

1
chsh -s /bin/zsh

如果要切换回原来的bash

1
chsh -s /bin/bash

2、安装oh-my-zsh

在线方式

oh-my-zsh的Github官方仓库提供了三种方法。

第一种,使用curl

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

第二种,使用wget

1
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

第三种,使用fetch

1
sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

离线方式

  1. oh-my-zsh的Github官方仓库下载好压缩包,得到ohmyzsh-master文件夹
  2. 拷贝ohmyzsh-master文件夹下所有文件到~/.oh-my-zsh目录下:
1
cp -r ~/Download/ohmyzsh-master ~/.oh-my-zsh
  1. 拷贝模板配置文件到~/.zshrc文件夹下
1
cp ~/.oh-my-zsh/templates/zshrc.zsh ~/.zshrc
  1. 刷新zsh配置
1
source ~/.zshrc

3、配置oh-my-zsh

插件安装

  1. 语法高亮: zsh-syntax-highlighting
  2. 智能建议: zsh-autosuggestions
  3. 解压文件: extract(自带)
1
2
3
4
cd ~/.oh-my-zsh/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions
source ~/.zshrc

详细配置

如下,更新时间2021.01.27:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/root/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# ZSH_THEME="amuse"
# ZSH_THEME="ys"
ZSH_THEME="agnoster"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
HIST_STAMPS="yyyy-mm-dd"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    git
    extract
    zsh-autosuggestions
    zsh-syntax-highlighting
    )

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

4. 故障排除

如果打开终端后,启动zsh出现如下警告:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxrwx 4 root root  4096 9月  27 14:26 /usr/share/zsh
drwxrwxrwx 5 root root  4096 9月  27 14:26 /usr/share/zsh/5.2
drwxrwxrwx 2 root root 36864 9月  27 14:26 /usr/share/zsh/5.2/functions
drwxrwxrwx 2 root root  4096 11月 17 17:07 /usr/share/zsh/site-functions

[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.

解决方法,修改.zshrc配置文件

1
vim ~/.zshrc

添加一行如下

1
ZSH_DISABLE_COMPFIX=true

再次刷新zsh配置即可

1
source ~/.zshrc

5、在OpenWrt系统上使用oh-my-zsh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
opkg update

opkg install zsh
opkg install git
opkg install git-http
opkg install ca-certificates

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
sed -i -- 's:/bin/ash:/usr/bin/zsh:g' /etc/passwd

# reboot, all done

6、参考资料

  1. oh-my-zsh官方仓库地址: https://github.com/ohmyzsh/ohmyzsh
  2. zsh+on-my-zsh配置教程指南: https://segmentfault.com/a/1190000013612471
  3. oh-my-zsh提示检测到不安全的完成相关目录: https://www.jianshu.com/p/7251819d790e
  4. oh-my-zsh主题展示: https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
  5. 在Fedora上优化bash或zsh: https://zhuanlan.zhihu.com/p/91195187
  6. zsh-syntax-highlighting : https://github.com/zsh-users/zsh-syntax-highlighting
  7. zsh-autosuggestions: https://github.com/zsh-users/zsh-autosuggestions
  8. Install oh-my-zsh on openwrt/lede-project: https://gist.github.com/fire1ce/65d3e370120750a5deb283abe1d74491
  9. 在openwrt中最小化安装oh-my-zsh: https://itcao.com/2020/06031516.html