Open files generated by 'rails generate' in your editor

Note: This post is for people who use the terminal to run Rails generators. It probably won’t interest you if your editor has a plugin to run them.

After running a Rails generator, you’ll often need to edit the generated files in your text editor.

Rails 4 may soon have the ability to open generated files in your text editor if you pass the --editor option on the command line. You can follow my pull request for more details.

If you use the --editor option (or its -e alias), the generator will open all generated or copied files in your text editor. The default editor is your GUI_EDITOR or EDITOR environment variable, but you can set the editor manually with --editor=manual-code-editor.

Rails 3 isn’t accepting any new features, so here’s how you can use the --editor option for your Rails 3 apps:

Single Rails 3 app

To install the --editor option in a single Rails 3 app, run the following command from your app directory:

curl https://gist.github.com/raw/4342095/rails -o script/rails

This will update script/rails, and you can commit the change.

Globally for all Rails 3 apps

If you are developing a lot of different apps, it might be too much hassle to update all of them. Instead, you can download the custom rails script to your /bin directory, and add some shortcut functions to your .bashrc.

Download script:

sudo curl https://gist.github.com/raw/4342095/rails3_with_editor -o /bin/rails3_with_editor
sudo chmod +x /bin/rails3_with_editor

Add shortcut functions:

Add the following to your ~/.bashrc:

rge() { rails3_with_editor generate "$@" --editor; }
rgm() { rails3_with_editor generate migration "$@" --editor; }

Now you can run rgm to generate a migration from any Rails 3 app.

comments powered by Disqus