oh-my-zsh让终端好用到飞起~

54,739 阅读1分钟

zsh

TL;DR

  • 安装zshbrew install zsh zsh-completions
  • 切换到zsh[sudo] chsh -s $(which zsh)
  • 安装oh-my-zshgit clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
  • 修改主题 ~/.zshrc里的ZSH_THEME="ys"
  • 安装autojump、zsh-autosuggestions、zsh-syntax-highlighting三个插件 brew install autojump;git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions;git clone git://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
  • 别忘了在~/.zshrc找到plugins=添加下面的,最后保存执行source ~/.zshrc
plugins=(
  autojump
  zsh-autosuggestions
  zsh-syntax-highlighting
)

oh-my-zsh简介

目前常用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash。「oh my zsh」是强化的的 Shell 。

使用步骤

1.安装zsh

# Linux
sudo yum install zsh    (Fedora和RedHat以及SUSE中)或
sudo apt-get install zsh    (Debian系列,Ubuntu )

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

# 或者也可以使用MacPorts(包管理工具)
sudo port install zsh zsh-completions

2.更改默认shell

# 把zsh设为默认shell,如果shell列表中没有zsh或者你没有使用chsh权限的时候,不起作用
echo $SHELL    
[sudo] chsh -s $(which zsh)  或 chsh -s /bin/zsh

3.安装 oh my zsh

# 安装 oh my zsh 之前必须安装 zsh,否则会收到如下提示:Zsh is not installed! Please install zsh first!
# 方法一:官网上的方法,但需要安装wget或者curl。wget,用来从指定的 URL 下载文件。curl,发出网络请求,然后得到和提取数据。
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

# 方法二:当然也可以通过git下载 ,我觉得git最亲切 哈哈哈哈
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

4.配置主题

# Oh-My-Zsh 的默认配置文件在:~/.zshrc。编辑~/.zshrc 修改主题,这里我用的是 ys 主题,更多主体看[这里](https://github.com/robbyrussell/oh-my-zsh/wiki/Themes),直接修改即可,无需下载
ZSH_THEME="ys"
# !!!! 重启终端后有效 或 使用 source ~/.zshrc 更新配置

# 题外话,不太想显示主机名,所以直接干掉主机名
# 编辑 ~/.oh-my-zsh/themes/ys.zsh-theme,最后面改成这样
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%}"

zsh_ui

oh-my-zsh 插件推荐

插件依然需要打开~/.zshrc,找到plugins=,然后在里面写需要的插件名,有些插件可能还需要安装。 !!!! 注意,只要改了此文件,重启终端后有效 或 使用 source ~/.zshrc 更新配置。

这边插件推荐 : autojump、zsh-autosuggestion 以及 zsh-syntax-highlighting。

autojump

功能:实现目录间快速跳转,想去哪个目录直接 j + 目录名,不用在频繁的 cd 了! github地址

history | grep "git clone"这个命令就能找到近期 clone 了哪些库,省却了写一堆代码的功夫。

autojump 就是通过记录你在 history 中的行为把你访问过的文件夹路径都 cache 下来,当你输入路径名的时候会模糊匹配你之前cd过的目录路径,配合后面的自动提示插件,无敌了!!!

如下图,我先cd到一些目录,然后就可以j快速切换,用jo快递在finder里打开文件夹 autojump autojump

# 安装步骤

# ------ mac -------
brew install autojump
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(autojump)
# 在文件末尾添加
[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh
source $ZSH/oh-my-zsh.sh
# 最后
source ~/.zshrc



# ------ linux -----
git clone git://github.com/joelthelion/autojump.git
cd autojump
./install.py
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(autojump)
# 在文件末尾添加
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh
source ~/.zshrc

zsh-autosuggestion

如图所示,输入命令时可提示自动补全(灰色部分),然后按键盘 → (!!!!上下左右的右键,不是tab键)即可补全 autosuggestion

# 安装
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(
  autojump
  zsh-autosuggestions
)
source ~/.zshrc

zsh-syntax-highlighting

日常用的命令会高亮显示,命令错误显示红色 highlighting

# 安装
git clone git://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
vim ~/.zshrc
# 在文件里找到plugins,添加
plugins=(
  autojump
  zsh-autosuggestions
  zsh-syntax-highlighting
)
source ~/.zshrc

引用文章