Como usar VIM para el desarrollo de Ruby on Rails

Acabo de ver un articulo en el que muestra como utilizar VIM para el desarrollo de Ruby on Rails.

Si eres nuevo en Ruby no dejes de ver el tutorial interactivo, donde podrás aprender los pasos iniciales de este sistema de programación.

Lo incluyo a continuación (en ingles).

Develop Rails applications with Vim. Here’s a page of tips.

Example vimrc

Add the following lines to ~/.vimrc (~\_vimrc on Windows):


  set nocompatible          " We're running Vim, not Vi!
  syntax on                 " Enable syntax highlighting
  filetype plugin indent on " Enable filetype-specific indenting and plugins

  " Load matchit (% to bounce from do to end, etc.)
  runtime! macros/matchit.vim

  augroup myfiletypes
    " Clear old autocmds in group
    autocmd!
    " autoindent with two spaces, always expand tabs
    autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
  augroup END

rails.vim

Tim Pope has written a nice vim plugin to help you work with Rails. It adds additional syntax highlighting for some specific rails methods, helps you move around (including refined implementations of many of the tips below), and other goodies! The included vim :help file explains it all in more detail. (Even got some basic “snippets” support in it; use :Rabbrev to list them.)

The preferred download location is on vim.org.

rubycomplete.vim

Mark Guzman has written a ruby omni completion script which provides ruby-based introspective completion for code. It has rails support including live-column completion (database-based). Since it uses ruby to find it’s information it can provide some very interesting information. It is part of the Vim-Ruby project, stable releases can be found in the vim-ruby releases. Development and edge releases can be found at the author’s site.

Redit – call editor from browser (TextMate FootNotes clone)


    $ script/plugin install <a href="http://svn.antono.info/redit">http://svn.antono.info/redit</a>
    $ rake redit:install

From now every error stack from rails now become linked list. You can click on link and your Vim (or any other nice editor) should open right file at right line (where error occurs).

Comments are welcome:
http://antono.info/en/3

The Vim-Ruby Project

If you haven’t done so already, get the latest vim-ruby package by:


  gem install vim-ruby --remote
  vim-ruby-install.rb

This will get you the most recent stable Vim scripts for editing Ruby—and
Rails—source files, including full syntax highlighting for your views,
rubycomplete.vim and various other features mentioned below. (It does not
include rails.vim.)

Most of these scripts come pre-packaged with Vim 7, but the gem helps you get and stay up-to-date with their development, which is active.

Syntax-highlighting .rhtml files

Vim 7 comes preconfigured for .rhtml files. If you happen to be using an older version that doesn’t currently support it, install Vim/Ruby as above.

Automatic jumping to classes

Add this to your .vimrc file


  set path+=/path/to/your/rails-application/app/**
  set path+=/path/to/your/rails-application/lib/**
  set suffixesadd=.rb
  set includeexpr+=substitute(v:fname,'s$','','g')
  " or you can add substitution pattern s/ies$/y/g, s/ves$/f/g like this:
  " set includeexpr+=substitute(substitute(substitute(v:fname,'s$','','g'),'ie$','y','g'),'ve$','f','g')

After this, you can move the cursor on a class like in

has_many :clubs

and press gf. Vim will automatically open the Club.rb model file. This also works for require, scaffold, model etc.

You can also use the :find command, e.g. :find todo_controller. Unfortunately, there doesn’t seem to be a shortcut for entering the file name.

If you add set path+=.rhtml you can quickly find RHTML files in this manner as well, although that will probably create clashes.

Avoiding MVC same-name clashes


  map <F11> <Esc>:set suffixesadd=.rb<CR>gf
  map <F12> <Esc>:set suffixesadd=.rhtml<CR>gf

Now F11 will open the .rb, and F12 will open the .rhtml (You will still need to rules above)

Part of rails.vim is dedicated to a far more elaborate version of the above.

Vim IDE

The Project plugin is another approach to IDE. It takes a little getting used to, but is very well thought out and documented, so have no fear. You essentially specify the files and directories that you are interested in, and can easily select them from the left pane. It is a more intentional approach than the typical file-explorer and/or buffer-manager plugin.

As a long time Vim user, it’s the only IDE-like solution I’ve been happy to use. (GavinSinclair)

Project quickstart guide

I have to agree with GavinSinclair about the ease of use of Project. Here is a quick guide to get you started after installing Project.

  1. Type :Project in Vim to launch the Project plugin.
  2. Type \C to launch the create project routine.
  3. Enter a name for the project (e.g. RailsApp)
  4. Enter the directory path (e.g. /Users/me/Development/RailsApp)
  5. Type . as the CD parameter.
  6. Type * as the filter.
  7. Type \r to load all files and directories.

You should now have a full directory list of your rails application accessible from within Project. A lot of directories that you may not need can simply be deleted (I normally only keep around app and public directories).

To save you project settings, simply do a :w as you would any other file. For Project settings, refer to :help project.

- Paul Chiu

The rails.vim plugin (from above) can automate the above steps if installed alongside Project. Simply edit any file from your Rails application and use :Rproject.

Vim IDE (another very good Alternative)

use ctags + taglist
(screenshots)

+ The Rails Tag List patch which allows automatic Rails project-file loading and prepends the listed files with neat MVC-tags (screenshot)

Screen and Vim

With screen you can start the server, the Vim IDE and a tail on development.log in different windows. Create a file rails.screen with

screen 1 ./script/server
split
focus
resize 20
screen 2 tail -F log/development.log
focus
screen 3 bash startvim

Then create a file startvim which contains

vim -S layout_ide.vim

Now start the enviroment with

screen -c rails.screen

You can close the enviroment with CTRL-A CTRL-\

Vim IDE (Yet another alternative)

VimMate is a graphical add-on to Vim with IDE-like features. Since it’s still Vim, you can use all the other tips on this page.

Abbreviations and Variable Skipping (Snippets)

You can use the iab command to define some useful abbreviations. For example:


  iab forin for @element@ in @collection@<CR> @element@.@@<CR>end<Esc><<kk/@[^@]*@<CR>li

will expand forin to the following and place the cursor after the first @ in insert mode:

for element in collection
  element.@@
end

Adding the following to you .vimrc file will allow you to use Shift-Tab to jump between anything in @@’s and Shift-Del to remove the @@’s and hop to the next variable


  imap <S-Tab> <Esc>/@[^@]*@/<CR>li
  imap <S-Del> <Esc>ld/@<CR>xF@x/@[^@]*@<CR>li<C-Y>

A script that builds on the above is available.

- Lllama

Another script requires that Vim is compiled with Ruby support and takes the Snippets from Textmate.

Questions

Has anyone come up with an errorformat string to parse rake test output yet? -mml

Rake simply outputs Test::Unit error/failure messages so the Test::Unit compiler plugin (rubyunit.vim), available as part of the Vim/Ruby project and included with the latest versions of Vim, will work. —djk

A custom errorformat (similar to the above, but with some Rails specific tweaks) is also provided by rails.vim.

Has anyone modified ctags to work with ruby on rails yet? -smm

Exuberant ctags has long included Ruby support, but it is of limited use due to the dynamic nature of Ruby.

También te puede interesar:

1 comentario hasta ahora

  1. rvdhnzj Noviembre 5, 2008 9:31 am

    TnzRvk lqiwknomffyv, [url=http://yewiaxtmlsgf.com/]yewiaxtmlsgf[/url], [link=http://vvjnqlzkfnff.com/]vvjnqlzkfnff[/link], http://epjxdyfkvuew.com/

Dejar un comentario

Se por favor respetuoso y en asunto. Tu e-mail nunca sera publicado.