Tuesday, December 29, 2009

VIM Destroys All Other Rails Editors

Updated on February 06, 2010 to account for updates in pathogen.vim 1.2

Updated on February 10, 2010 to account for set up difficulties some Linux users were experiencing

Now that I have your attention, I've come across quite a few blog and forum posts from people looking for "the best" text editor or IDE to use for Ruby on Rails development. I was fortunate enough to meet Tim Pope soon after I got interested in Rails and was subsequently introduced to the awesomeness that Vim is capable of. It took some ramp up time to get comfortable with modal editing but now I find myself frustrated when I don't have all the power that comes from Vim's modal mindset.

Flame War Disclaimer

What you use to edit code is any other tool and it comes down to personal preference. Personally I LOVE Vim. There are hundred different ways you could set up Vim. This is the setup that's worked best for me. Hope you enjoy it as well.

First things first

Install MacVim or Gvim (For Debian based distros install with apt-get). I do frequently use terminal Vim or Vi but I like having both available. If you're on Windows you probably want to spin up a Linux box or a VM. Ruby on Rails development on Windows is going to painful.

Let's Get Plugged In

Download pathogen.vim it makes managing Vim plugins a lot simpler. Create a ~/.vim/autoload/ directory and copy pathogen.vim into it.
$ mkdir ~/.vim
$ mkdir ~/.vim/autoload
$ cp ~/Downloads/pathogen.vim ~/.vim/autoload/
Next you need to create and edit your .vimrc file so that pathogen gets loaded in the correct order.
$ mvim ~/.vimrc
Content:
filetype off

call pathogen#runtime_append_all_bundles()

syntax on
filetype plugin indent on
set tabstop=2
set smarttab
set shiftwidth=2
set autoindent
set expandtab
The "filetype off" is necessary for some Linux distros. If you're on OS X you should be able to omit that though it won't hurt either way if you leave it in.

Create a .gvimrc to specify colorscheme
$ mvim ~/.gvimrc
Content:
colorscheme vividchalk
I've also become fond of the molokai colorscheme. But its all personal preference. You can also forgo the .gvimrc file and just specify your colorscheme in .vimrc but if you are using terminal vim loading some colorschemes doesn't work very well. Putting it in the .gvimrc will only load the custom colorscheme when you are in Gvim or MacVim.

http://www.vim.org/scripts/script.php?script_id=2340

