简体   繁体   English

如何在 Vim 中找到 Django 模板标签并用块标签替换它们?

[英]How to find Django template tags and replace them with block tags in Vim?

I need to change several instances of Django templates that look like this:我需要更改如下所示的 Django 模板的几个实例:

{% block content %}{{ mycontents }}{% endblock content %}

to this:对此:

{% templatetag openblock %} block content {% templatetag closeblock %}{% templatetag openvariable %} mycontents {% templatetag closevariable %}{% templatetag openblock %} endblock content {% templatetag closeblock %}

How can I construct a find-and-replace command to do this?如何构建查找和替换命令来执行此操作? I usually use Vim for this kind of thing, but I'm open to other alternatives.我通常将 Vim 用于这种事情,但我对其他选择持开放态度。

One can use the following two substitution commands for transforming the block and variable contents respectively:可以使用以下两个替换命令分别转换块和变量内容:

:%s/{%\(.\{-}\)%}/{% openblock %}\1{% closeblock %}/g
:%s/{{\(.\{-}\)}}/{% openvariable %}\1{% closevariable %}/g

I would personally do it with a macro.我个人会用宏来做。 this will only work if "mycontents" are all on one line这只有在“mycontents”都在一行时才有效

save macro with:保存宏:

:let @q='/{% block wcwtemplatetagw"aciwopenblockf}a block a {% templatetag closeblock %}{% templatetag openvariable %}l2dlf}C{% templatetag closevariable %}{% templatetag openblock %} endblock a {% templatetag closeblock %}'

and @q to use@q使用

edit ooookkkk I should have guessed that pasting special characters from vim wouldn't work编辑 ooookkkk 我应该猜到从 vim 粘贴特殊字符是行不通的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM