easy shortcut for <%= in vim (for rails apps) - ruby-on-rails

I type <%= often enough in rails that I want to find a shortcut for it. I am looking for a VIM solution (I specifically use macVim)
I know I can map it to keys or create my own snippetMate.vim snippet but I'm wondering if there's a generally accepted way of doing this.

Vim has abbreviations functionality out of the box. Enter:
:ab < <%=
then when you type < followed by <SPACE> in insert mode it will expand to <%=.
See :help :ab for more information.

tpope has created a plugin that you might find useful called Ragtag
It comes bundled with many mappings, but specifically the following
Mapping Changed to (cursor = ^) ~
<C-X>= foo<%= ^ %>
tpope has a great collection of plugins:
rails.vim
surround
pathogen
endwise
unimpaired
If you are doing any rails development I would highly suggest you look into rails.vim

The snippet way seems the best way to do that.

You can also add the following to your .vimrc:
imap <c-b> <%=
This adds the <%= whenever you type Control b.

Related

Vim html.erb snippets?? snipMate Need a vim tip

When I'm in an html.erb file, I get no snipMate snippets.
I would like both HTML and Ruby, or just HTML would be fine,
How would I do this?
Would I need to write a set of snippets?
If so, is there a way of pulling in existing snippets without copying them?
Is there a way of telling vim to go into html mode when it sees .html erb?
You can use an autocmd to set the filetype to html when opening a ".html.erb" file. This could have unwanted side effects for plugins that work for ".erb" files.
autocmd BufNewFile,BufRead *.html.erb set filetype=html
You can also load more than one set of snippets by using a dotted filetype:
autocmd BufNewFile,BufRead *.html.erb set filetype=html.eruby
See :help snippet-syntax in the snipMate help for more info.
Snippets are stored in directory called snippets somewhere in your ~/.vim folder.
If you look there, there is usually one file per filetype, there is a c.snippets, a ruby.snippets, so it seems what you have to do is to create an erb.snippets there with what you want.
Eventually you could copy the content of ruby.snippets and html.snippets into your new erb.snippets.
Alternatively you can search on github, some people have posted their own erb.snippets configuration. For example, there is a nice collection there :
https://github.com/scrooloose/snipmate-snippets
The best thing would to try first to open a snippet file and look at the syntax, it is pretty easy to create your own snippet depending on what you use the most.
I am currently on a promoting tour for UltiSnips on StackOverflow. UltiSnips supports extending other file types, your erb.snippets would look like this:
extends html, ruby, rails
snippet temp "A snippet only in Erb"
erb rules ${1}
endsnippet
A conversion script for snipMate snippets is shipped with UltiSnips, so switching is easy.
I used the autocommand method to the set the filetype, but then I got html syntax errors for things like this:
<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>
The last two angle brackets would be highlighted in red, which drove me bonkers. So, I created a symlink called eruby.snippets that points to html.snippets. That worked like a champ and now I don't have to make changes in two places. I also have an eruby-rails snippet directory for non-html eruby snippets.
This is on a Mac OS X system. Note that an alias won't work. You need to hit the terminal and use the ln command. Not sure about doing this on a Windoze system.
You can assign multiple snippets scopes to a single filetype. (I've found that altering the filetype tends to break some syntax highlighting).
You can check that the filetype for erb files is indeed 'eruby' with:
:set filetype?
If you're using the maintained fork of snipmate, it looks like you'll want both the eruby.snippets and eruby-rails.snippets from the snipmate-snippets repository (owned by honza, but I don't have enough reputation to link to it here) (see the INSTALL section of the snipmate README for proper setup).
If you are using the maintained fork, I believe setting g:snipMate.scope_aliases in your .vimrc with the following will work for your example:
let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['eruby'] = 'eruby,eruby-rails'
I've added a pull request to snipmate to have their documentation updated.
Jumping on the UltiSnips bandwagon after trying SnipMate for a while. Like SirVer mentioned, having the html, ruby, etc snippets available within an *.erb file was as simple as adding the extend line to the eruby.snippets file.
With the original snipMate plugin, create a file ~/.vim/ftplugin/erb_snippets.vim and put the following into it:
silent call ExtractSnipsFile(g:snippets_dir . 'html.snippets', &l:filetype)
silent call ExtractSnipsFile(g:snippets_dir . 'ruby.snippets', &l:filetype)

Tools to speed the generation of locale files (for rails)

I'm involved in preparing a pre-existing rails application for translation - going through the files under app/views/, finding the text, making a key in config/locales/de.yml (in this case), copying the text into de.yml, and putting t("key") in the view file. Repeat hundreds, maybe thousands of times. This is very tedious.
I don't think it can be fully automated, but the key things I need to do are select the text and give it a key.
So does anyone know of a tool that will automate the rest of the steps? I want to select the text, hit a key combo, type in a key, and the tool will put a key in the de.yml file, append the text, and put t("key") in place of the text that was selected.
My only key requirement is that it should run on Linux. I'm a vim user, but I'll learn Emacs if that is the best way to do it. I'll even install Eclipse if that is the best way ... I imagine that vim or emacs macros should easily be able to do the amount of work I'm asking for.
Anyone? Please?
With a bit of googling, I've managed to find a textmate plugin which would be near perfect if I had a Mac and Textmate. Something similar would be great.
Normally for those cases I use, I18n::Backend::Database (ActiveRecord Adapter for storing translations in the database). This has one major advantage that might also be helpful for your case, I can store translations without the key syntax.
Suppose you have this text <%= link_to 'Das ist ein Link Text', root_url %>
You could transform it to this with I18n::Backend::Database <%= link_to t('Das ist ein Link Text'), root_url %>.
Using this you could create a quick Textmate/Emacs/Vim shortcut to just basiclly wrap the t() around a string.
Does that solve your problem?

vim/macvim: locate where a method/symbol is defined

I'm using macvim/vim for most of my Ruby + Ruby on Rails development. Is there currently a way to jump to where a method was defined within a project, even if it's not in the same file as where it's being invoked? Either a language agnostic way or a Ruby/Rails specific way works.
I recommend using the ctags plugin, Bryan Liles put together a nice screencast on how to use it with rails development.
http://smartic.us/2009/04/05/using-ctags-in-vim/
From his page:
Not-so-complete cheat sheet:
^] – find a tag
^T – go backwards
:tags – show where you’ve been
:tag – go somewhere in your tag stack
:tselect or g] – show where something is referenced
^W-] – find a tag in a split window
You can also have a look at the vim help by running: help :tags
EDIT:
Here is a nice screencast on updating your tags file automatically http://smartic.us/2009/04/03/creating-ctags-with-git-hooks/

