More Ruby on Rails with Ubuntu… (rails.vim)
Posted by dnite on November 10, 2006
UPDATE: I have updated this entry on my new blog at blog.dnite.org. That link is a direct link to the updated article.
I wrote an article not too long ago about installing Ruby on Rails on Ubuntu (Edgy) quickly and easily. The one section I left pretty ‘in the air’ was definitely the IDE. I feel that Linux has the biggest choice when it comes to editor options.
If your using Windows, you’ll probably use RadRails. It’s feature rich. It does everything you need. And windows Java machine just seems to work more efficiently, in my experience, than the ones for linux. If anyone wants to give me some hints to improve my java machines performance, go right ahead. On the mac. You have TextMate. Mac’s also have a lot of options, but from what I’ve heard and seen. Very seldom do you develop Rails applications on a mac without using textmate.
Now, for Linux, you have a variety of things to choose from and I’ve dipped my hands in a few of them. First, I tried RadRails. RadRails is what I used in windows and it was familiar to me. Since it runs on all 3 platforms, I figured I’d give it a shot in Linux. The problem was, that it just seemed a lot slower than it ran in Windows. I’m not exactly sure why this is, but speed and reliability were the reasons I went looking for a better alternative. I did some research and found that some people used jEdit. But getting jEdit to work in Edgy was a chore, do I didn’t even try. gEdit was also another option. It highlights syntax. It can take plugins. It’s a pretty light weight editor. I tried this for a couple of days, but it just didn’t feel right.
Over in the #rubyonrails IRC channel on irc.freenode.net, I heard about vim and a plugin called rails.vim. I’d given vi a try a long time ago. But never a substantial try. It was always really confusing to get into right away. It’s just not like most other editors. But I figured I’d give it a shot anyways. After a week or so of working with Vim and rails.vim, I’m pretty sold. I’m not 100% comfortable with it just yet, but I can definitely see the potential. I find myself hitting ESC and starting to type :wq when quickly editing something in gEdit or something. Rails.vim takes vim a step further and adds a ton of nice Rails features. Here’s what I did to get into and start enjoying using vim in a couple of days.
First and foremost. Make sure you have Vim installed. Pretty simple. The vimtutor command is optional but VERY VERY recommended if you’ve never used vim. Take the half hour to complete the tutor and you will understand vim a lot better.
sudo apt-get install vim-common vim-runtime vim-gnome
Now that you have vim installed, let’s install the vim ruby gem that gives you all the nice syntax highlighting and other cool ruby features. When you run vim-ruby-install.rb, it gives you the option to install to your home folder. I picked this option, so make sure you don’t run this with sudo.
sudo gem install vim-ruby --remote
(select option 1 when it asks where you want to install. /home/username/.vim)
Now you should have Vim installed with some nice ruby stuff. Let’s go ahead and make a startup file to get rid of a few vim annoyances. Create a new file called .vimrc in your home directory (~/.vimrc) and add the following.
filetype plugin indent on
autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
Now, all we have to do is download rails.vim. Head over to rails.vim and download the latest version. There should be a plugin directory and a doc directory in this archive. Extract it to your .vim folder and your all set. The last thing you have to do is enable the rails documentation.
With rails.vim installed, you can launch vim from your project directory and have a lot of new functions. If you try :Rmodel user (which tab completes very nicely), it opens up app/models/user.rb. :Rcontroller blog will open up app/controllers/blog_controller.rb. Everything works just like you would expect it to. You can tab complete the commands and the filenames. If you don’t know what file your looking for, leave the filename off and start hitting tab. You’ll just cycle through all the models or controllers or views. :Rextract will take a currently highlighted section of your code, and automatically create a new partial file while replacing the current selection with the render :partial call. Also, if you move the cursor over another controller name or model name or anything similar and press gf, you will open up that file. It takes a little getting used to but it really works. For a completely list of everything that rails.vim can do, type :help rails. There’s a lot you can do.
Hope that helped out some people! Enjoy!