分享 提高 Ruby 程序员效率的 rc 文件

xiaoronglv · 2015年08月05日 · 最后由 rubyfan1 回复于 2016年01月24日 · 10685 次阅读
本帖已被管理员设置为精华贴
> ls -al ~/.*rc
-rw-r--r--  1 ryan  staff    67 Apr  1 23:09 /Users/ryan/.bashrc
-rw-------  1 ryan  staff   152 Aug  5 15:54 /Users/ryan/.gemrc
-rw-r--r--  1 ryan  staff   561 May  5 15:09 /Users/ryan/.htoprc
-rw-r--r--  1 ryan  staff   805 May  7 14:13 /Users/ryan/.inputrc
-rw-r--r--  1 ryan  staff   198 Aug  5 09:22 /Users/ryan/.irbrc
-rw-r--r--  1 ryan  staff    68 Apr 20 12:58 /Users/ryan/.mkshrc
-rw-r--r--  1 ryan  staff    42 Aug  4 17:26 /Users/ryan/.pryrc
-rw-r--r--  1 ryan  staff    31 Aug  5 12:34 /Users/ryan/.railsrc
-rw-------  1 ryan  staff  2776 Apr 29 16:20 /Users/ryan/.vimrc
-rw-r--r--  1 ryan  staff  3288 Jul 23 13:23 /Users/ryan/.zshrc

当终端里敲下这个 bash 命令时,会发现一堆隐藏的 dotfiles,其中又有很多 .xxxrc 风格的文件,比如 .bashrc .irbrc .pryrc .inputrc .zshrc .gemrc。rc 文件代表什么意思呢?

What does "rc" mean in dot files?

The 'rc' suffix goes back to Unix's grandparent, CTSS. It had a command-script feature called “runcom”. Early Unixes used 'rc' for the name of the operating system’s boot script, as a tribute to CTSS runcom.

这是 Stackoverflow 给出的一个解释,可以简单的理解为 Runtime Configuration。

Ruby 程序员如果能很好的利用 rc 文件,可以显著的提高工作效率。

1. ~/.zshrc

开发项目的时候免不了 bundle exec (why?),十分的啰嗦。

bundle exec rails c
bundle exec irb
bundle exec rails s
bundle exec cap production deploy
bundle exec annotate

如果你使用 oh my zsh,可以在 ~/.zshrc 中启用 bundler plugin。Zsh 会在 Rails 常用的开发命令前包一层 bundle exec,省事不少。这是我在 Railscast #201 Bundler) 学到的这一招。

# ~/.zshrc
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(bundler git vi-mode)
rails c # Awesome! It's equal to `bundle exec rails c` after load bundler plugin in Zsh
rails s 
cap 
pry
puma
annotate
irb

2. ~/.irbrc

Debug 中经常会碰到 3+ 层混排的数据结构,比如 ActionController::Parameters。

[
  {a:1, b:2, c:3},
  {d:4, e:5, f:6, g:7},
  {h:8, i:
    {
      j:9,
      k:10
    }
  }
]

终端默认打印出来的排版非常不友好,每次都把自己的钛合金狗眼看瞎。

Without awesome print

如果你喜欢 awesome_print 这个 gem,可以在 ~/.irbrc 中加上这两行代码,从此世界变得美好起来。

# ~/.irbrc
# please install awesome_print before you require.
require 'awesome_print'
AwesomePrint.irb!

以后打开 irb/console 时,AwesomePrint 会帮你做排版,查看 ActiveRecord 对象和多层嵌套的数据结构时都变得特别直观。

Awesome print

3. ~/.gemrc

每次安装 gem 时都会默认生成 rdoc/ri 文档,一个项目要依赖 30+ gem,安装文档耗费很多时间。大部分人没有查看 rdoc 文档的习惯,完全可以禁止安装文档。此外,中国地区访问 https://rubygems.org 不太稳定,大家喜欢 https://ruby.taobao.org 做默认的源。

方法一,你可以附上这二坨参数 gem install rails --source 'https://ruby.taobao.org' --no-document 来实现以上两个功能。

