iTerm2 与 Zsh

俗话说“工欲善其事,必先利其器”,作为一个长期在命令行下面工作的人,Mac那个自带的Terminal功能太弱了一些,长此用下去,必定是不能忍的。于是,终于轮到此篇的主角上场了:iTerm2 和 Zsh, 这次,我们来一场iTerm2和Zsh的体验之旅……

iTerm and Zsh

iTerm2

iTerm2是一款完全免费的,专为 Mac OS 用户打造的命令行应用,当然,也是程序员们的最爱。iTerm2本身支持很多自定义化的设置,比如自定义配色,自定义快捷键,方便的水平和垂直分屏功能,等等…… 毫不避讳的讲,说它是如今Mac下最好的终端工具也不为过。当然,日常工作与开发中,用它替代Mac自带的Terminal也是不二的选择。

有了终端利器iTerm2,当然还得配上一款顺手的shell。众所周知,如今用得最广泛的默认shell,就是bash了,因为它也是很多Linux发行版默认带的shell。作为默认自带的shell,其特点,也就是:特点不是很明显 -_-# 不太方便的地方是:可定制性和可扩展性有限,自动补全功能不够强大…… 所以,既然用上了iTerm2,还需要一款更为强大的shell来配合。首当推荐的就是Zsh了。

Zsh


之所以叫Zsh,其含义是:The last shell you’ll ever need!可以理解为,一旦用上它,就别无所求了。跟Bash相比,Zsh的补全功能强大了许多,可以自动补全命令、参数、文件名、进程、用户名、变量、权限符,等等…… 另外,还支持插件,通过插件又可以扩展出许多功能来。

既然有了Zsh,那Oh My Zsh又是啥呢?因为 zsh 的默认配置极其复杂繁琐,让人望而却步,直到有了Oh My Zsh这个开源项目,让zsh配置降到0门槛。而且它完全兼容 bash 。所以,我们可以理解为Oh My Zsh是一个方便你配置和使用Zsh的一个开源工具。掌握了这些姿势,随后我们可以开始动手配置我们逼格满满的终端环境了……

安装 iTerm2

安装iTerm2比较简单,直接从官网下载安装即可。安装好之后,我们还需要进行一系列的设置才行。

iTerm2 主题

iTerm2支持许多的主题配色,可以自己定义,也可以参考网上现成的主题配色。我个人比较喜欢Solarized配色,因为可以配合Vim 里面的Solarized主题。

推荐一些常用的主题配色的预设置文件下载地址:

这些配色预设置文件,可以直接导入到iTerm2中,然后直接在设置中选择即可

iTerm2

iTerm2 字体

为了终端下能正确的显示fancy字符,需要安装powerline字体,这样fancy字符不至于显示为乱码。 GitHub上已经有制作好的Powerline字体,下载直接安装到系统即可。安装好之后,就可以选择一款你喜欢的Powerline字体了
路径:Preferences -> Profiles -> Text -> Font -> Change Font

安装 Zsh

Mac OS默认应该自带了zsh了,安装之前可以确认一下。

  cat /etc/shells
  # List of acceptable shells for chpass(1).
  # Ftpd will not allow users to connect who are not using
  # one of these shells.
  /bin/bash
  /bin/csh
  /bin/ksh
  /bin/sh
  /bin/tcsh
  /bin/zsh

# 有`/bin/zsh`表明已经预装zsh了,没有的话,可以通过brew命令来安装。
brew install zsh

接下来,我们需要安装的是OhMyZsh

安装Oh-My-Zsh

OhMyZsh的安装也比较简单,官网提供一键安装脚本。默认安装过后,使用的是robbyrussell主题。

# 安装前把git curl命令给打上
brew install git curl
# 安装Oh-My-Zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Oh-My-Zsh 主题


官网提供了多款主题在里面找到一款你喜欢的主题,把主题文件下载到 ~/.oh-my-zsh/themes 里面,然后修改 ~/.zshrc 文件里的ZSH_THEME项,把主题名字填入即可

