Total Pageviews

Friday 3 June 2016

终极Shell- zsh

目前常用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash,但是真正强大的 Shell 是深藏不露的 zsh,下面我们看看如何安装、配置和使用 zsh。

安装zsh

如果你用 Mac,就可以直接看下一节
如果你用 Redhat Linux,执行:sudo yum install zsh
如果你用 Ubuntu Linux,执行:sudo apt-get install zsh
如果你用 Windows……去洗洗睡吧。
安装完成后设置当前用户使用 zsh:chsh -s /bin/zsh,根据提示输入当前用户的密码就可以了。

安装oh my zsh

首先安装 git,安装方式同上,把 zsh 换成 git 即可。
安装「oh my zsh」可以自动安装也可以手动安装。
自动安装:
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
手动安装:
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
都不复杂,安装完成之后退出当前会话重新打开一个终端窗口,你就可以见到这个彩色的提示了:

配置

zsh 的配置主要集中在用户当前目录的.zshrc里,用 vim 或你喜欢的其他编辑器打开.zshrc,在最下面会发现这么一行字:

Customize to your needs…

可以在此处定义自己的环境变量和别名,当然,oh my zsh 在安装时已经自动读取当前的环境变量并进行了设置,你可以继续追加其他环境变量。
接下来进行别名的设置,我自己的部分配置如下:
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'
alias javac="javac -J-Dfile.encoding=utf8"
alias grep="grep --color=auto"
alias -s html=mate   # 在命令行直接输入后缀为 html 的文件名,会在 TextMate 中打开
alias -s rb=mate     # 在命令行直接输入 ruby 文件,会在 TextMate 中打开
alias -s py=vi       # 在命令行直接输入 python 文件,会用 vim 中打开,以下类似
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'
zsh 的牛粪之处在于不仅可以设置通用别名,还能针对文件类型设置对应的打开程序,比如:
alias -s html=mate,意思就是你在命令行输入 hello.html,zsh会为你自动打开 TextMat 并读取 hello.html; alias -s gz='tar -xzvf',表示自动解压后缀为 gz 的压缩包。
总之,只有想不到,木有做不到,吓尿了吧。
设置完环境变量和别名之后,基本上就可以用了,如果你是个主题控,还可以玩玩 zsh 的主题。在 .zshrc 里找到ZSH_THEME,就可以设置主题了,默认主题是:
ZSH_THEME=”robbyrussell”
oh my zsh 提供了数十种主题,相关文件在~/.oh-my-zsh/themes目录下,你可以随意选择,也可以编辑主题满足自己的变态需求,我采用了默认主题robbyrussell,不过做了一点小小的改动:
PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$reset_color%}>'
#PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'
对照原来的版本,我把 c 改为 d,c 表示当前目录,d 表示绝对路径,另外在末尾增加了一个「 > 」,改完之后的效果是这样的:
大家可以尝试进行改造,也算个趣事。
最后我们来说说插件。

插件

oh my zsh 项目提供了完善的插件体系,相关的文件在~/.oh-my-zsh/plugins目录下,默认提供了100多种,大家可以根据自己的实际学习和工作环境采用,想了解每个插件的功能,只要打开相关目录下的 zsh 文件看一下就知道了。插件也是在.zshrc里配置,找到plugins关键字,你就可以加载自己的插件了,系统默认加载 git ,你可以在后面追加内容,如下:
plugins=(git textmate ruby autojump osx mvn gradle)
下面简单介绍几个:
1、git:当你处于一个 git 受控的目录下时,Shell 会明确显示 「git」和 branch,如上图所示,另外对 git 很多命令进行了简化,例如 gco=’git checkout’、gd=’git diff’、gst=’git status’、g=’git’等等,熟练使用可以大大减少 git 的命令长度,命令内容可以参考~/.oh-my-zsh/plugins/git/git.plugin.zsh
2、textmate:mr可以创建 ruby 的框架项目,tm finename 可以用 textmate 打开指定文件。
3、osx:tab 增强,quick-look filename 可以直接预览文件,man-preview grep 可以生成 grep手册 的pdf 版本等。
4、autojump:zsh 和 autojump 的组合形成了 zsh 下最强悍的插件,今天我们主要说说这货。
首先安装autojump,如果你用 Mac,可以使用 brew 安装:
brew install autojump
如果是 Linux,去下载 autojump 的最新版本,比如:
git clone git://github.com/joelthelion/autojump.git
解压缩后进入目录,执行
./install.py
最后把以下代码加入.zshrc:
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh
至此,安装、配置、插件三位一体,终极 Shell 全面登场。退出终端会话重新登录,开始感受 zsh 的训疾如风!

使用 zsh

1、兼容 bash,原来使用 bash 的兄弟切换过来毫无压力,该咋用咋用。
2、强大的历史纪录功能,输入 grep 然后用上下箭头可以翻阅你执行的所有 grep 命令。
3、智能拼写纠正,输入gtep mactalk * -R,系统会提示:zsh: correct ‘gtep’ to ‘grep’ [nyae]? 比妹纸贴心吧,她们向来都是让你猜的……
4、各种补全:路径补全、命令补全,命令参数补全,插件内容补全等等。触发补全只需要按一下或两下 tab 键,补全项可以使用 ctrl+n/p/f/b上下左右切换。比如你想杀掉 java 的进程,只需要输入 kill java + tab键,如果只有一个 java 进程,zsh 会自动替换为进程的 pid,如果有多个则会出现选择项供你选择。ssh + 空格 + 两个tab键,zsh会列出所有访问过的主机和用户名进行补全
5、智能跳转,安装了autojump之后,zsh 会自动记录你访问过的目录,通过 j + 目录名 可以直接进行目录跳转,而且目录名支持模糊匹配和自动补全,例如你访问过hadoop-1.0.0目录,输入j hado 即可正确跳转。j –stat 可以看你的历史路径库。
6、目录浏览和跳转:输入 d,即可列出你在这个会话里访问的目录列表,输入列表前的序号,即可直接跳转。
7、在当前目录下输入 .. 或 … ,或直接输入当前目录名都可以跳转,你甚至不再需要输入 cd 命令了。
8、通配符搜索:ls -l */.sh,可以递归显示当前目录下的 shell 文件,文件少时可以代替 find,文件太多就歇菜了。
9、更强的别名:请参考配置一节。
10、插件支持:请参考插件一节。
……
看完这篇文章,你就知道,zsh一出,无人再与争锋!终极二字不是盖的.
---------------
oh-my-zsh

A delightful community-driven framework for managing your zsh configuration. Includes 200+ optional plugins (rails, git, OSX, hub, capistrano, brew, ant, php, python, etc), over 140 themes to spice up your morning, and an auto-update tool so that makes it easy to keep up with the latest updates from the community. 

Oh My Zsh is an open source, community-driven framework for managing your zsh configuration. That sounds boring. Let's try this again.
Oh My Zsh is a way of life! Once installed, your terminal prompt will become the talk of the town or your money back! Each time you interact with your command prompt, you'll be able to take advantage of the hundreds of bundled plugins and pretty themes. Strangers will come up to you in cafés and ask you, "that is amazing. are you some sort of genius?" Finally, you'll begin to get the sort of attention that you always felt that you deserved. ...or maybe you'll just use the time that you saved to start flossing more often.
To learn more, visit ohmyz.sh and follow @ohmyzsh on Twitter.

Getting Started

Prerequisites

Disclaimer: Oh My Zsh works best on OS X and Linux.
  • Unix-based operating system (OS X or Linux)
  • Zsh should be installed (v4.3.9 or more recent). If not pre-installed (zsh --version to confirm), check the following instruction here: Installing ZSH
  • curl or wget should be installed
  • git should be installed

Basic Installation

Oh My Zsh is installed by running one of the following commands in your terminal. You can install this via the command-line with either curl or wget.

via curl

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

via wget

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

Using Oh My Zsh

Plugins

Oh My Zsh comes with a shit load of plugins to take advantage of. You can take a look in the plugins directory and/or thewiki to see what's currently available.

