Loading twitter status...

NOTE : This article is an update to an older wordpress blog I did. Since then I learned a few new tricks and thought this page could use a refresh. Enjoy teh Vim!

I wrote an article not too long ago about installing Ruby on Rails on Ubuntu 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
vimtutor

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
vim-ruby-install.rb
(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.

set nocompatible
syntax on
filetype plugin indent on
set mouse=a

runtime! macros/matchit.vim

augroup myfiletypes
  autocmd!
  autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
augroup END

Also, if you want to use Tab support, you can add the following to your .vimrc file. It will always show you the tab bar and give you a couple of shortcuts. Press CTRL+T to open a new tab, CTRL+N to move to the next tab and CTRL+P to move to the previous tab. With mouse=a, you can also click on the tabs to select them.

set showtabline=2
set tabpagemax=15
nnoremap <silent> <C-n> :tabnext<CR>
nnoremap <silent> <C-p> :tabprevious<CR>
nnoremap <silent> <C-t> :tabnew<CR>

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.

vim
:helptags ~/.vim/doc

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. If your using tabs, you can replace the :R commands with :RT commands.. :RTmodel user will open up a new tab with the user model. 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!

3 Comments

Excellent and much appreciated. I find it far easier to get into the zone and stay there with a vi-based IDE than the big Java-based IDEs (RadRails, Netbeans).

One very minor problem… line numbers are displayed by default. I tried “set nonumber” in my .vimrc, but I believe the plugins get loaded after the .vimrc is read and this had no effect.

There may be a better way, but I ending up adding “set nonumber” at the end of the ~/.vim/syntax/ruby.vim

Thanks again!

Nice tutorial …gr8 work !

I am newb on ubuntu. Can u pl tell me where .vim folder resides ? so i can extract rails.vim plugin over there..

Thanks !

Thanks for the comments! The .vim directory should reside in your home folder (~/.vim) .. if it doesn’t, just make it with mkdir ~/.vim

Leave a Comment

back to top

micro theme by seaofclouds, edited by me, and powered with Mephisto