19 May 2015

缘起

终于受不了bash了,换到更为强大的zsh以及(on-my-zsh)。

使用zsh的理由:

  1. 兼容bash
  2. 补全模式方便: 强大的命令补全 - ctrl-n/ctrl-p , 命令参数补全 , 目录补全和切换,
  3. 全局和后缀alias
  4. 语法优雅
  5. 插件体系: autojump, git之流
  6. 支持: ls **/*.sh ,据说matz就是因为这个去用zsh的。

注意: 用了几天zsh,发现,功能确实相当的强大,就是,我的rvm ruby版本和gemset功能,时而正常,时而不正常。这尼玛,就比较抓狂了。

安装

zsh: apt-get install zsh

on-my-zsh: wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh

切换: sudo chsh -s /bin/zsh username , 注意,一定要提供username,然后重启一下。

重启了几次,发现,chsh根本不起作用,除了对tty下的。 需要设置gome-termial下的某个选项,让其启动时,运行zsh。结果,gome-termial的菜单没了。坑爹啊,后来发现,这是Ubuntu的系统的问题。

修改配色: PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[black]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

迁移命令

bashrc中的命令配置:

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

# Nodejs环境变量
export NVM_DIR="/home/xiajian/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
eval "$(grunt --completion=bash)"
# set ssh alias ssh
export LD_LIBRARY_PATH='/usr/local/instantclient'
alias s99='ssh root@192.168.1.99'
alias s88='ssh root@192.168.1.88'
alias shutdown='sudo shutdown -h now '
alias ack='ack-grep'

# Just for fun
echo "Did you know that:" ; whatis $(ls /bin | shuf -n 1 )

#Handle some warming about gvim
function ngrok() { /home/xiajian/software/ngrok -authtoken 30YEx/1xB5S9UN/rTRFk $@ ; }

# 全功能的css和js替换函数
function replace() {
  if [ "$1" == "test" ] ; then
    HTML_PATH='/home/xiajian/works/test/tophold-html'
    TH_PATH='/home/xiajian/works/test/tophold-web'
    echo "show the different bewteen"
  else
    HTML_PATH='/home/xiajian/works/html'
    TH_PATH='/home/xiajian/works/web'
  fi
  cp ${HTML_PATH}/css/*.css ${TH_PATH}/app/assets/stylesheets/.
  cp ${HTML_PATH}/js/*.js ${TH_PATH}/app/assets/javascripts/.
  cp ${HTML_PATH}/bootstrap/*.css ${TH_PATH}/app/assets/stylesheets/.
  cp ${HTML_PATH}/bootstrap/*.js ${TH_PATH}/app/assets/javascripts/.
  cp ${HTML_PATH}/datepicker/*.css ${TH_PATH}/app/assets/stylesheets/.
  cp ${HTML_PATH}/datepicker/*.js ${TH_PATH}/app/assets/javascripts/.
  cp -rf ${HTML_PATH}/images/* ${TH_PATH}/app/assets/images/.
  cp -rf ${HTML_PATH}/email/images/* ${TH_PATH}/app/assets/images/email/.
  # sed 流编辑器的批量处理文件的能力还是值得称赞的,这一点vim,也已可以做到,不过不太熟悉非交互式的vim就是。
  sed -i 's/\.\.\/images/\/assets/' ${TH_PATH}/app/assets/stylesheets/*.css
  # unix2mac ${TH_PATH}/app/assets/stylesheets/*.css
  # 禁用比较的原因是,diff之后的输入比较的杂
  # echo "show some change in cp and sed" 
  # for file in ${TH_PATH}/app/assets/stylesheets/*.css  ; do
  #   file1=${HTML_PATH}/css/$(basename $file)
  # for file in ${TH_PATH}/app/assets/stylesheets/*.css  ; do
  #   file1=${HTML_PATH}/css/$(basename $file)
  #   echo "----show different between $file and $file1"
  #   diff $file $file1
  # done
  # 这里rm的文件是由于上面全盘复制时,引入的无用的文件,所以需要删除。
  rm ${TH_PATH}/app/assets/javascripts/jquery-1.8.3.min.js
  rm ${TH_PATH}/app/assets/javascripts/jquery*
  rm ${TH_PATH}/app/assets/javascripts/bootstrap.min.js
  rm ${TH_PATH}/app/assets/javascripts/date.js
  rm ${TH_PATH}/app/assets/javascripts/bootstrap-modal-extend.js
}

# 在全部的四个项目中查找某个
function gfind() {
  if [  $# ==  0  ] ; then
    echo "Usage: gfind函数需要一个参数"
    return
  fi
  dir=("/home/xiajian/works/engine" "/home/xiajian/works/bjobs" "/home/xiajian/works/mobile" /home/xiajian/works/mobile_ios /home/xiajian/works/web)
  for d in ${dir[*]}; do 
    cd $d
    echo "Find $@ in "$(pwd)
    git grep $@
  done
}

# 自写自用的jekyll函数 - js , 经验是千万别写与命令同名的函数,会挂的
function js() {
  if [  $# ==  0  ] ; then
     echo "默认执行"
     jekyll serve -w
  else
     jekyll "$@"
  fi
}

# git-one:添加并提交
function git-one() {
  if [ $# == 0 ] ; then
    echo "Usage: git-one [提交说明]"
    git st
  else
    echo "提交"$(pwd)"下的修改"
    git add -A $(pwd) && git ci -m"$1" && git push
  fi
}

# 显示并打开特定的gem包
function gem-show() {
  if [ $# == 0 ] ; then
    echo "描述: gem-show是用来编辑特定gem包的函数"
    echo "Usage: gem-show [gem name]"
  else
    if [[  $(bundle show $1) =~ "Could not" ]] ; then
      echo "当前环境中不包含$1"
    else
      cd $(bundle show $1)
      vi
    fi
  fi
}

# cap部署函数, 获得经验,不要取使用rvm安装获得的gem包的命令行工具同名的函数
# 这样会引起冲突,并使得bash进程崩溃。估计,gem包提供的命令也是以函数的形式存在的
function deploy() {
  if [ $# == 0 ] ; then
    echo "cap函数,默认部署staging"
    cap staging deploy
  else
    cap "$1" deploy
  fi
}

function cc() {
  if [ $# == 1 ] ; then
    echo "=== 将"$1"繁转简"
    opencc -i $1 -c zht2zhs.ini
  else
    opencc -i $1 -o $2 -c zht2zhs.ini
  fi
}

# python启动本地服务器的命令是: python -m SimpleHTTPServer 8888
function ruby-local-server(){
  if [ $# == 0 ] ; then
    port=8080
  else
    port=$1
  fi
  echo "启动本地server: http://localhost:"$port"/"
  ruby -run -e httpd . -p $port
}

function db() {
  mysql -ulodestone  -ptophold_3306 -h192.168.1.88
}

# 编写YUI的辅助函数
function yui() {
  yui_dir=/home/xiajian/software/yuicompressor/build/yuicompressor-2.4.8.jar
  java -jar ${yui_dir}  "$@"
}

# add path for zed
# export PATH= ${PATH}:$HOME/software/zed
# 写了几个别名,这样可以少敲点字 - 别名控
# 用了zsh之后,发现,通过autojump之类的程序,可以方便的在目录间跳转
work_space=/home/xiajian/works/
alias web='cd ${work_space}web'
alias engine='cd ${work_space}engine'
alias bjobs='cd ${work_space}bjobs'
alias html='cd ${work_space}html'
alias wind='cd ${work_space}windtalker'
alias cywin='cd ${work_space}cywin'
alias tst='cd ${work_space}test'
alias blog='cd ${work_space}blog'
alias wblog='cd ${work_space}wblog'
alias cpanel='cd ${work_space}cpanel'
alias gitlab='cd ${work_space}gitlab-ce'
alias redmine='cd ${work_space}redmine'
alias rhg-zh='cd ${work_space}rhg-zh'
alias rbp-zh='cd ${work_space}rbp-zh'
alias mongoid-zh="cd ${work_space}mongoid-zh"
alias grape='cd ${work_space}grape'
alias rack='cd ${work_space}rack'
alias h5bp='cd ${work_space}h5bp'
alias githuber='cd ${work_space}githuber'
alias amazeui='cd ${work_space}amazeui'
alias disc='cd ${work_space}discourse'
alias emapi='cd /home/xiajian/works/EMAPI'
alias emf='cd /home/xiajian/works/EMFramework'
alias rguide='cd /home/xiajian/works/rails_guides'
alias vbundle='cd /home/xiajian/.vim/bundle'
alias down='cd /home/xiajian/Downloads'
alias brc='vi ~/.bashrc'
alias vrc='vi ~/.vimrc'
alias rc='rails c'
alias rs='rails s'
alias redis='redis-cli'
alias sb='source ~/.bashrc'
alias ggrep='git grep'
alias gdiff='git diff'
alias img='sshfs root@192.168.1.99:/web/staging/th/current/public/uploads/ ${web}/public/uploads'
# 网上说:卸载远程文件系统可以使用umount和fusermount,前者不起作用,后者用来卸载fufs(用户空间的文件系统)
alias unimg='fusermount -u ${web}/public/uploads'
alias mlog='tail -f /var/log/mongodb/mongodb.log'
alias mdata='cd /var/lib/mongodb/'
alias mdb='mongo --host 192.168.1.88'

介绍

Oh-my-zsh的目录结构:

▸ cache/ - 不知干吗用的
▸ custom/ - 存放定制化的zsh插件, 插件的命名格式为 `xxx.plugin.zsh`
▸ lib/ - zsh中使用的库文件
▸ log/ - 大概是程序的日志之类的
▸ plugins/ - 强大的zsh的插件
▸ templates/ - zsh的配置模板
▸ themes/ - zsh配置的模板
▸ tools/ - oh-my-zsh 相关的工具
  MIT-LICENSE.txt 
  oh-my-zsh.sh
  README.markdown

设置插件: plugins=(git bundler rake ruby coffee rails rvm nvm zsh-syntax-highlighting)

据说,插件加多了,zsh会很慢的。此外,有两个仿照fish的中zsh插件,zsh-syntax-highlightingzsh-autosuggestions

对rvm的支持,需要在.zshrc文件中,添加如下的两行文件:

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
export PATH="$PATH:$HOME/.rvm/bin"

自从用上了zsh,升级了rvm,总是弹出如下的警告,WTF!! Warning! PATH is not properly set up, ‘/home/xiajian/.rvm/gems/ruby-1.9.3-p551@tophold/bin’ is not at first place,ders / archives and other
usually this is caused by shell initialization files - check them for ‘PATH=…’ entries,
it might also help to re-add RVM to your dotfiles: ‘rvm get stable –auto-dotfiles’,
to fix temporarily in this shell session run: ‘rvm use ruby-1.9.3-p551@tophold’.
此外,ruby version 和 gemset 功能也经常不太正常。还有就是zsh的进程模型感觉有些怪,有时有些东西是共享。但环境变量这部分,感觉到各种奇葩。

compctl命令: 基于上下文对文件名的完成进行过滤。

小技巧: z/j, d , ls - alt+L , zsh_stats

更多内容,参考 官方wiki

兼容 bash 这话一点坑爹,bash 函数中使用 [ ] 的表达式,都需要替换成 [[ ]],不然不能正常的工作。

后记

折腾了几个小时(将近一个上午),终于搞好了。对于这种日用软件,我觉得,慢慢来,一口吃不下。

自从,我换了zsh之后,干事就各种不顺利。比如,我的firefox中vim最近总是不能正常工作;RVM的gemset都bundle了好几次了(每次都很慢)。

参考资料

  1. 终极 Shell——ZSH
  2. 使用 Zsh 的九个理由
  3. 那些我希望在一开始使用 Zsh(oh-my-zsh) 时就知道的



傲娇的使用Disqus