is supported.
*rails-:Rscript*
:Rscript {script} {options}
Call ruby script/{script} {options}. Defaults to
calling script/console.
*rails-:Rconsole*
:Rconsole {options} Obsolete. Call |:Rscript| instead.
*rails-:Rrunner*
:[range]Rrunner {code} Executes {code} with script/runner. Differs from
:Rscript runner {code} in that the code is passed as
one argument. Also, |system()| is used instead of
|:!|. This is to help eliminate annoying "Press
ENTER" prompts. If a line number is given in the
range slot, the output is pasted into the buffer after
that line.
*rails-:Rp*
:[range]Rp {code} Like :Rrunner, but call the Ruby p method on the
result. Literally "p begin {code} end".
*rails-:Rpp* *rails-:Ry*
:[range]Rpp {code} Like :Rp, but with pp (pretty print) or y (YAML
:[range]Ry {code} output).
*rails-:Rgenerate*
:Rgenerate {options} Calls script/generate {options}, and then edits the
first file generated.
*rails-:Rdestroy*
:Rdestroy {options} Calls script/destroy {options}.
*rails-:Rserver*
:Rserver {options} Launches script/server {options} in the background.
On win32, this means |!start|. On other systems, this
uses the --daemon option.
*rails-:Rserver!*
:Rserver! {options} Same as |:Rserver|, only first attempts to kill any
other server using the same port. On non-Windows
systems, lsof must be installed for this to work.
REFACTORING HELPERS *rails-refactoring*
A few features are dedicated to helping you refactor your code.
Partial Extraction ~
*rails-partials*
The :Rextract command can be used to extract a partial to a new file.
*rails-:Rextract*
:[range]Rextract [{controller}/]{name}
Create a {name} partial from [range] lines (default:
current line).
*rails-:Rpartial*
:[range]Rpartial [{controller}/]{name}
Obsolete alias for :Rextract.
If this is your file, in app/views/blog/show.html.erb: >
1
2
<%= @post.title %>
3
<%= @post.body %>
4
And you issue this command: >
:2,3Rextract post
Your file will change to this: >
1
2 <%= render :partial => 'post' %>
3
And app/views/blog/_post.html.erb will now contain: >
1 <%= post.title %>
2 <%= post.body %>
As a special case, if the file had looked like this: >
1 <% for object in @posts -%>
2 <%= object.title %>
3 <%= object.body %>
4 <% end -%>
<
The end result would have been this: >
1 <%= render :partial => 'post', :collection => @posts %>
<
The easiest way to choose what to extract is to use |linewise-visual| mode.
Then, a simple >
:'<,'>Rextract blog/post
will suffice. (Note the use of a controller name in this example.)
Migration Inversion ~
*rails-migrations* *rails-:Rinvert*
:Rinvert In a migration, rewrite the self.up method into a
self.down method. If self.up is empty, the process is
reversed. This chokes on more complicated
instructions, but works reasonably well for simple
calls to create_table, add_column, and the like.
INTEGRATION *rails-integration*
Having one foot in Rails and one in Vim, rails.vim has two worlds with which
to interact.
Integration with the Vim Universe ~
*rails-vim-integration*
A handful of Vim plugins are enhanced by rails.vim. All plugins mentioned can
be found at http://www.vim.org/. Cream and GUI menus (for lack of a better
place) are also covered in this section.
*rails-:Rtree*
:Rtree [{arg}] If |NERDTree| is installed, open a tree for the
application root or the given subdirectory.
*rails-:Rdbext* *rails-dbext*
:Rdbext [{environment}] This command is only provided when the |dbext| plugin
is installed. Loads the {environment} configuration
(defaults to $RAILS_ENV or development) from
config/database.yml and uses it to configure dbext.
The configuration is cached on a per application
basis. With dbext version 8.00 and newer, this
command is called automatically when needed. When
dbext is configured, you can execute SQL directly from
Vim: >
:Select * from posts order by id desc
:Update comments set author_id = 1
<
*rails-surround*
The |surround| plugin available from vim.org enables adding and removing
"surroundings" like parentheses, quotes, and HTML tags. Even by itself, it is
quite useful for Rails development, particularly eRuby editing. When coupled
with this plugin, a few additional replacement surroundings are available in
eRuby files. See the |surround| documentation for details on how to use them.
The table below uses ^ to represent the position of the surrounded text.
Key Surrounding ~
= <%= ^ %>
- <% ^ -%>
# <%# ^ %>
<% ^ -%>\n<% end -%>
The last surrounding is particularly useful in insert mode with the following
map in one's vimrc. Use Alt+o to open a new line below the current one. This
works nicely even in a terminal (where most alt/meta maps will fail) because
most terminals send as o anyways.
>
imap o
<
One can also use the surrounding in a plain Ruby file to append a bare
"end" on the following line.
*rails-abolish*
Among the many features of |abolish| on vim.org is the ability to change the
inflection of the word under the cursor. For example, one can hit crs to
change from MixedCase to snake_case. This plugin adds two additional
inflections: crl for alternating between the singular and plural, and crt for
altering between tableize and classify. The latter is useful in changing
constructs like BlogPost.all to current_user.blog_posts.all and vice versa.
*rails-cream*
This plugin provides a few additional key bindings if it is running under
Cream, the user friendly editor which uses Vim as a back-end. Ctrl+Enter
finds the file under the cursor (as in |rails-gf|), and Alt+[ and Alt+] find
the alternate (|rails-alternate|) and related (|rails-related|) files.
*rails-menu*
If the GUI is running, a menu for several commonly used features is provided.
Also on this menu is a list of recently accessed projects. This list of
projects can persist across restarts if a 'viminfo' flag is set to enable
retaining certain global variables. If this interests you, add something like
the following to your vimrc: >
set viminfo^=!
<
Integration with the Rails Universe ~
*rails-rails-integration*
The general policy of rails.vim is to focus exclusively on the Ruby on Rails
core. Supporting plugins and other add-ons to Rails has the potential to
rapidly get out of hand. However, a few pragmatic exceptions have been made.
*rails-template-types*
Commands like :Rview use a hardwired list of extensions (erb, rjs, etc.)
when searching for files. In order to facilitate working with non-standard
template types, several popular extensions are featured in this list,
including haml, liquid, and mab (markaby). These extensions will disappear
once a related configuration option is added to rails.vim.
*rails-rspec*
The presence of a spec directory causes several additional behaviors to
activate. :A knows about specs and will jump to them (but Test::Unit files
still get priority). The associated controller or model of a spec is
detected, so all navigation commands should work as expected inside a spec
file. :Rake in a spec runs just that spec, and in a model, controller, or
helper, runs the associated spec.
|:Runittest| and |:Rfunctionaltest| lead double lives, handling model and
controller specs respectively. For helper and view specs, you can use
|:Rspec| or define your own navigation commands:
>
Rnavcommand spechelper spec/helpers -glob=**/*
\ -suffix=_helper_spec.rb -default=controller()
Rnavcommand specview spec/views -glob=**/* -suffix=_spec.rb
<
ABBREVIATIONS *rails-abbreviations* *rails-snippets*
Abbreviations are "snippets lite". They may later be extracted into a
separate plugin, or removed entirely.
*rails-:Rabbrev*
:Rabbrev List all Rails abbreviations.
:Rabbrev {abbr} {expn} [{extra}]
Define a new Rails abbreviation. {extra} is permitted
if and only if {expn} ends with "(".
*rails-:Rabbrev!*
:Rabbrev! {abbr} Remove an abbreviation.
Rails abbreviations differ from regular abbreviations in that they only expand
after a (see |i_CTRL-]|) or a (if does not work, it is
likely mapped by another plugin). If the abbreviation ends in certain
punctuation marks, additional expansions are possible. A few examples will
hopefully clear this up (all of the following are enabled by default in
appropriate file types).
Command Sequence typed Resulting text ~
Rabbrev rp( render :partial\ => rp( render(:partial =>
Rabbrev rp( render :partial\ => rp render :partial =>
Rabbrev vs( validates_size_of vs( validates_size_of(
Rabbrev pa[ params pa[:id] params[:id]
Rabbrev pa[ params pa params
Rabbrev pa[ params pa.inspect params.inspect
Rabbrev AR:: ActionRecord AR::Base ActiveRecord::Base
Rabbrev :a :action\ =>\ render :a render :action =>
In short, ( expands on (, :: expands on . and :, and [ expands on . and [.
These trailing punctuation marks are NOT part of the final abbreviation, and
you cannot have two mappings that differ only by punctuation.
You must escape spaces in your expansion, either as "\ " or as "". For
an abbreviation ending with "(", you may define where to insert the
parenthesis by splitting the expansion into two parts (divided by an unescaped
space).
Many abbreviations are provided by default: use :Rabbrev to list them. They
vary depending on the type of file (models have different abbreviations than
controllers). There is one "smart" abbreviation, :c, which expands to
":controller => ", ":collection => ", or ":conditions => " depending on
context.
SYNTAX HIGHLIGHTING *rails-syntax*
Syntax highlighting is by and large a transparent process. For the full
effect, however, you need a colorscheme which accentuates rails.vim
extensions. One such colorscheme is vividchalk, available from vim.org.
The following is a summary of the changes made by rails.vim to the standard
syntax highlighting.
*rails-syntax-keywords*
Rails specific keywords are highlighted in a filetype specific manner. For
example, in a model, has_many is highlighted, whereas in a controller,
before_filter is highlighted. A wide variety of syntax groups are used but
they all link by default to railsMethod.
If you feel a method has been wrongfully omitted, submit it to the
|rails-plugin-author|.
*rails-syntax-classes*
Models, helpers, and controllers are given special highlighting. Depending on
the version of Vim installed, you may need a rails.vim aware colorscheme in
order to see this. Said colorscheme needs to provide highlighting for the
railsUserClass syntax group.
The class names are determined by camelizing filenames from certain
directories of your application. If app/models/line_item.rb exists, the class
"LineItem" will be highlighted.
The list of classes is refreshed automatically after certain commands like
|:Rgenerate|. Use |:Rrefresh| to trigger the process manually.
*rails-syntax-assertions*
If you define custom assertions in test_helper.rb, these will be highlighted
in your tests. These are found by scanning test_helper.rb for lines of the
form " def assert_..." and extracting the method name. The railsUserMethod
syntax group is used. The list of assertions can be refreshed with
|:Rrefresh|.
*rails-syntax-strings*
In the following line of code, the "?" in the conditions clause and the "ASC"
in the order clause will be highlighted: >
Post.find(:all, :conditions => ["body like ?","%e%"], :order => "title ASC")
<
A string literal using %Q<> or %<> delimiters will have its contents
highlighted as HTML. This is sometimes useful when writing helpers. >
link = %<Vim>
<
*rails-syntax-yaml*
YAML syntax highlighting has been extended to highlight eRuby, which can be
used in most Rails YAML files (including database.yml and fixtures).
MANAGED VIM OPTIONS *rails-options*
The following options are set local to buffers where the plugin is active.
*rails-'shiftwidth'* *rails-'sw'*
*rails-'softtabstop'* *rails-'sts'*
*rails-'expandtab'* *rails-'et'*
A value of 2 is used for 'shiftwidth' (and 'softtabstop'), and 'expandtab' is
enabled. This is a strong convention in Rails, so the conventional wisdom
that this is a user preference has been ignored.
*rails-'path'* *rails-'pa'*
All the relevant directories from your application are added to your 'path'.
This makes it easy to access a buried file: >
:find blog_controller.rb
<
*rails-'suffixesadd'* *rails-'sua'*
This is filetype dependent, but typically includes .rb, .rake, and several
others. This allows shortening the above example: >
:find blog_controller
<
*rails-'includeexpr'* *rails-'inex'*
The 'includeexpr' option is set to enable the magic described in |rails-gf|.
*rails-'filetype'* *rails-'ft'*
The 'filetype' is sometimes adjusted for Rails files. Most notably, *.rxml
and *.rjs are treated as Ruby files, and files that have been falsely
identified as Mason sources are changed back to eRuby files (but only when
they are part of a Rails application).
*rails-'completefunc'* *rails-'cfu'*
A 'completefunc' is provided (if not already set). It is very simple, as it
uses syntax highlighting to make its guess. See |i_CTRL-X_CTRL-U|.
CONFIGURATION *rails-configuration*
Very little configuration is actually required; this plugin automatically
detects your Rails application and adjusts Vim sensibly.
*rails-:autocmd* *rails-autocommands*
If you would like to set your own custom Vim settings whenever a Rails file is
loaded, you can use an autocommand like the following in your vimrc: >
autocmd User Rails silent! Rlcd
autocmd User Rails map :Rake
You can also have autocommands that only apply to certain types of files.
These are based off the information shown when running the |:Rails!|
command, with hyphens changed to periods. A few examples: >
autocmd User Rails.controller* iabbr wsn wsdl_service_name
autocmd User Rails.model.arb* iabbr vfo validates_format_of
autocmd User Rails.view.erb* imap <%= %>3h
End all such Rails autocommands with asterisks, even if you have an exact
specification, to allow for more specific subtypes to be added in the future.
There is also a filename matching syntax: >
autocmd User Rails/config/environment.rb Rabbrev c config
autocmd User Rails/**/foo_bar.rb Rabbrev FB:: FooBar
Use the filetype based syntax whenever possible, reserving the filename based
syntax for more advanced cases.
*macros/rails.vim*
If you have several commands to run on initialization for all file types, they
can be placed in a "macros/rails.vim" file in the 'runtimepath' (for example,
"~/.vim/macros/rails.vim"). This file is sourced by rails.vim each time a
Rails file is loaded.
*config/rails.vim*
If you have settings particular to a specific project, they can be put in a
config/rails.vim file in the root directory of the application. The file is
sourced in the |sandbox| for security reasons.
*rails-:Rset*
:Rset {option}[={value}]
Query or set a local option. This command may be
called directly, from an autocommand, or from
config/rails.vim.
Options may be set in one of four scopes, which may be indicated by an
optional prefix. These scopes determine how broadly an option will apply.
Generally, the default scope is sufficient.
Scope Description ~
a: All files in one Rails application
b: Buffer (file) specific
g: Global to all applications
l: Local to method (same as b: in non-Ruby files)
Options are shown below with their default scope, which should be omitted.
While you may override the scope with a prefix, this is rarely necessary and
oftentimes useless. (For example, setting g:task is useless because the
default rake task will apply before considering this option.)
Option Meaning ~
b:alternate Custom alternate file for :A, relative to the Rails root
b:controller Default controller for certain commands (e.g., :Rhelper)
b:model Default model for certain commands (e.g., :Rfixtures)
l:related Custom related file for :R, relative to the Rails root
a:root_url Root URL for commands like :Rpreview
Examples: >
:Rset root_url=http://localhost:12345
:Rset related=app/views/blog/edit.html.erb
<
*rails-modelines*
If |g:rails_modelines| is enabled, these options can also be set from
modelines near the beginning or end of the file. These modelines will always
set buffer-local options; scope should never be specified. Examples: >
# Rset task=db:schema:load
<%# Rset alternate=app/views/layouts/application.html.erb %>
Modelines can also be local to a method. Example: >
def test_comment
# rset alternate=app/models/comment.rb
These two forms differ only in case.
Modelines are deprecated.
GLOBAL SETTINGS *rails-global-settings*
A few global variables control the behavior of this plugin. In general, they
can be enabled by setting them to 1 in your vimrc, and disabled by setting
them to 0. >
let g:rails_some_option=1
let g:rails_some_option=0
Most of these seldom need to be used. So seldom, in fact, that you should
notify the |rails-plugin-author| if you find any of them useful, as nearly all
are being considered for removal.
*g:loaded_rails* >
let g:loaded_rails=1
Set this include guard to prevent the plugin from being loaded.
*g:rails_abbreviations*
Enable Rails abbreviations. See |rails-abbreviations|. Enabled by default.
*g:rails_ctags_arguments* >
let g:rails_ctags_arguments='--languages=-javascript'
Additional arguments to pass to ctags from |:Rtags|. Defaults to ignoring
JavaScript files, since ctags has a tendency to choke on those.
*g:rails_default_file* >
let g:rails_default_file='config/database.yml'
File to load when a new Rails application is created. Defaults to the README.
*rails-screen* *g:rails_gnu_screen* >
let g:rails_gnu_screen=1
Use GNU Screen or Tmux (if it is running) to launch |:Rscript| console and
|:Rserver| in the background. Enabled by default.
*g:rails_history_size* >
let g:rails_history_size=5
Number of projects to remember. Set to 0 to disable. See |rails-menu| for
information on retaining these projects across a restart.
*g:rails_mappings* >
let g:rails_mappings=1
Enables a few mappings (mostly for |rails-navigation|). Enabled by default.
*g:rails_modelines* >
let g:rails_modelines=1
Enable modelines like the following: >
# Rset task=db:schema:load
Modelines set buffer-local options using the :Rset command.
Also enables method specific modelines (note the case difference): >
def show
# rset preview=blog/show/1
Modelines are deprecated and disabled by default.
*g:rails_menu* >
let g:rails_menu=1
When 2, a Rails menu is created. When 1, this menu is a submenu under the
Plugin menu. The default is 0, as the menu is slated for removal from future
versions of rails.vim.
*g:rails_url* >
let g:rails_url='http://localhost:3000/'
Used for the |:Rpreview| command. Default is as shown above. Overridden by
b:rails_url.
*g:rails_syntax* >
let g:rails_syntax=1
When enabled, this tweaks the syntax highlighting to be more Rails friendly.
Enabled by default. See |rails-syntax|.
*rails-tabs* *g:rails_tabstop* >
let g:rails_tabstop=4
This option now requires the plugin railstab.vim from vim.org:
http://www.vim.org/scripts/script.php?script_id=2253
If your goal is simply just override this plugin's settings and use your own
custom 'shiftwidth', adjust things manually in an autocommand: >
autocmd User Rails set sw=4 sts=4 noet
This is highly discouraged: don't fight Rails.
ABOUT *rails-about* *rails-plugin-author*
This plugin was written by Tim Pope. Email all comments, complaints, and compliments to him at vim at tpope. org.
The latest stable version can be found at
http://www.vim.org/scripts/script.php?script_id=1567
Bugs can be reported and the very latest development version can be retrieved
from GitHub:
https://github.com/tpope/vim-rails
git clone git://github.com/tpope/vim-rails.git
*rails-license*
Copyright (c) Tim Pope. Distributed under the same terms as Vim itself.
See |license|.
vim:tw=78:ts=8:ft=help:norl: