Gem 選擇 oh-my-zsh 讓 Terminal 更好用

victor · January 04, 2012 · Last by huihen replied at April 25, 2015 · 51073 hits

項目地址 https://github.com/robbyrussell/oh-my-zsh

1 安裝 信春哥得永生,所以直接用作者提供得自動方式安裝

2 配置 命令行下面輸入 mate ~/.zshrc 按照它的用戶手冊簡單修改一下,基本上以我人教英語初中版的英文水平是完全可以看懂的,相信你肯定不弱於我。貼一下我的

# Path to your oh-my-zsh configuration. 一般默認就行
ZSH=$HOME/.oh-my-zsh

# 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. 
# 選用的配色方案,我自己抄了一個
export ZSH_THEME="victor"

# Set to this to use case-sensitive completion
# CASE_SENSITIVE="true"

# Comment this out to disable weekly auto-update checks
# DISABLE_AUTO_UPDATE="true"

# Uncomment following line if you want to disable colors in ls
# DISABLE_LS_COLORS="true"

# Uncomment following line if you want to disable autosetting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment following line if you want disable red dots displayed while waiting for completion
# DISABLE_COMPLETION_WAITING_DOTS="true"

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Example format: plugins=(rails git textmate ruby lighthouse)
# 這裡選用你需要的插件
plugins=(git node brew bundle cap gem github osx rails rails3 ruby rvm svn textmate)

source $ZSH/oh-my-zsh.sh

# 下面導入你的環境變量,注意不要跟 .bash_profile 衝突啦,不然怎麼死的都不知道
# Customize to your needs...
# export PATH=/usr/local/mysql/bin:/opt/local/bin:/opt/local/sbin:/Users/victor/.rvm/gems/ruby-1.8.7-p302/bin:/Users/victor/.rvm/gems/ruby-1.8.7-p302@global/bin:/Users/victor/.rvm/rubies/ruby-1.8.7-p302/bin:/Users/victor/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

# 下面是必須的嘍
# http://episko.posterous.com/brew-zsh-git-and-rvm
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

3 自製 theme 先去 https://wiki.github.com/robbyrussell/oh-my-zsh/themes 看看現存的 theme 選幾個自己需要的功能,做到心中有數 回到命令行下面輸入 mate ~/.oh-my-zsh 在 themes 目錄下新建一個 victor.zsh-theme 好啦,現在需要什麼功能就開抄吧

# Grab the current date (%D) and time (%T) wrapped in {}: {%D %T}
DALLAS_CURRENT_TIME_="%{$fg[white]%}[%{$fg[yellow]%}%T%{$fg[white]%}]%{$reset_color%}"
# Grab the current version of ruby in use (via RVM): [ruby-1.8.7]
DALLAS_CURRENT_RUBY_="%{$fg[white]%}[%{$fg[magenta]%}\$(~/.rvm/bin/rvm-prompt i v g)%{$fg[white]%}]%{$reset_color%}"
# Grab the current machine name: muscato
DALLAS_CURRENT_MACH_="%{$fg[green]%}%m%{$fg[white]%}:%{$reset_color%}"
# Grab the current filepath, use shortcuts: ~/Desktop
# Append the current git branch, if in a git repository: ~aw@master
DALLAS_CURRENT_LOCA_="%{$fg[cyan]%}%~\$(git_prompt_info)%{$reset_color%}"
# Grab the current username: dallas
DALLAS_CURRENT_USER_="%{$fg[red]%}%n%{$fg[white]%}:%{$reset_color%}"
# Use a % for normal users and a # for privelaged (root) users.
DALLAS_PROMPT_CHAR_="%{$fg[white]%}%(!.#.%%)%{$reset_color%}"
# For the git prompt, use a white @ and blue text for the branch name
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[white]%}@%{$fg[blue]%}"
# Close it all off by resetting the color and styles.
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
# Do nothing if the branch is clean (no changes).
ZSH_THEME_GIT_PROMPT_CLEAN=""
# Add 3 cyan ✗s if this branch is diiirrrty! Dirty branch!
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[cyan]%}✗✗✗"

# Put it all together!
PROMPT="$DALLAS_CURRENT_TIME_$DALLAS_CURRENT_RUBY_$DALLAS_CURRENT_USER_$DALLAS_CURRENT_LOCA_ $DALLAS_PROMPT_CHAR_ "

回到命令行新開一個窗口,效果如下,顯示了當前 rvm 的 ruby 和 gemset 版本,並且顯示了當前目錄和用戶,對我來說足夠用了

[23:49][ruby-1.9.2@rails3x]victor:~/Work/Source %

我就 oh-my-zsh,我想换个主题,railscast 他的 textmate 用的字体是什么。谁知道。

zsh 很强大,一直用他

不知道为什么,git status 运行很慢,theme 里如果显示是否是 dirty 就会每次显示 prompt 的时候卡一下。用到最后还是简单点好,https://github.com/doitian/dotfiles/blob/master/_zsh_/theme.zsh 我的很简洁的显示用户,机器名,目录,git,rbenv,return status 和反色的一大块容易分隔开命令

#5 楼 @doitian 我已经退回去用 bash 了,估计是以前用 bash 用多了,对自动补全不怎么依赖,体验了一段 zsh 后,感觉 just not that into it...

#6 楼 @lgn21st #5 楼 @doitian 我怀疑是 zsh 的 git 插件有问题,我也遇到过,我提了个 bug,有人说是 zsh 版本的事。 你们可以试试去掉 git 插件

#6 楼 @lgn21st 有时间改改 bug 多好

#6 楼 @lgn21st 我也是非常讨厌那个自动补全(应该叫智能匹配)

zsh 还是很棒的,oh-my-zsh 过犹不及,git 状态自己写一个更好...

另外显示 git stash 状态:

git_prompt_stash() {
  if [[ -n $(git stash list 2> /dev/null) ]]; then
    echo "%{$fg[cyan]%}$"
  fi
}
RPROMPT="$(git_prompt_stash)"

我表示 oh-my-zsh 很好用啊.... 要说速度是要略微比以前用 bash 慢点点

oh-my-zsh 除了 alias 和 theme 没多少东西... completion 都是用的 zsh 自带的,如果只要 completion 不要 alias, 就得自己把整个插件拷到 custom 里改

#11 楼 @poshboytl 慢应该还是慢在 git status

zsh 是可以完全整成 bash 一样的行为,除了 completion 不通用

貧道覺得也挺好

@night_song alias 很好用呀。

rs = rails server rg = rails generate rdm = rails db:migrate

看来只有我的比较简陋

@camel 就是不喜这种 alias...和我自己的 alias 和函数冲突了...

一直在用,不过从来不用 alias,因为要多记一套东西,要记得东西实在太多了....

#13 楼 @roamlog 一直在用你的 Theme⋯⋯多谢! #22 楼 @pzgz 有些还是挺方便的啊。。。比如

alias work = cd ~/Documents/workspace
alias go   = ~/app/net/goagent/local/proxy.py
alias rake = bundle exec rake

#24 楼 @southwolf

哈哈,我觉得你这个用 alias 来切换目录的主意很棒,非常棒,谢啦

贴一个不用 zsh,而是默认 shell 的 screenshot simple and just enough.

仅仅在.bash_profile里面添加

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\w\$(parse_git_branch) $ "

我还是觉得默认的 theme 最耐看。

#26 楼 @lgn21st 握手,自带的 Terminal+bash 足够使用了,把 iTerm2 和 zsh 拆了。

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\$(parse_git_branch)-$(rvm current)\n\W$ "

#28 楼 @hhuai 我干掉了 zsh,但是 iTerm2 是我的大爱啊,特别是分屏,非常方便,比如开发中要启动多个后台进程,查看 log 信息的时候非常方便,截图给你看看:

#29 楼 @lgn21st 没颜色,看起来不爽

#29 楼 @lgn21st 用 linux 的 awesome wm 的表示路过。

awesome 天生就有你要的这个特性。tiling window manager。单词错了。呵呵

#26 楼 @lgn21st Good script!

试了一下改用 grep

git branch 2> /dev/null | grep -o -P '(?<=^\* ).*$'

速度没太大差别,略快一点点。实际性能开销不是在 grep 或 sed。只不过感觉用两次 sed 命令有些浪费

#29 楼 @lgn21st 这种情况我喜欢新开一个 tab,用快捷键貌似也挺方便的

#29 楼 @lgn21st 哈哈,看来你终端底下的工作比较多。 我需求没那么足,一般开 tab 配合 Finder 和 subl 就够用了。 一直羡慕那个 27 的 imac 啊,可以将 xcode,模拟器和 chrome 平铺起来就好了,切换起来好累啊。

oh-my-shell 是个好东东。一直在用。GUI 终端用 TotalTerminal + iTerm2 , TotalTerminal 屏幕下拉的效果在临时性的命令的时候用。iTerm2 的分屏和多 Tab 很给力。终端的多窗口也可以用 tmux。想分多少分多少屏幕。

Unknow user #36 February 09, 2012

信春哥得永生

#35 楼 @cngump 新版本 iTerm 可以热键出来了,嘿嘿

zsh 的一些 autocomplete 和 bash 不一样,bash 基本是死调,zsh 鼓励你们用缓存,所以在用上 zsh 快,启动上 bash 快。

用 bash 一样也有自动补全,而且暂时不是很想转 zsh 上去

#26 楼 @lgn21st 不错,我也已经放弃 zsh 了,其实只是要显示当前 branch

凑个热闹,bash 改的

#34 楼 @hhuai 一直用 27‘,但现在觉得如果有两个 27’就更好了

皮肤我喜欢 fox

@huacnlee 加到.bashrc 里

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
c_1="\[\e[0m\]"
c0="\[\e[30m\]"
c1="\[\e[31m\033[1m\]"
c2="\[\e[32m\033[1m\]"
c3="\[\e[33m\]"
c4="\[\e[34m\]"
c5="\[\e[35m\]"
c6="\[\e[36m\]"
c7="\[\e[37m\]"
PS1="$c2\W $c3(\$(~/.rvm/bin/rvm-prompt v g)) $c1\$(parse_git_branch) $c_1$ "


#45 楼 @ivice 这个是不 for Mac 的?

俺也用过一段时间的 zsh,可是最后还是回到 bash 了,可能是习惯了吧,bash 自己调教了一下也没觉得有多少不方便了,就这么几条(mac 上的)

  • 用 iTerm2
  • 自己定制一些常用到的 alias
  • 简单定制一下 PS1, 给 ls 加上颜色
  • 把 completion-ignore-case,show-all-if-ambiguous 这些打开,自动补全就差不多了
  • 至于 git,反正命令也就那么几条,再自己定义几个简单的 alias, 就全记住了

大家有什么好的使用方法,请多多补充 :)

最后再贡献个非常好的 mac terminal tips 集合 http://apple.stackexchange.com/questions/5435/what-are-some-tips-or-tricks-for-terminal-in-mac-os-x

#46 楼 @huacnlee for ubuntu 俺还没有 mac

早知有这玩意当初就不用自己配了,这是自己 DIY 的:

请问,ubuntu 系统我安装的时候第一步就出现这样的情况/usr/bin/env: zsh: 没有那个文件或目录?这个怎么弄呢?

#50 楼 @virgil 当然首先是要装 zsh。。。 sudo apt-get install zsh

谢谢哈。。。菜鸟。。

密码:chsh:PAM 验证失败??

#53 楼 @virgil chsh 是切换当前用户的 shell,应该是需要你输入当前用户的密码

@kaka 快来喔,装 zsh 还真不错

#54 楼 @ninehills 恩,不上那个原因,不过解决了。。谢谢。。

zsh 的自动补全,还有 git 插件,很有必要。

#53 楼 @virgil chsh:PAM 验证失败,我也遇到了这个问题,请问这个是怎么解决的啊?

#56 楼 @virgil 这里有人遇到和你一样的问题了

@jack_dls ,你卸载之后重新装一下,具体什么我也忘记了。

#53 楼 @virgil #58 楼 @jack_dls 密码:chsh:PAM 验证失败

chsh -s $(which zsh)
You need to Sign in before reply, if you don't have an account, please Sign up first.