https://github.com/freed-wu/template.vim
Powerful template engine/plugin for vim
https://github.com/freed-wu/template.vim
jinja2 template vim
Last synced: 4 months ago
JSON representation
Powerful template engine/plugin for vim
- Host: GitHub
- URL: https://github.com/freed-wu/template.vim
- Owner: Freed-Wu
- License: gpl-3.0
- Created: 2023-02-28T17:39:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-11-24T18:48:43.000Z (7 months ago)
- Last Synced: 2025-11-28T07:09:19.223Z (7 months ago)
- Topics: jinja2, template, vim
- Language: Vim Script
- Homepage: https://luarocks.org/modules/Freed-Wu/template.vim
- Size: 51.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# template.vim
[](https://results.pre-commit.ci/latest/github/Freed-Wu/template.vim/main)
[](https://github.com/Freed-Wu/template.vim/actions)
[](https://github.com/Freed-Wu/template.vim/releases)
[](https://github.com/Freed-Wu/template.vim/releases/latest)
[](https://github.com/Freed-Wu/template.vim/issues)
[](https://github.com/Freed-Wu/template.vim/issues?q=is%3Aissue+is%3Aclosed)
[](https://github.com/Freed-Wu/template.vim/pulls)
[](https://github.com/Freed-Wu/template.vim/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/Freed-Wu/template.vim/discussions)
[](https://github.com/Freed-Wu/template.vim/milestones)
[](https://github.com/Freed-Wu/template.vim/network/members)
[](https://github.com/Freed-Wu/template.vim/stargazers)
[](https://github.com/Freed-Wu/template.vim/watchers)
[](https://github.com/Freed-Wu/template.vim/graphs/contributors)
[](https://github.com/Freed-Wu/template.vim/graphs/commit-activity)
[](https://github.com/Freed-Wu/template.vim/commits)
[](https://github.com/Freed-Wu/template.vim/releases/latest)
[](https://github.com/Freed-Wu/template.vim/blob/main/LICENSE)
[](https://github.com/Freed-Wu/template.vim)
[](https://github.com/Freed-Wu/template.vim)
[](https://github.com/Freed-Wu/template.vim)
[](https://github.com/Freed-Wu/template.vim)
[](https://github.com/Freed-Wu/template.vim)
[](https://github.com/Freed-Wu/template.vim)
[](https://luarocks.org/modules/Freed-Wu/template.vim)
Powerful template engine/plugin for vim.
Template `%.c`, which syntax looks like a subset of
[jinja2](https://github.com/pallets/jinja/).
```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:r') }}.h"{% here %}
#endif
#include
{#-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 }};
}
```
```bash
# Use vim to create a file `test.c` (match the regular expression `\.c$`)
vi test.vim
```
You got
```c
/*
* test.c.bak
* Copyright (C) 2023 Freed
*
* Distributed under terms of the GPL3 license.
*/
#if 0
#include "test.h"
#endif
#include
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](https://github.com/honza/vim-snippets/blob/master/plugin/vimsnippets.vim).
```vim
let g:snips_author = 'Freed'
let g:snips_email = 'Freed@mail.com'
```
Syntax highlight is provided:

## Similar projects
### Vim Template Plugins
- [aperezdc/vim-template](https://github.com/aperezdc/vim-template) converts
`%USER%`, `%MAIL`, `%FILE%`, `%YEAR%`, ...
- [tibabit/vim-templates](https://github.com/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:
1. define some global variables in your `.vimrc` to store those invariable
data. Such as your name, email, ...
2. use `expand('%:XXX')` to get any thing about file path. You can make it
enough complex. Such as you can get perl module name `Foo::Bar::Baz` in
[`foo-bar-baz/Makefile.PL`](https://metacpan.org/pod/ExtUtils::MakeMaker) 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.
3. use `strftime()` to get any thing about date and time.
4. 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. A `test.vim` for
this propose looks like:
```vim
#!/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:
```sh
# change marks
perl -pi -e"s/%DATE%/{{ strftime('%F') }}/g" *
perl-rename 's/^=template=\./%/' *
perl-rename 's/$/%24/' *
```
## Usage
[`:help template`](doc/template.txt)
## 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](https://github.com/kana/vim-smartinput/): input `{`
will get `{|}`, `|` is cursor, then input `%` will get `{%|%}` or input `#`
will get `{#|#}`. Input `` will get `{% | %}`, `{# | #}`, ... See
[my configuration of this
plugin](https://github.com/Freed-Wu/my-dotfiles/blob/main/.config/nvim/autoload/init/smartinput.vim).
- [sheerun/vim-polyglot](https://github.com/sheerun/vim-polyglot) contains jinja2
syntax highlight.
- [My templates for this plugin](https://github.com/Freed-Wu/my-dotfiles/tree/main/.config/nvim/templates)
### 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](https://github.com/google/vimdoc) A tool to generate vim
document from comment.
- [vim-jp/vital.vim](https://github.com/vim-jp/vital.vim) A library of vim
script.
- [thinca/vim-themis](https://github.com/thinca/vim-themis) Unit test framework
for vim script.
- [Other many tools](https://github.com/Freed-Wu/template.vim/tree/master/.pre-commit-config.yaml)