zsh-setup

Your terminal never felt this good before.

Install oh-my-zsh

Basic Installation

如果是能联网,直接用curl或者wget拿到install.sh,再安装就好了

via curl

1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

via wget

1
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

####

Manual Installation

如果不能联网,想要手动安装,可以下载库到server,再安装。

1. Clone the repository:
1
git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh
2. Optionally, backup your existing ~/.zshrc file:
1
cp ~/.zshrc ~/.zshrc.orig
3. Create a new zsh configuration file

You can create a new zsh config file by copying the template that we have included for you.

1
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
4. Change your default shell
1
chsh -s $(which zsh)

You must log out from your user session and log back in to see this change.

5. Initialize your new zsh configuration

Once you open up a new terminal window, it should load zsh with Oh My Zsh’s configuration.

Install autojump

从github下载https://github.com/wting/autojump

1
2
> cd autojump-master
> ./install.py

.zshrc里面的plugins添加

1
plugins=(git autojump)

setenv and alias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# for bash: ${!var}
# for zsh: ${(P)${var}}
append () {
export ${1}=${(P)${1}}:${2}
# export ${1}=${!1}:${2}
}

insert () {
export ${1}=${2}:${(P)${1}}
# export ${1}=${2}:${!1}
}

setenv () {
export ${1}=${2}
}
setenv TMPDIR ${HOME}/tmp
insert PATH ${HOME}/etc/bin/

alias g="gvim"

Usage

Basic

  • 智能拼写纠正,输入gtep mactalk * -R,系统会提示:zsh: correct ‘gtep’ to ‘grep’ [nyae]?

    y (yes)

    n (no)

    a (abort)

    e (edit)

  • 各种补全:路径补全、命令补全,命令参数补全,插件内容补全等等。触发补全只需要按一下或两下 tab 键,补全项可以使用 ctrl+n/p/f/b上下左右切换。比如你想杀掉 java 的进程,只需要输入 kill java + tab键,如果只有一个 java 进程,zsh 会自动替换为进程的 pid,如果有多个则会出现选择项供你选择。ssh + 空格 + 两个tab键,zsh会列出所有访问过的主机和用户名进行补全

  • 命令选项补齐,比如输入git,然后按Tab,即可显示出git都有哪些命令

  • 目录一次性补全:比如输入Doc/docTab键会自动变成Documents/document/

  • 目录浏览和跳转:输入 d,即可列出你在这个会话里访问的目录列表,输入列表前的序号,即可直接跳转。

  • 自动cd:只需输入目录的名称即可

  • 在当前目录下输入 .. 或 … ,或直接输入当前目录名都可以跳转,你甚至不再需要输入 cd 命令了。

  • 一旦在 shell 敲入正确命令并能执行后,shell 就会存储你所敲入命令的历史记录(存放在~/.zsh_history 文件中),方便再次运行之前的命令。可以按方向键↑和↓来查看之前执行过的命令

  • 可以用 r来执行上一条命令

  • 使用 ctrl-r 来搜索命令历史记录

autojump

智能跳转,安装了autojump之后,zsh 会自动记录你访问过的目录,通过 j + 目录名 可以直接进行目录跳转,而且目录名支持模糊匹配和自动补全,例如你访问过hadoop-1.0.0目录,输入j hado 即可正确跳转。j –stat 可以看你的历史路径库。

  • Jump To A Directory That Contains foo:

    j foo
    
  • Jump To A Child Directory:

    Sometimes it’s convenient to jump to a child directory (sub-directory of
    current directory) rather than typing out the full name.

    jc bar
    

extract

在plugin里面加上就行了。

1
plugins=(git autojump extract)

这个插件,支持解压一切压缩包,一个x命令搞定,再也不用记命令了。

1
$ x file.tar.gz