Welcome to Rapid Rails Part 3: Desktop mastery, the third article in my series focussing on making Rails (and yourself) faster.
A good programmer recognises when to reuse and therefore reduce code. A great programmer applies this tendency to their own workflow. Whether you use an IDE or text editor, working with Rails can be made more pleasant and efficient by observing commonly performed tasks and simplifying them.
The examples given below have a heavy bias toward TextMate, Vim and Mac OS. If you work in Windows or Linux, at the very least consider the following 10 ideas.
- Ensure you can invoke Rails scripts without leaving your editor
- Run tests without leaving your editor
- Keep documentation nearby and searchable
- Make sure your database schema and data are only key presses away
- Integrate source code management with your editor
- Make switching between projects as easy as possible
- Use snippets, completion and generators when possible
- Master your text editor – collect books, articles, blog posts
- Learn how to extend your text editor
- Code navigation
1. Ensure you can invoke Rails scripts without leaving your editor
Repeatedly visiting the terminal to run Rails scripts quickly gets tiresome. The TextMate Rails bundle allows you to call generator scripts and migrations by pressing
shift-ctrl-|. Meanwhile rails.vim provides somewhat abstracted access to these scripts.
2. Run tests without leaving your editor
Running tests in TextMate is as simple as pressing
cmd-r when you're viewing a test file. To show a menu of types of tests to run, press
ctrl-\. With rails.vim, you can use
:Rake with the appropriate tests.
3. Keep documentation nearby and searchable
TextMate provides documentation for word using RDoc by pressing
ctrl-h, which should cut down your trips to http://api.rubyonrails.org. Similarly, rails.vim has the
4. Make sure your database schema and data are only key presses away
Hover over a model name in TextMate, then press
cmd-ctrl-shift-s -- this will show the schema for the model. rails.vim provides integration with the dbext plugin. This is from the rails.vim documentation:
Then, you can use features like table name completion and commands like: :Create database brablog_development
5. Integrate source code management with your editor
In TextMate, press
shift-ctrl-a to bring up the subversion bundle's menu. The commit window is especially user-friendly. Vim has vcscommand.vim
6. Make switching between projects as easy as possible
Stacks were added in Mac OS Leopard, and I find these a great way of grouping my TextMate projects for quick, spring–loaded access. It’s worth getting to know your editor’s project management capabilities. Even though it’s tempting to use scratch projects (with TextMate this is usually done with
mate . in the terminal), carefully maintaining your own projects allows you to group files (differently to the file system) and maintain state. Vim supports these features through project.vim.
Of course, there are many ways of quickly accessing projects. Here’s a few ideas:
- Use Quicksilver with TextMate
- Create memorable shell aliases
- Pin them to your Windows start menu, or add a folder
- Use a Windows-compatible Start Menu replacement to find project files
7. Use snippets, completion and generators when possible
TextMate has loads of snippets. You can access them by typing a phrase then pressing tab. For example,
deft[tab] creates the outline for a unit test case. To search TextMate snippets (or any of the keyboard shortcuts I've detailed here), press ctrl-cmd-t to bring up the select bundle item menu. This can also be found by clicking on the Bundles menu.
Also, don’t forget to make the most of Rails generators. Run
script/generate to see what's available.
8. Master your text editor – collect books, articles, blog posts
O’Reilly’s Vim books helped me get to grips with Vim, I used to carry The vi Editor Pocket Reference around in my early days at university and my first job. Searching through Vim's plugins can yield some useful finds, and there’s even a Vim tips wiki.
9. Learn how to extend your text editor
Vim’s macros can take a bit of getting used to, but this article should set you straight: More Vim: Save Time With Macros. TextMate is a bit easier, but the power of TextMate’s extensibility lies in the Bundle Editor (found in: Bundles, Bundle Editor, Show Bundle Editor.)
Many of TextMate’s bundles are shell scripts or even ruby scripts, and there’s a TextMate ruby library for integrating with the GUI. You can also create your own snippets, which can be useful for quickly writing our your company’s boilerplate file headers. The TextMate manual entry on bundles is pretty in–depth.
10. Code navigation
Some of these features require RubyAMP.
cmd-tlets you quickly search and switch files
shift-cmd-fis Find in Project
shift-cmd-mskips to a method definition
cmd-ctrl-alt-fgreps through the project, and is usually faster than Find in Project
cmd-alt-down arrowswitches from an action to a view or model to unit test
shift-cmd-alt-down arrowbrings up the Go To menu
cmd-shift-tdisplays the symbol navigator
rails.vim has loads of equivalents, which uses
:A (alternate) and
:R (related) for easy jumping between files.
Bonus Stage: Workflow–dependent desktops
If you’ve got Mac OS Leopard or Linux you’ve probably tried out workspaces. Why not set up a work–friendly space, with terminals, editors, and everything else all set up so you can access them easily?
Here are a few more ideas:
- Use Mac OS’s Automator. Leopard introduced macro–like functionality, so creating workflows is even easier
- Make the most of Quicklook in Mac OS
- Use smart folders and folder actions (ideas: search for project files, or even search for files that have been tagged in some way)
- Try using some widgets: Dashboard
Super–Secret Bonus Stage: Stop working
Play more videogames. You can use the excuse that you’re researching their interfaces for ideas, it’s the perfect crime!