原文地址: Mac-zsh 安装和使用(原创)
首先,我们知道,shell 有很多种:可以通过命令 cat /etc/shells 可以查看系统支持哪些 shell:
# 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/dash/bin/ksh/bin/sh/bin/tcsh
Zsh全称为 Z Shell,因为 Z 是最后一个字母,因此大家称之为——终极 Shell。
Oh My Zsh 安装
Linux/Unix 默认都是使用 Bash(Bourne-again Shell),使用 zsh,由于配置过于复杂,前期很少人使用,但后来有外国程序员弄出一个 Oh My ZSH 来管理 zsh 的配置和支持更多插件,使得 zsh 变得更容易使用和更加强大。
curl 方式
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
wget 方式
wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh
安装完之后,关闭当前终端,并重新打开一个,oh my zsh的默认主题是 robbyrussel。
如下图所示:![[转] Mac-zsh 安装和使用 - 图1](/uploads/projects/duoli@os/f0ac67e8cb5c4d7bf76973fbcf08b210.png)
安装完成之后退出当前会话重新打开一个终端窗口,你就可以见到这个彩色的提示了:
配置
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='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}> '
与原来不同的是,将 c(表示当前目录)改为 d(表示绝对路径),另外在尾部添加一个 > 作为隔离符号,效果如下:
切換 theme
所有的主題都放在 ~/.oh-my-zsh/themes 目录中,先看一下有哪些可以用:
~/ [master*] ls ~/.oh-my-zsh/themes3den.zsh-theme junkfood.zsh-themeSoliah.zsh-theme kafeitu.zsh-themeadben.zsh-theme kardan.zsh-themeaf-magic.zsh-theme kennethreitz.zsh-themeafowler.zsh-theme kiwi.zsh-themeagnoster.zsh-theme kolo.zsh-themealanpeabody.zsh-theme kphoen.zsh-themeamuse.zsh-theme lambda.zsh-themeapple.zsh-theme linuxonly.zsh-themearrow.zsh-theme lukerandall.zsh-themeaussiegeek.zsh-theme macovsky-ruby.zsh-themeavit.zsh-theme macovsky.zsh-themeawesomepanda.zsh-theme maran.zsh-themebira.zsh-theme mgutz.zsh-themeblinks.zsh-theme mh.zsh-themebureau.zsh-theme michelebologna.zsh-themecandy-kingdom.zsh-theme mikeh.zsh-themecandy.zsh-theme miloshadzic.zsh-themeclean.zsh-theme minimal.zsh-themecloud.zsh-theme mira.zsh-themecrcandy.zsh-theme mortalscumbag.zsh-themecrunch.zsh-theme mrtazz.zsh-themecypher.zsh-theme murilasso.zsh-themedallas.zsh-theme muse.zsh-themedarkblood.zsh-theme nanotech.zsh-themedaveverwer.zsh-theme nebirhos.zsh-themedieter.zsh-theme nicoulaj.zsh-theme...
切换方式是修改 .zshrc 的 ZSH_THEME 这个参数,预设是 robbyrussell,如果想改成 apple.zsh-theme,那么请把 ZSH_THEME 改为 apple:
# Set name of the theme to load.# Look in ~/.oh-my-zsh/themes/# Optionally, if you set this to "random", it'll load a random theme each# time that oh-my-zsh is loaded.ZSH_THEME="apple"
这里有一些 theme 的截图,如果不想一个一个來看效果,可以參考看看。
zsh 的改进方法
- 你可以打开
auto_pushd选项(通过命令setopt auto_pushd),这样你通过cd切换目录时,zsh会自动将前一个目录加到栈里,这样你就不会因为忘记pushd而遗憾了; bash里面可以cd -回到上一个目录(即最后一次调用cd时所在的目录),但 zsh 里面有cd -2,cd +3这样的用法,并且在输入cd -之后按TAB能够列出目录名供挑选补全。不过需要注意的是,这里-2并不表示倒数第二次调用cd时的目录,而是倒数第二次通过pushd记录的目录,如果打开了auto_pushd选项,那么这两个的含义倒是一样的;zsh里面将~这个符号的用法进行了扩展,我们可以用hash -d www=/var/www/html定义一个路径别名,然后用cd ~www就可以进入到/var/www/html了。我在配置 XAMPP 的 mysql 中,就用到了hash -d myserver=/Applications/XAMPP/xamppfiles/bin/mysql.server,配置 XAMPP 启动路径为hash -d myxampp=/Applications/XAMPP/xamppfiles/xampp。插件
oh my zsh提供丰富的插件,存放在~/.oh-my-zsh/plugins目录下:![[转] Mac-zsh 安装和使用 - 图4](/uploads/projects/duoli@os/95db9313606020bf90ad1a653acff112.png)
想了解每个插件的功能以及如何使用,只要打开相关插件的目录下zsh文件即可,以git插件为例:![[转] Mac-zsh 安装和使用 - 图5](/uploads/projects/duoli@os/ca54ada462c1eef8973935d5c419eb14.png)
显示如下:![[转] Mac-zsh 安装和使用 - 图6](/uploads/projects/duoli@os/6ca76b4e873b87ffeff790e240177454.png)
打开git.plugin.zsh文件,里面有很多命名别来来简化命令的输入。您可以根据自己的需要来启用哪些插件,只需在~/.zshrc配置文件追加内容即可:plugins=(git autojump osx)
常用插件的使用:
1.git:
当您处在一个
git受控的目录下时,Shell明确显示git和branch信息,另外简化git很多命令,具体使用请参考: Plugin:git2.autojump
autojump智能跳转,通过zsh记录访问过的目录,autojump的简写j + 目录名,随意的在目录间跳转,支持各种模糊匹配、补全,例如之前的cd work;现在可以直接work,或者j wor、j work达到同样的效果,节省大好的时间。
1.安装
如果你是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
注意:关于
zsh: command not found: j报错
请在.zshrc文件相应的位置,找到plugins=()这行,添加autojump记得各插件名之间用英文空格隔开
如下图:![[转] Mac-zsh 安装和使用 - 图7](/uploads/projects/duoli@os/ff4f905f9ec4909af375781ce54b0823.png)
2.使用
我之前打开过我的博客~/octopress/source目录,现在只需敲入j o就可以快速切换到~/ocotpress目录。
如下图:![[转] Mac-zsh 安装和使用 - 图8](/uploads/projects/duoli@os/60ff85b532bca9c956ac4e77c41f7009.png)
利用j –s可以看你的历史路径库。
3.osx
tab- 在一个新标签打开当前目录cdf- cd 到当前 Finder 目录quick-look- 快速浏览特殊的文件man-preview- 在 Preview 应用打开特定的 man pagetrash- 将特定的文件移到垃圾桶
使用:
1.各种补全:除了支持命令补全和文件补全之外,还支持命令参数补全,插件内容补全,只需要按 tab 键
2.目录浏览和跳转:输入 d,就显示在会话里访问的目录列表,输入列表前的序号,即可以跳转![[转] Mac-zsh 安装和使用 - 图10](/uploads/projects/duoli@os/5c20a02342fb8a3731024a25d9a26506.png)
3.输入 .. 可以返回到上级目录
安装 zsh-comopletions
安裝:
$ brew install zsh-completions
![[转] Mac-zsh 安装和使用 - 图12](/uploads/projects/duoli@os/61dfad614309ab4ed52c0ad3ac6feb6b.png)
要启用还需要打开你的 .zshrc 加入以下两行来纳入 zsh-completions 的补全功能:
.zshrc
+# zsh-completions+fpath=(/usr/local/share/zsh-completions $fpath)
同时还需要 rebuild zsh 的 .zcompdump
$ rm -f ~/.zcompdump; compinit
转换过程中可能遇到的一些问题
1.alias
如果你本來就有設定一些 alias 在你的 .bashrc,你又把這些設定直接套用到 .zshrc,那有機會有一些指令會變怪怪的,這有可能是你設定的 alias 與 oh-my-zsh 內建的衝到了。oh-my-zsh 內建的 alias 放在 ~/.oh-my-zsh/lib/aliases.zsh,內容如下:
aliases.zsh
# Push and pop directories on directory stackalias pu='pushd'alias po='popd'# Basic directory operationsalias ...='cd ../..'alias -- -='cd -'# Super useralias _='sudo'alias please='sudo'#alias g='grep -in'# Show historyif [ "$HIST_STAMPS" = "mm/dd/yyyy" ]thenalias history='fc -fl 1'elif [ "$HIST_STAMPS" = "dd.mm.yyyy" ]thenalias history='fc -El 1'elif [ "$HIST_STAMPS" = "yyyy-mm-dd" ]thenalias history='fc -il 1'elsealias history='fc -l 1'fi# List direcory contentsalias lsa='ls -lah'alias l='ls -la'alias ll='ls -l'alias la='ls -lA'alias sl=ls # often screw this upalias afind='ack-grep -il'
建议要在 .zshrc 加上自己的 alias 前先确认看看這個档案裡是不是有一些 alias 会跟你的冲突。
2.bash-completion
如果你跟我一样用 bash 时有装 bash-completion,而且又沒仔細弄清楚就把 .bashrc 的內容一般会套用在 .zshrc 上,那么当你开一个新的 shell 时有可能会发现遇到以下的信息:
.../usr/local/etc/bash_completion:138: command not found: complete/usr/local/etc/bash_completion:141: command not found: complete/usr/local/etc/bash_completion:144: command not found: complete/usr/local/etc/bash_completion:147: command not found: complete/usr/local/etc/bash_completion:150: command not found: complete/usr/local/etc/bash_completion:153: command not found: complete/usr/local/etc/bash_completion:156: command not found: complete/usr/local/etc/bash_completion:159: command not found: complete/usr/local/etc/bash_completion:162: command not found: complete/usr/local/etc/bash_completion:246: parse error near `]]'
这是因为在安裝 bash-completion 的时候,原則上都会加入以下內容在你的 .bashrc:
.bashrc
if [ -f $(brew --prefix)/etc/bash_completion ]; then. $(brew --prefix)/etc/bash_completionfi
以上指令会去 source bash_completion 的內容,而 bash_completion 里面使用了 bash completion buitins 中的 complete 這個指令,所以如果你的 .zshrc 也去 source bash_completion,那就会 show 出像上面的 command not found: complete 这种信息了。所以记得在 .zshrc 中不要加入这段 code。
