在vim中拥有简单的模板系统 - 包含文件

时间:2014-03-10 14:00:53

标签: vim include skeleton-code

我已阅读vim-wiki关于动态模板的内容,我想要类似,简单的“模板系统”。我创建了一个函数:

function! Read_template(file)
    execute '0r /home/zsolt/.vim/skeletons/'.a:file
    %substitute#\[:EVAL:\]\(.\{-\}\)\[:END:\]#\=eval(submatch(1))#ge
    %substitute#\[:READ:\]\(.\{-\}\)\[:END:\]#??????#ge
endfunction

我想要包含模板中的文件。 EVAL运行良好,但如何解决READ函数?评估包含的文件并不重要。

一个例子:

main.tex

\documentclass[a4paper]{article}
....

exam.tex

% Created [:EVAL:]strftime('%Y. %B. %d.')[:END:]
[:READ:]/path/of/main/main.tex[:READ:]

我执行Read_template("exam.tex")并希望exam.tex包含main.tex

我该怎么做?

2 个答案:

答案 0 :(得分:5)

您需要读取文件并插入其内容。由于你不能使用:read(它将读取整行而不能在替换中调用),你必须使用较低级别的readfile() Vimscript函数,如下所示:

%substitute#\[:READ:\]\(.\{-\}\)\[:END:\]#\=join(readfile(submatch(1)),"\n")/#ge

答案 1 :(得分:1)

您必须解析导入的每一行并应用所需的内容: - 表达替换 - 包含其他模板等(这意味着您必须动态删除和添加行。在mu-template模板扩展引擎的最新版本中,扩展在内存中完成)

仅供参考,我的mu-template工作已经具备此功能:http://code.google.com/p/lh-vim/wiki/muTemplate#Completely_useless_recursive_example