Code


Version 8 (modified by tim.stoop@…, 9 years ago) (diff)

Just a small spelling correction.

Using Vim with Django

This page is intended to be used as a collection of hints for using the Vim editor with Django.

SnippetsEmu

The SnippetsEmu plugin for Vim allows one to define abbreviations which can be expanded into larger blocks of text. The abbreviations can also contain place markers which can be 'jumped to' in a similar manner to the macros defined in TextMate on OS X.

One can add specific abbreviations for models or templates based on file contents. Adding the following line to your ~/.vim/ftplugin/python.vim file (create it if you don't have it) will allow you to define abbreviations just for models:

if getline(1) =~ 'from django.core import meta'
    "Your abbreviations here
endif

The following is a collection of Django specific abbreviations for use with the plugin (using the default start and end tags: '<' and '>'):

Models:

Iabbr addmodel class <>(meta.Model):<CR><><CR><CR>def __repr__(self):<CR>return "%s" % (<>,)<CR><CR>class META:<CR>admin = meta.Admin()<CR><CR>
Iabbr mcf meta.CharField(maxlength=<>)<CR><>
Iabbr mff meta.FileField(upload_to=<>)<CR><>
Iabbr mfpf meta.FilePathField(path=<>, match=<".*">, recursive=<False>)<CR><>
Iabbr mfloat meta.FloatField(max_digits=<>, decimal_places=<>)<CR><>
Iabbr mfk meta.ForeignKey(<>, edit_inline=<False>)<CR><>
Iabbr m2m meta.ManyToManyField(<>)<CR><>
Iabbr o2o meta.OneToOneField(<>)<CR><>

Templates:

Templates should be used with different tag delimiters as they will no doubt contain (X)HTML which will confuse the plugin. Assuming start and end tags are '@':

Iabbr fore {% for @entry@ in @list@ %}<CR>{{ @entry@.@@ }}<CR>@@{% endfor %}<CR>@@

XML.vim

The xml.vim plugin is really useful for editing XML and HTML files. It makes tag completion easy and allows you to bounce between start and end tags.

Syntax for templates

This is a simple addition to let Vim deal with Django template syntax:

Create the file: $VIM/vimfiles/after/syntax/html.vim with the following content:

syn region  djangotagmarkers start="{{" end="}}"
syn region  djangovariablemarkers start="{%" end="%}"
command! -nargs=+ HiLink hi def link <args>
HiLink djangotagmarkers PreProc
HiLink djangovariablemarkers PreProc
delcommand HiLink

Attachments (5)

Download all attachments as: .zip