统一设置rails2和rails3的常用命令快捷键

Saturday, July 17, 2010

我知道很多的开发者(包括我), 通过Bash的alias来给一些常用的命令取别名。 比如 用ss 来表script/server, sc表示
script/console, sg 表示script/generate 把下面的代码添加到Bash的配置文件中,例如
~/.bash_login:

alias ss='script/server'
alias sc='script/console'
alias sg='script/generate'

挺方便的,到了Rails 3, 这些命令就换成rails命令来控制,rails server 来代替 script/server, rails
console 代替 script/console,Rails 3 对这些命令提供了快捷键,rails s 是rails server的快捷键,
rails c 是rails console的快捷键,等等, 你只要给rails命令取个别名r就可以了, 这样 r s就是启动server了.

这样就可以敲更少的代码了, 但是我不想对rails2和rails3分别记两个不同的命令(rails2xx 通过s s来启动server,
rails3xx 通过s
s),我想通过ss工作在所有项目中,alias是做不到的,但是可以写个bash的shell函数,放到bash配置文件中:

function ss {
  if [ -e script/rails ]; then
    script/rails server $@
  else
    script/server $@
  fi
}
function sc {
  if [ -e script/rails ]; then
    script/rails console $@
  else
    script/console $@
  fi
}
function sg {
  if [ -e script/rails ]; then
    script/rails generate $@
  else
    script/generate $@
  fi
}

这样就搞定了!~

译自:http://blog.envylabs.com/2010/07/common-rails-command-shortcuts/

This entry was tagged Linux and Rails

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top