方法二,你也可把这些偏好保存在 ~/.gemrc 做默认值,每次 gem install xxx 时会自动加载这些环境参数。

显然后者更方便一些。

---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://ruby.taobao.org
:update_sources: true
:verbose: true
benchmark: false
gem: "--no-document"

Ruby Gem enviroment 还有很多参数,就不一一介绍了,有兴趣的话可以去翻 文档

备注:

  • 很多同学还在使用“—no-rdoc —no-ri”参数来禁止安装文档,最新的 RubyGems 已废弃这两个参数,新的参数是 "—- no-document"。 查看文档

-​-[no-]rdoc - Generate RDoc for installed gems Use -​-document instead

-​-[no-]ri - Generate ri data for installed gems. Use -​-document instead

-​-[no-]document [TYPES] - Generate documentation for installed gems List the documentation types you wish to generate. For example: rdoc,ri

  • 当有多个 source 时,每次安装都会去两个源检查最新版本,反而会让安装过程变得更漫长。

4. ~/.railsrc

做外包的同学应该会很喜欢这个配置文件。

每次使用 rails new appname 创建一个新项目时,都希望 Rails generator 能顺带完成以下常规工作:

  • 暂时不执行 bundle install(等修改完 Gemfile 再执行);
  • 不使用 Minitest;
  • 在 config/database.yml 中使用 MySQL 做默认数据库。

这些都可以通过修改 ~/.railsrc 来实现:

--skip-bundle #skips bundle install on generating a new rails app
--skip-test-unit #skips making the test unit folders if you prefer rspec
--database=mysql #defaults my development database to MySQL instead of SQLite3 in config/database.yml

搭配模板(Rails generators template),甚至可以让 rails new 时采用你喜欢的技术栈,做外包项目的同学可以通过 template 节省大量的时间。

  1. 暂时跳过 bundle install(放到第 4 步去执行);

  2. 把项目 Gemfile 的 source 修改为 ruby.taobao.org;

  3. 引入常用 Gem

    • 在 development group 引入 capistrano / annotate / better_errors / quite_assets
    • 在 test group 引入 minitest / mocha / factory_girl
    • 在 default group 引入 puma / sidekiq / kaminari
  4. 执行 bundle install;

  5. 使用 MySQL 做默认数据库。

使用以下二个配置文件可以实现以上 5 个功能,

~/.railsrc

--skip-bundle
--database=mysql
--template='~/template.rb' 

~/template.rb

# template.rb
# 替换默认的 source 为淘宝的源
# 'source "http://ruby.taobao.org"' => 'source "https://ruby.taobao.org"'
gsub_file 'Gemfile', /^source.*$/, "source 'https://ruby.taobao.org'"