Enabling Plugins

If you spot a plugin (or several) that you would like to use with Oh My Zsh, you will need to edit the ~/.zshrc file. Once you open it with your favorite editor, you'll see a spot to list all the plugins that you'd like Oh My Zsh to load in initialization.
For example, this line might begin to look like...
plugins=(git bundler osx rake ruby)

Using Plugins

Most plugins (should! we're working on this) include a README, which documents how to use them.

Themes

We'll admit it. Early in the Oh My Zsh world, we may have gotten a bit too theme happy. We have over one hundred themes now bundled. Most of them have screenshots on the wiki. Check them out!

Selecting a Theme

Robby's theme is the default one. It's not the fanciest one. It's not the simplest one. It's just right (for him).
Once you find a theme that you want to use, you will need to edit the ~/.zshrc file. You'll see an environment variable (all caps) in there that looks like:
ZSH_THEME="robbyrussell"
To use a different theme, simply change the value to match the name of your desired theme. For example:
ZSH_THEME="agnoster" # (this is one of the fancy ones)
Open up a new terminal window and your prompt should look something like...
Agnoster theme
In case you did not find a suitable theme for your needs, please have a look at the wiki for more of them.
If you're feeling feisty, you can let the computer select one randomly for you each time you open a new terminal window.
ZSH_THEME="random" # (...please let it be pie... please be some pie..)

Advanced Topics

If you're the type that likes to get their hands dirty, these sections might resonate.

Advanced Installation

Some users may want to change the default path, or manually install Oh My Zsh.

Custom Directory

The default location is ~/.oh-my-zsh (hidden in your home directory)
If you'd like to change the install directory with the ZSH environment variable, either by running export ZSH=/your/pathbefore installing, or by setting it before the end of the install pipeline like this:
export ZSH="$HOME/.dotfiles/oh-my-zsh"; sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Manual Installation

1. Clone the repository:
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
2. Optionally, backup your existing ~/.zshrc file:
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 included for you.
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
4. Change your default shell
chsh -s /bin/zsh
5. Initialize your new zsh configuration
Once you open up a new terminal window, it should load zsh with Oh My Zsh's configuration.

Installation Problems

If you have any hiccups installing, here are a few common fixes.
  • You might need to modify your PATH in ~/.zshrc if you're not able to find some commands after switching to oh-my-zsh.
  • If you installed manually or changed the install location, check the ZSH environment variable in ~/.zshrc.

Custom Plugins and Themes

If you want to override any of the default behaviors, just add a new file (ending in .zsh) in the custom/ directory.
If you have many functions that go well together, you can put them as a XYZ.plugin.zsh file in the custom/plugins/directory and then enable this plugin.
If you would like to override the functionality of a plugin distributed with Oh My Zsh, create a plugin of the same name in thecustom/plugins/ directory and it will be loaded instead of the one in plugins/.

Getting Updates

By default, you will be prompted to check for upgrades every few weeks. If you would like oh-my-zsh to automatically upgrade itself without prompting you, set the following in your ~/.zshrc:
DISABLE_UPDATE_PROMPT=true
To disable automatic upgrades, set the following in your ~/.zshrc:
DISABLE_AUTO_UPDATE=true

Manual Updates

If you'd like to upgrade at any point in time (maybe someone just released a new plugin and you don't want to wait a week?) you just need to run:
upgrade_oh_my_zsh
Magic!

Uninstalling Oh My Zsh

Oh My Zsh isn't for everyone. We'll miss you, but we want to make this an easy breakup.
If you want to uninstall oh-my-zsh, just run uninstall_oh_my_zsh from the command-line. It will remove itself and revert your previous bash or zsh configuration.

Contributing

I'm far from being a Zsh expert and suspect there are many ways to improve – if you have ideas on how to make the configuration easier to maintain (and faster), don't hesitate to fork and send pull requests!
We also need people to test out pull-requests. So take a look through the open issues and help where you can.
from https://github.com/robbyrussell/oh-my-zsh