安装 Zsh,并使用 Oh My Zsh

Zsh 是什么

shell 俗称壳,c 语言编写的命令解析器程序,是用户使用 linux 的桥梁。Linux/Unix 提供了很多种 Shell。常用的 Shell 有这么几种,sh、bash、csh 等。可以通过以下命令,查看系统有几种 shell:

1
$ cat /etc/shells

目前常用的 Linux 系统和 macOS X 系统的默认 Shell 都是 bash。但是真正强大的 Shell 是深藏不露的 zsh,史称『终极 Shell』,由于与 bash 相似,功能又有所加强,zsh 在 Linux 社区获得了关注。但因配置过于复杂,所以初期无人问津。直到国外有个程序员开发出了一个能够快速上手的 zsh 项目,叫做「Oh My Zsh」,Github 网址是:https://github.com/ohmyzsh/ohmyzsh

安装 zsh

2020.12.18 更新:
撸了个安装脚本,传送门 -> Github

查看系统中有无 zsh ,以及其版本

1
2
3
$ cat /etc/shells
$ zsh --version
$ echo $ZSH_VERSION

若系统中没有 zsh ,则需要安装

1
2
3
4
5
$ sudo apt-get install zsh  # Ubuntu
$ sudo yum install zsh # CentOS

# macOS 系统自带了zsh, 一般不是最新版,如果需要最新版可通过Homebrew来安装
$ brew install zsh

修改默认 shell

1
2
3
4
5
$ chsh -s /bin/zsh
# 或
$ chsh -s /usr/bin/zsh
# 或
$ chsh -s $(which zsh)

安装 Oh My Zsh

一键安装,方式如下:

1
2
3
4
5
6
7
8
9
10
#方法一:wget方式自动化安装oh my zsh:
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

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

#官网上的另外一种写法
curl -Lo install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
sh install.sh

美化、插件、配置

以下是自用配置,做个备份

修改主题

1
$ vim ~/.zshrc

修改 ZSH_THEME="robbyrussell"ZSH_THEME="ys"

修改显示方式

1
$ vim ~/.oh-my-zsh/themes/ys.zsh-theme

修改最下面部分为

1
2
3
4
5
6
7
8
9
10
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
%{$fg[white]%}in \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
\
%{$fg[white]%}[%*] $exit_code
%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"

或改为(2020.1.7):

1
2
3
4
5
6
7
8
9
10
11
12
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
%{$fg[white]%}@ \
%{$fg[green]%}morooi's MacBook Pro \
%{$fg[white]%}in \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
\
%{$fg[white]%}[%*] $exit_code
%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"

插件:zsh-syntax-highlighting

shell 命令的代码高亮,Github 地址:https://github.com/zsh-users/zsh-syntax-highlighting

安装:

  1. Clone this repository in oh-my-zsh’s plugins directory:

    1
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  2. Activate the plugin in ~/.zshrc:

    1
    plugins=( [plugins...] zsh-syntax-highlighting)
  3. Restart zsh (such as by opening a new instance of your terminal emulator).

插件:zsh-autosuggestions

在输入命令的过程中根据你的历史记录显示你可能想要输入的命令,按 tab 补全。

安装:

  1. Clone this repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins)

    1
    git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  2. Add the plugin to the list of plugins for Oh My Zsh to load (inside ~/.zshrc):

    1
    plugins=(zsh-autosuggestions)
  3. Start a new terminal session.

插件:extract, z

extract 用于解压任何压缩文件,不必根据压缩文件的后缀名来记忆压缩软件

z 用于目录间快速跳转

添加到 ~/.zshrc

1
plugins=(extract z)

若提示字体颜色显示不正常,执行

1
$ echo "export TERM=xterm-256color" >> ~/.zshrc

自用 .zshrc 文件备份

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
export ZSH="/Users/morooi/.oh-my-zsh"
ZSH_THEME="ys"
HIST_STAMPS="yyyy-mm-dd"
plugins=(git zsh-completions z zsh-syntax-highlighting zsh-autosuggestions extract)
source $ZSH/oh-my-zsh.sh

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# Brew
alias bs="brew search"
alias bi="brew install"
alias bu="brew rmtree"
alias bup="brew upgrade"
alias bci="brew install --cask"
alias bcup="brew cu -a -f --cleanup"
alias bcu="brew uninstall --cask"
alias cleanbrewcache="rm -rf /Users/morooi/Library/Caches/Homebrew"
export PATH="/usr/local/sbin:$PATH"

# Java
export JAVA_HOME_8="/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home"
export JAVA_HOME_11="/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home"
export JAVA_HOME_13="/Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home"
export JAVA_HOME=$JAVA_HOME_13

alias jdk8="export JAVA_HOME=$JAVA_HOME_8"
alias jdk11="export JAVA_HOME=$JAVA_HOME_11"
alias jdk13="export JAVA_HOME=$JAVA_HOME_13"

# NodeJS
export PATH="/usr/local/opt/node@12/bin:$PATH"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/usr/local/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/usr/local/miniconda3/etc/profile.d/conda.sh" ]; then
. "/usr/local/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/usr/local/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

安装 Zsh,并使用 Oh My Zsh

https://morooi.com/2019/zsh/

作者

SJ Zhou

发布于

2019-07-24

更新于

2021-01-06

许可协议

评论