Easily create a Ruby on Rails partial from an existing block of markup using vim

Is there currently a plugin that you ruby on rails developers that are also using macvim/gvim/vim that allows you to take a quick block of code and create a partial from it? I know that TextMate does this, figured someone has ported it by now to vim also.
You want Tim Pope's rails.vim plugin:
http://rails.vim.tpope.net/
It provides an :Rextract command that pulls a range of lines into a partial. Here's a very short demo of it in action:
http://rails.vim.tpope.net/images/rpartial.gif
(The :Rpartial command in the demo is an alias for :Rextract.)
The plugin provides dozens of other features, too, and many people consider it a must-have for Rails development in Vim.
It works in vim with vim-rails plugin.
Select in visual mode code which you need to send to partial
Press key : and you will see :'<,'>
Complete command to :'<,'>Rextract partial_name where partial_name will your partial's file name. You can set folder for partial, for example :'<,'>Rextract shared/menu
Press Enter and enjoy.
rails.vim can do this. From the features summary:
:Rextract file replaces the desired
range (ideally selected in visual line
mode) with render :partial => 'file',
which is automatically created with
your content. The #file instance
variable is replaced with the file
local variable.

Problems with Netbeans re: Rails erb/rhtml intellisense?

I've been using Netbeans for Rails and like it a lot, considering how little I paid for it. But something that bothers me is that when I'm editing an RHTML or ERB file, it doesn't do the code autocomplete - or at least not reliably. Sometimes it shows the appropriate variables and methods that are available on an object after you type the dot operator. Sometimes it ignores the instance variables. Is there a solution for this? (Please don't say RadRails).
Oh and one more thing in case anyone has solved this: considering how often I have to type <% when I'm in a Rails template, I wish there was some hotkey for autotyping the tag . . . ? I always have to stop and look down at my keyboard to find the < and % keys before I can type the tag so it's not as trivial as it might sound.
I believe you're looking for something like this:
http://ruby.netbeans.org/codetemplates-rhtml.html
Type in one of the triggers, then hit the tab key to expand it to the code as given.
Also, you might want to explore using HAML. It's much easier on the hands.

Resources