qqkh-w
Last Updated: February 25, 2016
·
2.987K
· marinftw
Yo

Vim: Django Code AutoComplete

I recently came back on Django development and after a year my own set of coding tools had changed a lot, mainly because Vim became my main text editor. On this came back many of my friends suggested PyCharm as the right tool to use but I just can’t work with an IDE, it just doesn’t feel smooth and fast as Vim.

So, if you are like me and love Vim a simple wrapper will transform Vim into a more powerful tool to work with Django and Python, but before we set up the wrapper we have to configure some things inside or .vimrc:

"--ENABLE PYTHON/DJANGO OMNICOMPLETE

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 are very helpful and comfortable to work with <a href="https://github.com/ervandew/supertab">Super Tab</a> which must be inside of our .vim folder.

Now we will be able to define or little wrapper which I built based on a few examples that I found over the internet, those examples were not working for me but once I understood the idea it was very easy to code.

paste this lines inside a file called djvim (DON'T FORGET TO MODIFY YOUR PROJECT PATH):

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

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

user@machine:$ djvim myproject models.py

and inside Vim type:

:python from django import db

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

if it doesn’t work you can check out my dotfiles <a href="http://github.com/marinhero/dotfiles">repo</a> and use them as personal files or guide.
Happy Coding, improvements are always welcome :)

Say Thanks
Respond

3 Responses
Add your response

9055
V681o1wvc2c

Hi there!
I done actually what you write, but my vim still displayed ImportError.
Can you help me?

over 1 year ago ·
9067
Yo

Did you change the path to the one in your own computer?

over 1 year ago ·
15024
70f2d1f4be99dbcc3391239b9dbc14ec

so that means we have to run djvim every time on a new project?

over 1 year ago ·
Filed Under