Your terminal never felt this good before.

1. 查看已有终端

> cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
/usr/bin/tmux

> chsh -l
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
/usr/bin/tmux

> echo $SHELL
/bin/bash

可以看到 CentOS 7默认终端/bin/bash

2. 安装 zsh

> yum info zsh
Available Packages
Name        : zsh
Arch        : x86_64
Version     : 5.0.2
Release     : 31.el7
Size        : 2.4 M
Repo        : base/7/x86_64
Summary     : Powerful interactive shell
URL         : http://zsh.sourceforge.net/
License     : MIT
Description : The zsh shell is a command interpreter usable as an interactive
            : login shell and as a shell script command processor.  Zsh resembles
            : the ksh shell (the Korn shell), but includes many enhancements.
            : Zsh supports command line editing, built-in spelling correction,
            : programmable command completion, shell functions (with
            : autoloading), a history mechanism, and more.

> yum install -y zsh
> cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
/usr/bin/tmux
/bin/zsh

3. 切换默认 Shell 为 zsh

请在root用户下切换 Shell

> chsh -s /bin/zsh
Changing shell for root.
Shell changed.

提示终端切换完成,但还需重启方能生效。之后继续安装oh-my-zsh

4. 安装 oh-my-zsh

Via curl

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Via wget

sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

提示oh-my-zsh安装成功

         __                                     __   
  ____  / /_     ____ ___  __  __   ____  _____/ /_  
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \ 
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / / 
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  
                        /____/                       ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us at https://twitter.com/ohmyzsh.

p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh.

5. 修改主题及配置

oh-my-zsh默认主题robbyrussell,可以在~/.zshrc修改ZSH_THEME主题字段,主题清单参见 Themes | 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/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster"

另外还可以在~/.zshrc设置PATH路径添加alias命令别名,使用方法与~/.bashrc相同:

> vim ~/.zshrc

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

export GOLANDPATH=$HOME/GoLand-2018.3.5/
export GOPATH=$HOME/go
export PATH="$PATH:$GOPATH/bin:$GOLANDPATH:bin"

# 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,
ZSH_THEME="agnoster"

...
...

# Set list of themes to pick from when loading at random
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# 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 rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'
alias pc='proxychains4'

保存之后更新配置

source ~/.zshrc

例如下图所示即为agnoster-zsh-theme主题效果:

agnoster-zsh-theme
agnoster-zsh-theme

注意:在 CentOS 7 下使用agnoster主题,部分符号在终端无法正常显示,还需安装 Powerline fonts 字体

# clone
git clone https://github.com/powerline/fonts.git --depth=1
# install
cd fonts
./install.sh
# clean-up a bit
cd ..
rm -rf fonts

之后在终端输入以下命令测试Powerline font字体是否成功安装

echo "\ue0b0 \u00b1 \ue0a0 \u27a6 \u2718 \u26a1 \u2699"
字体安装成功后符号可正常显示
字体安装成功后符号可正常显示

6. zsh 小技巧

输入d命令,即可查看在这个终端会话中访问过的目录,输入目录对应的序号即可跳转:

~ > d
0    ~
1    ~/.oh-my-zsh/plugins/git
2    /opt/google
3    ~/.oh-my-zsh
4    ~/.oh-my-zsh/plugins
5    ~/GolandProjects/go-rest-api-server
6    ~/GolandProjects

~ > 6
~/GolandProjects

~/GolandProjects >

另外还可以忽略cd命令,输入.....或目录名都可以跳转。

参考文章

  1. oh my zsh
  2. oh-my-zsh | Github
  3. Themes - oh-my-zsh | Github
  4. oh-my-zsh,让你的终端从未这么爽过 | 简书
  5. CentOS 7 安装 zsh 配置 oh-my-zsh | 简书
  6. oh-my-zsh配置你的zsh提高shell逼格终极选择 | 一介布衣
  7. 使用 oh-my-zsh 的 agnoster theme | Medium.com
  8. 强大的终端工具ohMyZsh | 技术特工队
  9. agnoster.zsh-theme | Github
  10. Powerline fonts | Github