众所周知,大部分的Linux发行版的默认命令解释器是Bash。但除此之外,这个星球上的命令解释器多了去了。

这里是几个常用的命令解释器:

Sh
Bash
Zsh
Csh
Ash
Fish

PART 0 → 为什么要用 Zsh

相比于默认的 Bash,Zsh 有更多的自定义选项,并支持扩展。因此 Zsh 可以实现更强大的命令补全,命令高亮等一系列酷炫功能

不过代价就是启动速度稍微有点慢(1秒左右,不过相比Bash还是慢了点),而且据说语法和标准 Shell 有点区别(至少我是在使用中完全没有碰到兼容性问题)。因此现在几乎所有的 Linux / Unix 版本的默认 Shell 仍然是 Bash。

网上也有人用 Fish 的,不过据说 Fish 和 Bash 的语法有点差异,所以我没试过。

PART 1 → oh-my-zsh

默认的 Zsh 配置有点麻烦。因此一个叫 robbyrussel 的用户在 GitHub 上制作了一个配置文件 oh-my-zsh,这是目前为止最流行的 Zsh 配置:

oh-my-zsh 项目地址(不要吝啬你的 Star)

如果你不想看官方的安装说明,请看这里:

第一步 → 把 oh-my-zsh 项目 Clone 下来:

git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

第二步 → 复制 .zshrc

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

第三步 → 更改你的默认 Shell

chsh -s /bin/zsh

现在,你会看到你的命令提示符变了:

v2-fcc7b595fa9cb301ccc101690d7f8450_720w.jpg

酷吧,其实还不只于此。

PART 2 → 主题配置

你可能会觉得现在的主题已经超棒了,但是记得吗,我说过Zsh支持很多个性化配置和插件。现在,编辑 ~/.zshrc 文件(比如用 Vim 或者 VSCode),我们来尝试一下各种主题:

v2-fcc7b595fa9cb301ccc101690d7f8450_720w.jpg

你会看到有一行教ZSH_THEME="robbyrussell"的脚本,把它替换成ZSH_THEME="agnoster"。然后回到终端,输入 source ~/.zshrc,你会发现你的Zsh主题变了

v2-4e8a9514e73959314e4ffd3215831318_720w.jpg

现在你的主题名称是Agnoster,如果你觉得不太好看,你可以改。前往 oh-my-zsh 的 Wiki 就可以看到大多数 oh-my-zsh 的内置主题以及它们的截图。如果你看中的其中的一款,可以重复上面的步骤,编辑~/.zshrc,并更改ZSH_THEME="xxx"。

比如你看中了 ys 主题(我的最爱),你可以把 ZSH_THEME 改成 "ys",然后回到终端,执行 source ~/.zshrc 命令。

P.S. 这些主题都保存在 "~/.oh-my-zsh/themes" 目录中

PART 3 → 插件

oh-my-zsh 的自带插件都储存在 "~/.oh-my-zsh/plugins" 目录中,如果你希望安装一个插件,可以在 "~/.zshrc" 的 plugins=(xxx, xxx, ...) 这一行里加入插件名称

v2-8b36102ab7c9af468f11f7697cc0547d_720w (1).jpg

如果你需要安装第三方插件和主题,你可以在 "~/.zshrc" 的某一行(比如末尾)加入 source /path/to/plugin

比如你希望添加一个 zsh-syntax-highlighting 插件 ↓

source ~/.zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# 这里要根据你自己的输入目录,而不是抄我的,否则然并卵

最后给大家看一下我自己的 Zsh

v2-04236671dcde0e383a39bb7c674b5097_720w.jpg

# 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=$HOME/.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="robbyrussell"
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 one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 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.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# 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"

# 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)

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"

alias cls='clear'
alias ll='ls -lha'
alias la='ls -a'
alias vi='vim'
alias javac="javac -J-Dfile.encoding=utf8"
alias grep="grep --color=auto"
alias -s html=mate   # 在命令行直接输入后缀为 html 的文件名,会在 TextMate 中打开
alias -s rb=mate     # 在命令行直接输入 ruby 文件,会在 TextMate 中打开
alias -s py=vi       # 在命令行直接输入 python 文件,会用 vim 中打开,以下类似
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

# PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$reset_color%}>'

PROMPT='%{$fg_bold[red]%}%n@%m:%{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$reset_color%}>'

setopt nonomatch