Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim inner tag blocks for Django templates

Does a plugin exist which provides these text objects for Django templates? For example, in HTML/XML if I have the following text

<p>This is some text.<p>

and my cursor is positioned anywhere inside the tags, I can use cit to change the text inside the tags. I'm looking for something similar for Django templates. For example, with the text

{% block title %}This is the title{% endblock %}

I'd like the same behavior. If nothing exists, any vimscripters out there know if this is possible and willing to give some hints?

Thanks!

like image 494
Michael Mior Avatar asked Oct 07 '22 21:10

Michael Mior


1 Answers

Have a look on the official Django documentation. There is a "Using Vim with Django" section.

https://code.djangoproject.com/wiki/UsingVimWithDjango

and a surround mapping part.


Here is an extract :

let b:surround_{char2nr("v")} = "{{ \r }}"
let b:surround_{char2nr("{")} = "{{ \r }}"
let b:surround_{char2nr("%")} = "{% \r %}"
let b:surround_{char2nr("b")} = "{% block \1block name: \1 %}\r{% endblock \1\1 %}"
let b:surround_{char2nr("i")} = "{% if \1condition: \1 %}\r{% endif %}"
let b:surround_{char2nr("w")} = "{% with \1with: \1 %}\r{% endwith %}"
let b:surround_{char2nr("f")} = "{% for \1for loop: \1 %}\r{% endfor %}"
let b:surround_{char2nr("c")} = "{% comment %}\r{% endcomment %}"

Put the above in ~/.vim/ftplugin/htmldjango.vim.

Examples in visual mode (select some text first):

  • type Sv or S{ for a variable
  • type Sb for a block
  • type Si for an if statement
  • type Sw for a with statement
  • type Sc for a comment
  • type Sf for a for statement
  • type S% for other template tags

PS : another possibility is the use of eclim (vim + eclipse) which supports Django project and template editing http://eclim.org/vim/python/django.html but that's a lot heavier.

like image 193
Nic_tfm Avatar answered Oct 12 '22 22:10

Nic_tfm