......
ZSH_THEME="lambda-mod"
......

待配置妥当,看起来应该是这样的:

OhMyZsh

Zsh 黑科技

路径别名

需要在配置文件(通常是 .zshrc)中先定义好规则,形如:
hash -d sortcut_name=path 配置应用后,直接使用 ~ sortcut_name (甚至直接输 sortcut_name 回车就行了)就可以跳转到相应目录

命令别名

通过在.zshrc中配置alias,可以方便的为其他的命令设置别名,这是个很不错的功能。因为我懒,平时甚至连敲命令也想偷懒,于是,我设置了很多alias。
比如跟git相关的:

alias gs="git status"
alias gsm="git summary"
alias ga='git add'
alias gd='git diff'
alias gf='git fetch'
alias grv='git remote -v'
alias grb='git rebase'
alias gbr='git branch'
alias gpl="git pull"
alias gps="git push"
alias gco="git checkout"
alias gl="git log"
alias gc="git commit -m"
alias gm="git merge"

比如,要push提交到远程git仓库的时候,就不必老老实实的输入 git push origin master 了,只需要输入 gps origin master 即可。于是 git pull 也简化成了 gpl , git commit -m 也简化成了 gc

切换目录

在Zsh中,可以通过输入 .号来跳转,比如:

直接输入.. 和 … 可以快速切换到上层和上上层目录
直接输入n+1个点,可以往上层跳转 n 层

进程id补全

Zsh的补全功能非常不错,除了一般的目录和文件名补全,还可以自动补全进程ID。比如,我们通常要kill掉一个进程,得先用 ps -aux|grep process_name 先拿到进程id,然后再 kill pid 来终止掉一个进程。在Zsh中可以直接这样:
Zsh

快速跳转

Zsh支持目录的快速跳转,我们可以使用 d 这个命令,列出最近访问过的各个目录,然后选择目录前面的数字进行快速跳转:
Zsh

目录名简写与补全

如果确切的知道我们要进入某一层目录,但是目录名比较长,没关系,Zsh帮你搞定! 比如我们要进入到 ~/workspace/src/dict,我们只需要输入每个目录的首字母就行,然后再TAB键补全,Zsh会帮你搞定剩下的:
Zsh

常用命令参数补全

Zsh在行的,不光是目录名的补全,嗯,常用的命令参数,它也能给你提供参考。重复上一条命令,输入 r ,可以很便捷的重复执行上一条命令。

Zsh

Zsh 常用插件

Zsh支持插件,通过插件扩展可以实现许多方便的功能。这里介绍一下我常用的几个Zsh插件

autojump

autojump也是一个目录直接快速跳转的效率工具,它会自动记录之前访问过的目录,并计算权重。用法也很简单 j directory_name 即可。比如我要访问 ~/workspace/src ,只需要输入 j src 就行了……

安装 autojump

# 安装autojump
brew install autojump

sublime

可以方便的在Zsh终端中调用Sublime Text打开文件,比如输入 st README.md 就可以调用机器上安装的Sublime Text打开当前目录的README.md文件进行编辑操作。

web-search

一个方便的终端搜索工具,支持大多常用的搜索引擎,比如:

输入 baidu hhkb pro2 直接在浏览器打开百度搜索关键字”hhkb pro2”
输入 google minila air 直接在浏览器打开Google搜索关键字”minila air”

通过查看这个插件的代码,可以看到支持的搜索引擎,包括baidu, google, bing, yahoo 等等…… 当然,还有广大程序化最爱的GitHub哦!

zsh-autosuggestions

zsh-autosuggestions是一个自动提示插件,是个很有意思也很高效的插件。能记录平时你输入过的命令,下次再输入的时候,它会提前提示你,方便懒人。如果是你需要的命令,直接 Ctrl+F 搞定,来直接看看效果吧:
Zsh

安装zsh-autosuggestions

