Develop Rails applications with Vim. Here's a page of tips.


h2. Automatic jumping to classes

Add this to your .vimrc file
<pre><code>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')
</code></pre>
After this, you can move the cursor on a class like in
<pre><code>has_many :clubs
</code></pre>
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.

h2. Vim IDE

If you use screen and vim together, it's possible to create an IDE like enviroment for Rails. To split the Vim window and create a file explorer on the left type:

<pre><code>:vertical split ~/
</pre></code>

You can resize the windows a bit to your liking using (this example is for 20 cols width):

<pre><code>:vertical resize 20</code></pre>

Then save the session with:

<pre><code>:mksession layout_ide.vim
</pre></code>

Now you can start Vim with this layout:

<pre><code>vim -S layout_ide.vim
</pre></code>

In the file explorer on the left you can press SHIFT-o on a file to open it on the right or 'Enter' to open a folder in the left pane. You can switch between the explorer and the code window with  CTRL-w w or CTRL-w CTRL-w

!http://www.qualityfab.de/vim.png!

p(. You can also directly start vim with a vertically split pane with the following command:
<pre><code>vim --cmd 'vsplit | vertical resize 30' app</code></pre>

h2. Vim IDE (Alternative)

The "Project":http://www.vim.org/scripts/script.php?script_id=69 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)

h2. 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

<pre><code>screen 1 ./script/server
split
focus
resize 20
screen 2 tail -F log/development.log
focus
screen 3 bash startvim
</pre></code>

Then create a file _startvim_ which contains

<pre><code>vim -S layout_ide.vim
</pre></code>

Now start the enviroment with 

<pre><code>screen -c rails.screen
</pre></code>

You can close the enviroment with CTRL-A CTRL-\

h2. Syntax-highlighting .rhtml files

There's an eruby syntax highlighting plugin available: http://www.vim.org/scripts/script.php?script_id=403



h2. Vim and syntax Highlighting

<a name="syntax"></a>
You can utilize vim to save a syntax-highlighted version of your code (as .html files, for publishing on web).  By utilizing this "vim script":http://bougyman.com/miscfiles/beautify.vim you can execute the following in the shell:
<pre><code>
vim -s beautify.vim someruby.rb
</code></pre>
And you will now have someruby.rb.html as well.  I like to use the following in my .vimrc for proper (light fg, dark bg) formatting:
<pre><code>
:set background=dark
:set tabstop=4
</pre></code>
"Example output":http://bougyman.com/miscfiles/php_serialize-1.0.2.1.rb.html

h2. Authors

- StephanSchmidt

h2.  Abbreviations, variable skipping and various tips

You can use the <code>iab</code> command to define some useful abbreviations.  For example:
<pre>
<code>
iab forin for @element@ in @collection@<CR> @element@.@@<CR>end<Esc><<kk/@[^@]*@<CR>li
</code>
</pre>
will expand @forin@ to the following and place the cursor after the first @ in insert mode:
<pre>
<code>
for @element@ in @collection@
    @element@.@@
end
</code>
</pre>
Adding the following to you _.vimrc_ file will allow you to use @Shift-Tab@ to jump between anything in <code>@@</code>'s and @Shift-Del@ to remove the <code>@@</code>'s and hop to the next variable
<pre>
<code>
imap <S-Tab> <Esc>/@[^@]*@/<CR>li
imap <S-Del> <Esc>ld/@<CR>xF@x/@[^@]*@<CR>li<C-Y>
</code>
</pre>

Finally, get yourself a copy of _xml.vim_, put it in _~/.vim/ftplugins_, make an _html.vim_ symlink that points to it and add <code>au BufNewFile,BufRead *.rhtml set filetype=xhtml</code> to your _.vimrc_.  Enjoy the magic.

A script that builds on the above is available "here":http://www.vim.org/scripts/script.php?script_id=1318

 - Lllama
---

Has anyone come up with an errorformat string to parse rake test output yet? -mml

category:Howto
