Freed-Wu / template.vim

Powerful template engine/plugin for vim

Home Page:https://luarocks.org/modules/Freed-Wu/template.vim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

template.vim

pre-commit.ci status github/workflow

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

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

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 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:
#!/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

:help template

Install

From Package Manager

See :help 'your package manager'.

From Source

Download and extract it to &runtimepath (See :help 'runtimepath').

Other Resources

Tools

Develop

This plugin use the following tools to develop (you don't need to install them if you only want to use this plugin):

About

Powerful template engine/plugin for vim

https://luarocks.org/modules/Freed-Wu/template.vim

License:GNU General Public License v3.0


Languages

Language:Vim Script 98.4%Language:Perl 0.7%Language:C 0.7%Language:Makefile 0.3%