# 安装zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/zsh-autosuggestions
# 激活zsh-autosuggestions(注意自己安装的路径)
source ~/.oh-my-zsh/zsh-autosuggestions/zsh-autosuggestions.zsh

zsh-syntax-highlighting

zsh-syntax-highlighting是一个代码高亮插件。当命令在zsh提示符下键入到交互式终端时,它可以突出显示命令。这有助于在运行命令之前对其进行检查,尤其是在捕获语法错误时。

安装zsh-syntax-highlighting

# 安装zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/zsh-syntax-highlighting
# 激活zsh-syntax-highlighting(注意自己安装的路径)
source ~/.oh-my-zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

extract

一个功能强大的解压插件,所有类型的文件解压通过一个命令x全搞定,再也不需要去记tar后面到底是哪几个参数了。

比如: x backup.tar.gz 或者 x temp.zip

iTerm 背景特技

iTerm还支持显示背景图,这样我们还可以把单调的终端界面配置得更加酷炫,比如可以这样:
iTerm
还可以这样:
iTerm
配置方法:Preferences -> Profiles -> Window -> Background Image
选择一个你喜欢的背景,然后通过Blending调整图片背景的透明度。

.zshrc文件

这是我的.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="/Users/july/.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="ys"

# 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 automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=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.
# 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=(gitfast autojump sublime web-search extract)

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`.
#
eval "$(thefuck --alias)"
alias y="ydict"
alias tmux="tmux -2"
alias ssh="ssh -X"
alias s="ssh -X"
alias md="mkdir -p"
alias rd="rmdir"
alias df="df -h"
alias mv="mv -i"
alias slink="link -s"
alias l="ls -l"
alias la="ls -a"
alias ll="ls -la"
alias lt="ls -lhtrF"
alias l.="ls -lhtrdF .*"
alias grep="grep --color=auto"
alias cd..="cd .."
alias cd...="cd ../.."
alias cd....="cd ../../.."
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias zb="cat /dev/urandom | hexdump -C | grep --color=auto \"ca fe\""
alias mtr="/usr/local/bin/mtr"
alias gs="git status"
alias gsm="git summary"
alias ga='git add'
alias gd='git diff'
alias gf='git fetch'
alias grv='git remote -v'
alias grb='git rebase'
alias gbr='git branch'
alias gpl="git pull"
alias gps="git push"
alias gco="git checkout"
alias gl="git log"
alias gc="git commit -m"
alias gm="git merge"
alias pro="proxychains4"
alias gb="go build"

#For docker
alias dm="docker-machine"
alias di="docker images"
alias dps="docker ps"
alias dsp="docker stop"
alias ds="docker start"
alias dl="docker logs --tail=50"
alias drm="docker rm"
alias drmi="docker rmi $(docker images --filter "dangling=true" -q --no-trunc)"
alias kc="kubectl"

alias -s go=vi
alias -s html=vi
alias -s rb=vi
alias -s py=vi
alias -s txt=vi
alias -s ex=vi
alias -s exs=vi
alias -s js=vi
alias -s json=vi

# alias for proxy
alias proxy="export ALL_PROXY=socks5://127.0.0.1:1080"
alias unproxy="unset ALL_PROXY"
alias ip="curl -4 ip.sb"
alias ipv6="curl -6 ip.sb"

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

# Load zsh-autosuggestions.
source ~/.oh-my-zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
# Load zsh-syntax-highlighting.
source ~/.oh-my-zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

完结

iTerm和Zsh的配置就暂且介绍到这里吧,其实介绍到的也只是它们功能中我比较常用的,还有更多的功能等着你去发现。有了这些称手的工具和配置,我们就可以更加高效的去完成日常的编码和运维工作了。当然,此篇文章也不会就此为止,平常使用的过程中如果有新的心得和体会,我会持续更新……

文章参考:iTimothy

Last modification:September 7th, 2020 at 07:56 pm
If you think my article is useful to you, please feel free to appreciate