django + vim

Posted October 17th, 2008 by andrew

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!

Explore posts in the same categories: How'd they do that?, Technical, Uncategorized

13 Comments on “django + vim”

  1. mtl_zack Says:

    wow. i understood that :S. if i ever need to use this, i know where to look.

  2. syz Says:

    “It’s like he’s trying to speak to me, I know it!” - Marlin the clownfish

  3. Arnar Birgisson Says:

    I have to disagree that macports is the easiest way to install Vim on OS-X. I much prefer MacVim, and it looks better too:

    http://code.google.com/p/macvim/

  4. HearKat Says:

    @Syz: One of the best lines from one of the best movies ever!!
    And it applies in this situation for me, too!

  5. james Says:

    ok so vim is not seeing the DJANGO_SETTINGS_MODULE when I try this. Are you setting this in the shell before opening vim?
    >DJANGO_SETTINGS_MODULE=mysite.settings
    >vim
    or am I missing something ?
    would be amazing to get this working correctly!

  6. Andrew Says:

    @james: Are you using bash as your shell? It’s all on one line (the command).

  7. Andrew Says:

    @Arnar: I agree. I’m using MacVim as well.

    The problem is that MacVim points to python 2.3 (the default shipping with Panther) — so if you’re running 2.5 like we are, you have to compile it from source (still not too hard to do).

    You can test which version of python your vim is using by running
    :python import sys
    :python print sys.version

  8. emidgeste Says:

    Thanks the author!

  9. Don Says:

    Thanks for this, not sure why I thought setting up Omnicompletion in vim was so hard. On ubuntu, all I had to do was:

    sudo apt-get install vim-python

    and drop the 4 autocmd lines into ~/.vimrc

    Typing DJANGO_SETTINGS_MODULE all the time can get a little tedious, so I also put together a tiny shell script to determine the project’s settings module based on the current directory. I happen to keep all my projects in a single directory (”django”) so the following works great for me:

    #!/bin/sh
    PROJECT=`python -c “import os;print os.getcwd().partition(’django’)[2].split(os.sep)[1]”`
    DJANGO_SETTINGS_MODULE=$PROJECT.settings vim $@

    now I just type “djvim models.py” and I’ve got django-enabled omnicompletion in vim. Hooray!

  10. John Says:

    Hi,
    where do I type this line: ‘DJANGO_SETTINGS_FILE=myapp.settings vim’?
    and I’m guessing I have to type that for every project.. is there a way to automate it on Windows Vista?

    thanks!

  11. Franck Says:

    @ Don:

    #/bin/bash
    PROJECT=`python -c “import os; print os.path.basename(os.getcwd())”`
    DJANGO_SETTINGS_MODULE=$PROJECT.settings mvim $@

  12. Nagy Viktor (V): Vim actually seems to be quite nice for Python/Django development Says:

    […] omnicomplete […]

  13. Nagy Viktor (V): Vim actually seems to be quite nice for Python/Django development Says:

    […] omnicomplete […]

Comment: