The Fluther Blog

Back to Fluther

What is Fluther?

Fluther is a free Q&A collective that specializes in getting
fast answers from the right people. Check it out!

django + vim

1:59 pm

I’ve finally weaned myself off Komodo, since the vim bindings had just enough tiny errors to annoy me, and I was never satisfied with how long it took to find and open a file inside the project.

The one thing I was really missing in vim, though, was solid code-completion feature. After much haranguing, I finally figured out how to enable omnicomplete with django.

Download and install vim for OS X

  • By far the easiest way to get a version of vim that has python compiled is through macports.
    sudo port install vim +python
  • Now replace your old version of vim in your default path (useful for when a shell command calls vim as the editor)
    sudo cp /usr/bin/vim /usr/bin/vim7.0
    sudo cp /opt/local/bin/vim /usr/bin/vim
  • Verify that you have python compiled by typing :python print "Hello" while in vim

Enable omnicompletion

  • I’ve enabled omnicomplete for a bunch of different different completers. Add these lines to your .vimrc:
    autocmd FileType python set omnifunc=pythoncomplete#Complete
    autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
    autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
    autocmd FileType css set omnifunc=csscomplete#CompleteCSS

Run vim so that you can actually complete django files

  • Omnicomplete imports the modules behind the scenes to help you with completion. Problem is, it won’t be able to import django.db since you don’t have a settings module specified… so any module that imports models won’t work. Not very useful.
    DJANGO_SETTINGS_FILE=myapp.settings vim
  • Verify that imports will work by typing :python from django import db
  • Now you can just press <C-X><C-O> to omnicomplete!

I also highly recommend the rope plugin for vim. It simplifies opening files within your project, and even will organize your imports! for you!