Code


Version 21 (modified by Lllama, 8 years ago) (diff)

Updated snippetsemu info to use more explicit command name

Using Vim with Django

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

  • Tips:
    • Vim Modeline
  • Plugins:
    • snippetsEmu
    • xml.vim
    • Project
  • Vim/IDE:
    • PIDA

Tips

Vim Modeline

To insure that all developers are using a standard tab format (as long as they are using Vim), you can add a Vim modeline (special comment) to your files, to set people in the same mode for that file. Example:

# vim: ai ts=4 sts=4 et sw=4

Tab Setting Reference for Python and Vim

Plugins

snippetsEmu

Development takes place on Google Code, where the subversion repository includes predefined snippets for django/python/etc.

SnippetsEmu 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/after/ftplugin/python.vim file (create it if you don't have it) will allow you to define abbreviations just for models (use the file \Program Files\vim\vimfiles\after\ftplugin\python.vim under Windows):

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

The following is an example collection of Django specific abbreviations for use with the plugin. More examples can be found in the subversion repository. Please email the author with any of your own additions (f dot ingram dot lists at gmail dot com):

Models:

Snippet addmodel class <{}>(models.Model):<CR><><CR><CR>def __str__(self):<CR>return "%s" % (<{}>,)<CR><CR>class Admin:<CR>pass<CR><CR>
Snippet mcf models.CharField(maxlength=<{}>)<CR><{}>
Snippet mff models.FileField(upload_to=<{}>)<CR><{}>
Snippet mfpf models.FilePathField(path=<{}>, match="<{}>", recursive=<False>)<CR><{}>
Snippet mfloat models.FloatField(max_digits=<{}>, decimal_places=<{}>)<CR><{}>
Snippet mfk models.ForeignKey(<{}>, edit_inline=<False>)<CR><{}>
Snippet m2m models.ManyToManyField(<{}>)<CR><{}>
Snippet o2o models.OneToOneField(<{}>)<CR><{}>

Templates:

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

XMLEdit.vim

The xmledit.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

Dave Hodder has contributed standard Vim syntax files.

If you are interested in rolling your own, 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

Project.vim

The Project plugin adds IDE file organisation functionality to Vim.

Something like the following could be used to view a project's files. It looks scary but is definatly worth trying.

Django Project=/path/to/project CD=. filter="*.py" {
 settings.py
 urls.py
 apps=apps {
  Polls=polls {
   views.py
   Models=models {
    models.py
   }
  }
  ToDo=todo {
   views.py
   Models=models {
    models.py
   }
  }
 media=mediaDir {
  images=imagesDir {
  }
 }
}

Such a structure can also be created by using the build in '\C' command which will recurse through the whole directory tree.

Vim/IDE

PIDA

Although this is not Django-specific (I don't think some of the other stuff in here is very Django-specific either), PIDA looks like a pretty nice IDE environment for VIM+Python, which would therefore help you out in Django :).

Attachments (5)

Download all attachments as: .zip