折腾oh-my-zsh

不管是在 Mac 还是 Linux 终端,oh-my-zsh 都成了终端的标配,安装的时候以为很简单,网上也有很多教程,但最终还是折腾了一些时间,下面记录一下:

首先查看系统有几种shell

$ cat /etc/shells

一般系统默认的都是 bash

查看系统当前shell

echo $SHELL

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

$ yum install zsh	# Linux 上,或 apt-get
$ brew install zsh	# mac电脑上

然后设置zsh为默认 bash
$ chsh -s /bin/zsh

安装 oh my zsh

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

安装补全插件 zsh-autosuggestions

git clone git://github.com/zsh-users/zsh-autosuggestions /root/.zsh/zsh-autosuggestions

安装颜色高亮插件

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/plugins/zsh-syntax-highlighting

个人感觉这个插件不是很有必要,会在终端的有效目录下添加下划线,影响美观

安装 zsh-completions

git clone https://github.com/zsh-users/zsh-completions.git ~/.oh-my-zsh/plugins/zsh-completions

安装autojump

yum -y install autojump-zsh

然后需要把以下命令加到 ~/.zshrc

[ -f /usr/local/etc/profile.d/autojump.sh ] && . /usr/local/etc/profile.d/autojump.sh

配置 /root/.zshrc

可以从/root/.oh-my-zsh/template/zshrc.zsh-template 拷一份

cp /root/.oh-my-zsh/template/zshrc.zsh-template /root/.zshrc

以下是完整配置:

# 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/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="robbyrussell"

# 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 ~/.oh-my-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 change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

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

# 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 ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
  autojump
  history 
  zsh-syntax-highlighting
  # zsh-autosuggestions
  zsh-completions
  # vim-mode
  extract
)

export LC_ALL=en_US.UTF-8  
export LANG=en_US.UTF-8                                                                                                                                                     
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh

# autojump env
[ -f /usr/local/etc/profile.d/autojump.sh ] && . /usr/local/etc/profile.d/autojump.sh

# bindkey ',' autosuggest-accept

__git_files () { 
    _wanted files expl 'local files' _files     
}

source $ZSH/oh-my-zsh.sh
unsetopt share_history

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

# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"

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

export TERM=xterm-256color   #声明终端类型
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=247'   #设置建议命令的文字颜色

最后两行的设置很重要,不然终端的建议命令字体不会变成灰色。

Tips

Mac 电脑下如果 source ~/.zshrc 保错 '-bash: autoload: command not found' ,则需要先 brew install zsh 安装 zsh,然后输入 zsh 命令切换一下。

 


关注微信公众账号「曹当家的」,订阅最新文章推送

Table of Contents