template.vim
Powerful template engine/plugin for vim.
Template %5C.c%24
(URL encode of \.c$
), which syntax looks like a subset of
jinja2.
/*
* {{ expand('%:t') }}
* Copyright (C) {{ strftime('%Y') }} {{ g:snips_author }} <{{ g:snips_email }}>
*
* Distributed under terms of the GPL3 license.
*/
{# comment #}
#if {{ len([]) }}
#include "{{ expand('%:t:r') }}.h"{% here %}
#endif
#include <stdio.h>
{#-comment, strip left whitespaces #}int
{#-comment, strip around whitespaces-#} main(int argc, char *argv[])
{
printf("'\{\{ string \}\}' is %s", "not variable");
printf("'\{\# string \#\}' is %s", "not comment");
printf("'\{\%% string %\%\}' is %s", "not directive");
{# comment, strip right whitespaces-#} return {{ 1 - 1 }};
}
# Use vim to create a file `test.c` (match the regular expression `\.c$`)
vi test.vim
You got
/*
* test.c
* Copyright (C) 2023 Freed <Freed@mail.com>
*
* Distributed under terms of the GPL3 license.
*/
#if 0
#include "test.h"
#endif
#include <stdio.h>
int
main(int argc, char *argv[])
{
printf("'{{ string }}' is %s", "not variable");
printf("'{# string #}' is %s", "not comment");
printf("'{%% string %%}' is %s", "not directive");
return 0;
}
Your cursor stays in {% here %}
.
Your .vimrc
. The variable names come from
vim-snippets.
let g:snips_author = 'Freed'
let g:snips_email = 'Freed@mail.com'
Similar projects
Vim Template Plugins
- aperezdc/vim-template converts
%USER%
,%MAIL
,%FILE%
,%YEAR%
, ... - tibabit/vim-templates converts
{{NAME}}
,{{EMAIL}}
,{{FILE}}
,{{YEAR}}
, ...
This plugin doesn't provide any mark. You should use your vim script knowledge to get what you want:
- define some global variables in your
.vimrc
to store those invariable data. Such as your name, email, ... - use
expand('%:XXX')
to get any thing about file path. You can make it enough complex. Such as you can get perl module nameFoo::Bar::Baz
infoo-bar-baz/Makefile.PL
by{{ substitute(substitute(expand('%:p:h:t'), '\%(-\|^\)\(.\)', '::\u\1', 'g'), '^::', '', 'g') }}
If you think it is too long, you can define a global function in your.vimrc
, then simply call it. - use
strftime()
to get any thing about date and time. - use
system()
to get any thing about your OS. Such as, when you report bug to a vim plugin, you should provide the basic information of your platforms: Your OS version, your vim version. If this plugin is written in python, ruby, perl, js or any other languages except vim script, you should provide your python version or any other language's version, too. Atest.vim
for this propose looks like:
#!/usr/bin/env -S vi -u
" $ uname -r
" {{ trim(system('uname -r')) }} {#-OS version #}
" $ vi --version | head -n1
" {{ join(split(execute('version'))[0:1]) }}
{#-vim version can be gotten form `:version` which is faster than `system()`-#}
" $ python --version
" {{ trim(system('python --version')) }}
" $ cat test.vim
set runtimepath=$VIMRUNTIME
set runtimepath+=~/.local/share/nvim/repos/github.com/{% here %}
" rest configuration in vim
" $ chmod +x test.vim
" $ ./test.vim
" press some keys or input some commands
" Expected behaviour: work normally
" Actual behaviour: broken, the error information is:
" segmentation fault
BTW, if you change from other vim template plugins, you can do the following works to convert your template formats to let this plugin support them:
# change marks
perl -pi -e"s/%DATE%/{{ strftime('%F') }}/g" *
# change filenames like `=template=.c` to URL encode of `\.c$`
perl-rename 's/^=template=\./%5C./' *
perl-rename 's/$/%24/' *
Usage
Install
From Package Manager
See :help 'your package manager'
.
From Source
Download and extract it to &runtimepath
(See :help 'runtimepath'
).
Other Resources
Tools
- kana/vim-smartinput: input
{
will get{|}
,|
is cursor, then input%
will get{%|%}
or input#
will get{#|#}
. Input<Space>
will get{% | %}
,{# | #}
, ... See my configuration of this plugin. - sheerun/vim-polyglot contains jinja2 syntax highlight.
- My templates for this plugin
Develop
This plugin use the following tools to develop (you don't need to install them if you only want to use this plugin):
- google/vimdoc A tool to generate vim document from comment.
- vim-jp/vital.vim A library of vim script.
- thinca/vim-themis Unit test framework for vim script.
- Other many tools