django + vim
Posted October 17th, 2008 by andrewI’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
October 17th, 2008 at 10:48 pm
wow. i understood that :S. if i ever need to use this, i know where to look.
October 18th, 2008 at 8:21 am
“It’s like he’s trying to speak to me, I know it!” - Marlin the clownfish
October 19th, 2008 at 3:59 am
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/
October 20th, 2008 at 10:31 am
@Syz: One of the best lines from one of the best movies ever!!
And it applies in this situation for me, too!
October 22nd, 2008 at 4:27 pm
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!
October 22nd, 2008 at 6:43 pm
@james: Are you using bash as your shell? It’s all on one line (the command).
October 22nd, 2008 at 6:46 pm
@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
December 19th, 2008 at 8:05 pm
Thanks the author!
January 15th, 2009 at 11:49 am
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!
August 9th, 2009 at 5:45 pm
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!
September 12th, 2009 at 3:43 pm
@ Don:
#/bin/bash
PROJECT=`python -c “import os; print os.path.basename(os.getcwd())”`
DJANGO_SETTINGS_MODULE=$PROJECT.settings mvim $@
September 14th, 2009 at 4:05 pm
[…] omnicomplete […]
September 14th, 2009 at 4:05 pm
[…] omnicomplete […]