Create a ~/.vim/bundle directory and install plugins. I prefer to clone them from github.com when I can. Some plugins still need to be downloaded from vim.org, extracted and copied to your .vimbundles directory. Either way go vote up plugins you find useful on vim.org. If you don't have git installed check out git-scm.com
$ mkdir ~/.vim/bundle
$ cd ~/.vim/bundle
Install rails.vim
$ git clone git://github.com/tpope/vim-rails.git
Install cucumber.vim
$ git clone git://github.com/tpope/vim-cucumber.git
Install vividchalk.vim
$ git clone git://github.com/tpope/vim-vividchalk.git
Install haml.vim (There are two. I use Tim Pope's.)
$ git clone git://github.com/tpope/vim-haml.git
Install endwise.vim
$ git clone git://github.com/tpope/vim-endwise.git
Install surround.vim
$ git clone git://github.com/tpope/vim-surround.git
Install unimpaired.vim
$ git clone git://github.com/tpope/vim-unimpaired.git
Install abolish.vim
$ git clone git://github.com/tpope/vim-abolish.git
Install repeat.vim
$ git clone git://github.com/tpope/vim-repeat.git
Install NERD_tree.vim
$ git clone git://github.com/scrooloose/nerdtree.git
Install NERD_commenter.vim
$ git clone git://github.com/scrooloose/nerdcommenter.git
Restart Vim and you now have a super powerful Rails editor at your fingertips. Check out each of the plugins for documentation of features.

Technorati Tags: , ,

45 comments:

  1. With a little bit of @tpope's help, I came up with this install script. I think you'll like it: http://bit.ly/3DeRUn

    ReplyDelete
  2. If your looking for a good setup for windows and gvim, check this out. http://akitaonrails.com/2009/1/13/the-best-environment-for-rails-on-windows

    ReplyDelete
  3. You can also get a sweet vim setup via github. Check out my vim files here:

    http://github.com/kevincolyar/vimfiles

    ReplyDelete
  4. I love Vim for Ruby, but didn't know Endwise. Thanks for that.

    Along, the same lines I'm a big fan of AutoClose. It gives you auto-insertion of closing halves of {, [, (, " and '.

    ReplyDelete
  5. The point of pathogen.vim being in autoload is that you don't have to manually source/runtime it before using its functions.

    And there's no need to source gvimrc from vimrc. Gvim/MacVim loads it anyway.

    ReplyDelete
  6. graywh, you actually do need to manually source/runtime for this setup to work. That is straight from Tim Pope, the author of pathogen.vim and I have tested to confirm.

    You don't have to source .gvimrc from vimrc and will work fine. That's up to personal preference on formatting I want displayed when I'm in terminal vim.

    ReplyDelete
  7. Updated the post per a clean up of my dot files today after a conversation with "the Pope"

    ReplyDelete
  8. Wow... I use vim daily for rails work but always felt like I could go beyond just FuzzyFinder and NerdTree. Thanks for seriously pimping my ride!!!

    ReplyDelete
  9. Screenshots of what?! (Also, why are we yelling?)

    ReplyDelete
  10. Serious question--I have been thinking about learning vi/vim but with this setup, what do I do to breakpoint and step-through ruby/rails code?

    ReplyDelete
  11. You might want to check out http://github.com/astashov/vim-ruby-debugger I haven't used it but could provide what you're looking for.

    ReplyDelete
  12. Vim is awesome, after you have spent a month memorising keyboard shortcuts. Most people give up after 5mins.

    Of course true geeks never give up.

    P.S: I still prefer aptana :)

    ReplyDelete
  13. great stuff, but PLEASE post follow-up with some of the more popular techniques/key-commands you use for jumping around in a rails project and being productive. i've tried to make the jump from textmate to vim several times but i keep giving up because it's just SO much faster to jump between files in textmate (e.g. command-t vs. :Rmodel blahblahblah).

    ReplyDelete
  14. What about integrate version control?

    ReplyDelete
  15. Thanks for this, I hadn't come across pathogen for some reason. I hope everyone who finds tpope's scripts useful has donated a few bucks through his github pledgie.

    Cheers,

    Chris

    ReplyDelete
  16. trak3r, you can use gf in a lot of situations (go to file) but I have been meaning to do a follow up to my Vim Cards post with the more specific Rails features. Here is the set for my core Vim commands I use.

    http://www.adamlowe.me/2009/07/vim-cards-how-i-learning.html

    Also if you're in Jacksonville, FL you should stop by the Hashrocket Offices and let Tim Pope and Veez show you just how fast editing Rails in Vim can be.

    ReplyDelete
  17. vimrails.org, If you're using git then check out Tpope's fugitive.vim

    http://github.com/tpope/vim-fugitive

    ReplyDelete
  18. While this post is helpful, it would be 100x more so if you recorded and posted a screencast demonstrating how you use your setup.

    ReplyDelete
  19. I've never manually sourced it and my setup works just fine.

    silent! call pathogen#...

    ReplyDelete
  20. graywh is correct about the explicit runtime and guard clause not being strictly necessary. I often do it in example files so that things don't blow up if pathogen.vim isn't installed.

    ReplyDelete
  21. Nice writeup. I've redone my vim setup to follow and really like the vimbundles setup.

    I did add the following to my vimrc to use a shortcut for NERDTree:

    let mapleader = ","

    map nt :NERDTree

    Do you have any more shortcuts like this?

    Thanks again for the writeup!

    ReplyDelete
  22. I pretty much just use the keymaps from the plugins listed above, standard Vim, and other then that just what I've put in slurper.vim for story cards.

    ReplyDelete
  23. Great stuff! Now I'm trying to switch to Vim, thanks for this article Adam

    ReplyDelete
  24. I stand corrected by graywh and The Pope. I have amended the .vimrc content accordingly. You can indeed simply use:

    call pathogen#runtime_prepend_subdirectories(expand('~/.vimbundles'))

    ReplyDelete
  25. Check out snipMate too ... http://www.vim.org/scripts/script.php?script_id=2540 (I had to type that in by hand so if it doesn't work, just look for snipMate on vim.org). It's been a huge help for ruby (and indeed all language development) for me.

    ReplyDelete
  26. Note to moderator, the leader stuff and cr commands that have angle brackets around them are probably going to be stripped out, so if that doesn't come through, you might not want to post or it will mislead.

    let mapleader = "," Easier to reach fingers
    map t :execute 'NERDTreeToggle ' . getcwd() Toggle nerdtree
    map r :TlistToggle Toggle ctags
    Tlist_Use_Right_Window = 1 Opens Tags in the "right" window, and nerdtree is in left.
    map g :FufFile**/ Toggle FuzzyFile Finder
    map b :FufBuffer**/ Toggle FuzzyBuffer Finder
    map s w Jump from screen to screen, such as Nerdtree to main window and then to ctags window.
    nmap n :tabnew New Tab
    nmap :tabn Tab Next
    nmap :tabn Also Tab Next
    nmap :tabp Tab Previous
    namp :tabp Also Tab Previous
    nmap :tabclose close tab
    nmap :C-]> : easier jumping to links/files under the cursor
    nmap w :on easy close all windows but primary one you are in

    Another big one is I have supertab completion on, and set to complete on , to autocomplete words and snippets.




    nmap a :!exctags -R --exclude=.svn --exlude=.git --exlude=log * easy ctags regeneration

    autocmd TabLeave * wincmd p auto open and close Nerdtree
    autocmd TabEnter * NERDTreeMirror same
    autocmd TabEnter * wincmd p same

    I also use folding, but the commands are easy on my fingers, so I don't remap.

    ReplyDelete
  27. The learning curve for Vim on Rails is too high. It puts me off. I mean, are you saying I have to download ALL those plugins and read about them before I can use them? there's a lot of plugins.

    Someone should package all this up into an easy to use interface. Configuring VIM and selecting/finding decent packages should surely be made easy to do?

    ReplyDelete
  28. Too high for what? If you are a developer or serious about learning software development the above set up IS simple. There is no quick, easy path to learning a powerful tool like Vim or a complex craft like software development.

    ReplyDelete
  29. Heads up folks due to some updates to pathogen.vim you'll need to make some changes to your setup if you want to update to pathogen.vim 1.2. I've amended the instructions above accordingly.

    ReplyDelete
  30. But why does Vim destroy all other Rails editors?

    ReplyDelete
  31. The speed and features it provides are far better than any of the other editors out there IMO. Modal editing is much more pleasant and efficient than having to context switch to slower mouse movements required by most of the other editors. Again it's person preference and this is mine. The title was meant to be a hook to generate interest and discussion.

    ReplyDelete
  32. Thanks for this interesting post about your vim config.

    I installed all the plugins you suggested and adjusted my .vimrc according to your instructions.

    The plugins are working fine, but I could not get the helpfiles working. I'm wondering if this might be an issue with the way the plugins are loaded using pathogen?

    ReplyDelete
  33. Does vim-cucumber work with pathogen? I'm not gettin any of the features nor syntax highlight once cloned into the bundle dir (The other plugins seem to work like a charm).

    ReplyDelete
  34. It does indeed work with cucumber. Some users on debian based linux distros have experienced an issue that can be resolved by amending your .vimrc as follows:

    filtype off

    call pathogen#runtime_append_all_bundles()

    syntax on
    filtype plugin indent on

    It looks like debian turns filetype on automatically for you before it calls pathogen and is what causes the problem. This turns it off, calls pathogen and then turns it back on. Should fix your issues.

    ReplyDelete
  35. Thank ya.

    But, bhould it be `filetype` and not `filtype`? Was getting some errors when I tried this verbatim.

    ReplyDelete
  36. @roman to generate all the help docs try

    `:call pathogen#helptags()`

    This will generate the help files for the bundles

    ReplyDelete
  37. J. Weir, thanks for heads up on the typo. Corrected.

    ReplyDelete
  38. Hey Adam, this is great, I was wondering why you or others aren't using git submodules for this?

    ReplyDelete
  39. There was a question about using Git submodules for this setup. For some reason it didn't show up after approving.

    I'm not a fan of submodules in general. Just haven't found a good justification yet. I wanted to give people a good foothold of the core items they would need to get a solid Ruby and Rails editor with Vim but not tie them to any of them or a certain way of installing/maintaining, especially one that might confuse newer folks.

    You could do submodules or just script it out and have an alias for your script. We have scripts for our core setup at Hashrocket. I update incrementally as necessary.

    ReplyDelete
  40. Definitely the best development environment. I love VIM and VIM + Rails looks good with Rails.

    ReplyDelete
  41. I really like this setup. Simple yet elegant and exactly what i need.

    ReplyDelete
  42. I'm getting issues with the vim-cucumber. I have the amended .vimrc file:

    filetype off

    call pathogen#runtime_append_all_bundles()

    syntax on
    filetype plugin indent on

    But I still get no syntax highlighting for my .feature files. The identing seems to be working though.

    ReplyDelete
  43. I experienced an issue after the latest MacVim update and have since uninstalled and then reinstalled via homebrew which solved my problems with stuff not loading properly from my .vimrc.

    Try this in your .vimrc file:

    runtime! autoload/pathogen.vim
    if exists('g:loaded_pathogen')
    call pathogen#runtime_prepend_subdirectories(expand('~/.vimbundles'))
    endif

    syntax on
    filetype plugin indent on

    if filereadable(expand('~/.vimrc.local'))
    source ~/.vimrc.local
    endif


    I would have a look at installing through Hashrocket's dotmatrix if that doesn't work.

    http://github.com/hashrocket/dotmatrix

    ReplyDelete

Constructive and informative comments are appreciated. They are also moderated. I review them as quickly as I can.