Django-Vim code autocomplete

September 2013 · 1 minute read

Recently I came back to Django development and after a year my set of tools had change a lot. Mainly because vim became my main text editor.

The following lines of code will turn vim into a more powerful tool to work with Django and Python, place them inside your .vimrc:


filetype plugin on
set omnifunc=syntaxcomplete#Complete
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

"--SuperTab Integration
set completeopt-=previewtj
let g:SuperTabDefaultCompletionType = ""
let g:SuperTabDefaultCompletionType = "context"

First lines are activating the Omni Completion feature of Vim for those type of files and the last lines reference Super Tab which installed as a plugin.

Now we will define a wrapper, paste this lines inside a file called djvim (DON’T FORGET TO MODIFY YOUR PROJECT PATH):

if [ $# -eq 2 ]
        export PYTHONPATH="$PROJECT_PATH/$1"
        export DJANGO_SETTINGS_MODULE=$1.settings
        vim $2
        echo "Usage: djvim [Project] [File]"

then make the file executable and move it to your /bin folder, that will do the trick. Test djvim with:

<code>[email protected]:$ djvim myproject

and inside Vim type:

<code>:python from django import db

Then test the autocomplete with some functions, launch it with the Tab key.