# 删除不用的 Gem
gsub_file "Gemfile", /^gem\s+["']sqlite3["'].*$/,''
gsub_file "Gemfile", /^gem\s+["']turbolinks["'].*$/,''

# 安装常用的 Gem
gem 'newrelic_rpm'
gem 'puma'

gem_group :development do
  gem 'capistrano'          , require: false
  gem 'capistrano-bundler',   require: false
  gem 'capistrano-rails',     require: false
  gem 'capistrano-rvm',       require: false
  gem 'capistrano3-puma',     require: false
  gem 'capistrano-sidekiq',   require: false
  gem 'annotate',             require: false
  gem 'better_errors'
  gem 'quiet_assets'
end

gem_group :test do
  gem 'minitest'
  gem 'mocha', require: false
end

gem_group :development, :test do
  gem 'pry-rails'
  gem 'pry-byebug'
  gem 'pry-stack_explorer'
  gem 'awesome_print'
  gem 'factory_girl_rails'
end

# 修改完 Gemfile 后执行 bundle install
run "bundle install"

Rails generators template 能干的事情很多,超乎你的想象,我整理了几个比较好玩的 Rails Generators Templates 供参考。

如果你想 DIY 自己的模板,语法可以参考 Creating and Customizing Rails Generators & Templates

5. ~/.inputrc

如果你是 Vim 党,丧心病狂到要在 MySQL console 和 Rails console 中使用 Vim,可以创建 ~/.inputrc 文件来开启 console 的 vi mode。

# Best goddamn inputrc in the whole world.
# Author: Seth House <[email protected]>

# Woo!
set editing-mode vi

# Adds punctuation as word delimiters
set bind-tty-special-chars off

# Completion Options
set page-completions on
set completion-ignore-case on
set completion-query-items 200
set show-all-if-ambiguous on
set show-all-if-unmodified on
set visible-stats on

# Useful stuff for UTF-8
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off

$if mode=vi
    set keymap vi-command
    "gg": beginning-of-history
    "G": end-of-history

    set keymap vi-insert
    "\C-l": clear-screen
    "\C-w": backward-kill-word
    # auto-complete from the history
    "\C-p": history-search-backward
    "\C-n": history-search-forward
$endif

# file for clear-screen to work
set keymap vi

--eof

my blog: http://mednoter.com/rc-file.html work@sap jam team

还有.gitconfig .bundle/config 呐,虽然不是 rc 结尾的。

#1 楼 @mingyuan0715

这和标题不符啊,哈哈。

还有 ~/.ssh/config

inputrc 相当好用

👍以前还不知道 .railsrc

天天折腾农具现在,工作不饱和啊 @genewu

#5 楼 @zgm

这叫磨刀不误砍柴工啊

7 楼 已删除

bundle exec 基本不需要吧。我很少用。

再推荐一个 git-up

真是卧虎藏龙呀

@xiaoronglv 感谢楼主的分享,对于提高效率的确非常有用。

做为 vim 的忠实用户,我刚按楼主的方式,新增了 ~/.inputrc 文件,但对 mysql console 暂时没起作用,所以在 stackoverflow 上面查询了下

It's ~/.inputrc if mysql was built with GNU Readline. It's ~/.editrc if mysql was built with the BSD libedit. –  JdeBP Feb 10 '14 at 2:00

fedora 20: .inputrc worked, .editrc didn't. –  G Mawr Jul 15 '14 at 9:33

参考 stackoverflow

http://stackoverflow.com/questions/2216471/how-to-get-vi-keybindings-to-work-in-mysql-client

而我系统的 mysql was built with BSD libedit,所以用不 inputrc,而新增 editrc,搞定

$ vim .editrc
bind -v

再次感谢楼主分享,已经点赞,希望更多人看到该文章,从今天起,终于可以在 msyql console 下面用 vim 了,coooool

注:如果 irb console 要使用 vim,还是需要 ~/.inputrc 这个文件

还有 ~/.ssh/config

inputrc 相当好用

tigrc 也是个好东东

set commit-order = topo         # Order commits topologically
set git-colors = no             # Do not read Git's color settings.
set horizontal-scroll = 33%     # Scroll 33% of the view width
set blame-options = -C -C -C    # Blame lines from other files

# Wrap branch names with () and tags with <>
# set reference-format = (branch) <tag>

# Configure blame view columns using command spanning multiple lines.
set main-view = \
        date:default \
        author:abbreviated \
        id:yes,color \
        commit-title

不错,已收藏。

zsh 中添加 pluginrails 然后rc就是rails c rs就是rails s rdm就是rake db:migrate rdd就是rake db:drop 其他-,- 自己理解吧。

:plus1: 学习到 . ~/.irbrc

好东西。收藏。

~/.inputrc 里面怎么能没有这几行!!

"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

"\C-p": history-search-backward
"\C-n": history-search-forward

快速查找历史命令

凑个热闹,加一个 .ackrc。仅仅举了几个例子,全部的就不贴了。

--ignore-dir=node_modules
--type-add=css=.scss,sass,less
--type-add=js=.es6,coffee
--type-add=ruby=.ru

好贴!

@xiaoronglv

~/.railsrc

--skip-bundle --database=mysql --template='~/template.rb'

最后一行似乎要改成 -template ~/template.rb 不然会出错

已赞,已收藏,好帖子!

太折腾了,要效率高最好的方法就是用 IDE

需要 登录 后方可回复, 如果你还没有账号请 注册新账号