开发工具 Vim 下开发 Ruby on Rails 必装插件之 rails.vim 简单介绍

griffinqiu · 2017年02月18日 · 最后由 torvaldsdb 回复于 2017年03月09日 · 6922 次阅读

rails.vim

刚刚抛弃世界上最强大的语言转投 Ruby on Rails 怀抱。刚开始被被 Rails 的 View, Model, Controller, UnitTest 文件之间跳来跳去折腾的够呛。单独的 CtrlP 或者 Fzf 明显不能满足要求。仔细研究 rails.vim 插件后豁然开朗。

常规命令

rails.vim 提供了一些直接在 Vim 执行的 Rails 命令。但是在 Vim 8 之前 执行外部命令都是同步运行的,需要等待的时间比较久,这段时间不能在 Vim 不能做其他的操作。虽然 Vim 8 提供了异步模式,但大多数插件都没跟上。在目前主流的 Tmux + Vim 搭配下,反而不如在 Tmux 里切换到另外一个 Pane 或者 Window 来执行外部命令。所以在这里不对 Vim 执行的 Rails 命令做介绍,详情请见 :help rails-commands

导航

这才是 rails.vim 的强大之处。没有太多需要记忆的命令而又功能强大,处处体现了 Rails 的哲学。

Goto File

gf 本是一个自带的 Vim 命令,Normal 模式下,当光标下为文件路径时,按下 gf 可以基于相对路径跳转到该文件。但是在实际项目开发过程中很多时候 gf 不会命中。rails.vim 遵循 Rails 的习惯大于配置的原则加强了 gf 命令,大大的提高了这个命令的命中率。只要我们项目文件的路径和命名是符合 Rails 的约定。gf 的命中率是 100%。下面列举了一些常见的跳转案例。 * 为光标所在的位置。

Pos*t.first 
app/models/post.rb 

has_many :c*omments
app/models/comment.rb 

link_to 'Home', :controller => 'bl*og'
app/controllers/blog_controller.rb 

<%= render 'sh*ared/sidebar' %>
app/views/shared/_sidebar.html.erb 

<%= stylesheet_link_tag 'scaf*fold' %>
public/stylesheets/scaffold.css 

class BlogController < Applica*tionController
app/controllers/application_controller.rb 

class ApplicationController < ActionCont*roller::Base
.../action_controller/base.rb 

fixtures :pos*ts
test/fixtures/posts.yml 

layout :pri*nt
app/views/layouts/print.html.erb 

<%= link_to "New", new_comme*nt_path %>
app/controllers/comments_controller.rb (并且会跳转到 def new) 

Alternate 和 Related 是 rails.vim 插件定义的两个概念,命令分别是 :A 和 :R。

注意 A 和 R 都为大写,在 Vim 里有个约定,插件提供的命令都是需大写开头

  • :A 跳转到当前文件的 Alternate 文件
  • :R 跳转到当前文件的 Related 文件

Alternate 和 Related 对应表:

Current file Alternate file Related file
model unit test schema definition
controller (in method) functional test template (view)
template (view) functional test controller (jump to method)
migration previous migration next migration
database.yml database.example.yml environments/*.rb

配合 S, V, T 可以在不同的 Vim 窗口模式下打开,如

  • :AS 在一个新的 Vim Split 窗口打开 Alternate 文件
  • :RV 在一个新的 Vim 垂直的 Split 窗口打开 Related 文件
  • :AS 在一个新的 Vim Tab 打开 Alternate 文件

快速在指定文件类型中打开文件

rails.vim 还提供一组命令可以快速指定文件类型打开某个文件。如

  • :Econtroller users 可以打开 app/controllers/users_controller.rb
  • :Etask user 可以打开 lib/tasks/user.rake

类似的命令还很多,具体请查看 :h rails-type-navigation

重构

提取 partial

日常 Rails 开发中,经常会碰到在 view 中需要将可重用或者逻辑较为独立的片段提取为一个 partial。rails.vim 里提供一个叫 Rextract 的命令来帮忙完成。

如需要将下图的 simple_form 片段提取成 _form。先选中相关代码后执行 :Rextract form 即可。

![alt text][rextract-cmd]

![alt text][rextract-result]

rails.vim 非常智能,如果是在 Helper 文件里 Rextract 为提取新的 helper。在 Model 和 Controller 里则是 concern。

和 surround.vim 的集成

surround.vim 是一个用来添加、修改、替换类似各种括号,引号等成对出现的符号。只要 Vim 安装了 surround.vim,rails.vim 便会自动对它增强。在 eruby 中可以用 <C-g>s= 等快捷键快速插入 eruby 标签。如

  • <C-g>s= 可以得到 <%= %>
  • <C-g>s- 可以得到 <% %>
  • <C-g>s# 可以得到 <%# %>
  • <C-g>s<C-e> 可以得到 <% %> <% end %>

遗憾的是 rails.vim 并没有对删除和替换 surround 做增强。

[rextract-cmd]: https://ww4.sinaimg.cn/large/006tKfTcgy1fcuohz615jj31j60tkgv0.jpg [rextract-result]: https://ww3.sinaimg.cn/large/006tKfTcgy1fcuoi0md08j31d80tqdov.jpg

给刚用 vim 的同学一些参考资料~

最牛编辑器:Vim. 第一节 http://www.imooc.com/article/13269 最牛编辑器:Vim. 第二节 http://www.imooc.com/article/13272 最牛编辑器:Vim. 第三节 http://www.imooc.com/article/13275

整理得很棒!

文件跳转在分模块之后会失效,我的处理是把子模块都加进来

let g:rails_path_additions =  [ 'app/models/bag',  'app/models/admin' ]

另外 Emodel 这类操作是可以扩展的,例如,如果做了分文件的 model 的 locals 配置,在 model :Etrans 就可以跳转过去

let g:rails_projections = {
      \   "config/locales/models/*.yml": {
      \     "affinity": "model",
      \     "command": "trans",
      \   }
      \ }

最下面的,建议用 completion.vim

rails.vim 以前一直不敢用,因为老的 Vim 没有 async 的实现,所有动作都是同步的,每个命令都要卡那么一下。

rails.vim 提供了一些直接在 Vim 执行的 Rails 命令。但是在 Vim 8 之前 执行外部命令都是同步运行的,需要等待的时间比较久,这段时间不能在 Vim 不能做其他的操作。虽然 Vim 8 提供了异步模式,但大多数插件都没跟上。

那么到底 rails.vim 改进了没有?还卡不卡?

一直在 linux 的 vim 上用,倒没有遇到卡的问题。 只遇到一个 scrooloose/syntastic 因为同步的原因,在同事的 mac vim 上面卡滞,在 linux vim 上也不卡。

#4 楼 @huacnlee gf, A, R 这些基本的操作很流畅呀。但是不建议在 Vim 里执行 rails 等一些外部命令

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