Date in a boilerplate file

69 views
Skip to first unread message

Guido Milanese

unread,
Jun 23, 2020, 12:56:34 PM6/23/20
to vim_use
In a project I am developing, I have written some boilerplate files to be used as headers for Markdown/LaTeX documents. One of the lines contains the document date, and ideally it should be:

date: <TODAY>

I know how to insert date from command line, but is it possible to embed the command in the boilerplate file and have it transformed into the real date? I tried autocmd to no success -- clearly I have not really understood how to use it!
The same applies to other fields (such as AUTHOR), but the DATE field is the most important one.
Thank you!
guido (Northern Italy)


Paul

unread,
Jun 23, 2020, 1:17:09 PM6/23/20
to vim_use
I would have the template as you have it above, and an autocmd on "BufNewFile *.md,*.latex" or whatever, that inserts it, with "0r file", then does a substitution of "<TODAY>" with "\=system('date')" or whatever you want. It would have been quicker to just write it than explain it:

autocmd BufNewFile *.md,*.latex 0r $HOME/.vim/templates/markdown-latex.txt | %s/<DATE>/\=system('date')->trim()/eg | normal G

Or something :)
signature.asc

Tim Chase

unread,
Jun 23, 2020, 1:34:04 PM6/23/20
to Guido Milanese, vim...@googlegroups.com
On 2020-06-23 09:56, Guido Milanese wrote:
> One of the lines contains the document date, and ideally it should
> be:
>
> date: <TODAY>
>
> I know how to insert date from command line, but is it possible to
> embed the command in the boilerplate file and have it transformed
> into the real date? I tried autocmd to no success -- clearly I have
> not really understood how to use it!
> The same applies to other fields (such as AUTHOR), but the DATE
> field is the most important one.

Assuming the notation <var> for your variables, you could do
(automate) something like the following

:%s/<TODAY>/\=strftime('%c')/ge
:%s/<AUTHOR>/\=$USER/ge

If you have a lot of them, you could simplify them to a single
replacement with something like

:%s/<\([^>]*\)>/\=get({'TODAY':strftime('%c'), 'AUTHOR':$USER}, submatch(1), submatch(1))

putting as many tokens and their respective values as you want in
that hard-coded dictionary/mapping.

-tim






aro...@vex.net

unread,
Jun 23, 2020, 1:59:43 PM6/23/20
to vim...@googlegroups.com

> I know how to insert date from command line, but is it possible to embed
> the command in the boilerplate file and have it transformed into the real
> date?

In order to keep track of file versions, I have a .vimrc file that updates
a timestamp every time the file is saved. That sounds similar to this
problem.

I've attached the complete file, but in case that has the problem, here's
the text:

" .vimrc with timestamp

function! UpdateTimestamp () " Note silent! suppresses error message if
" no matching string found
silent! '[,']s/^[#\"] End.*Last changed: \zs.*/\= strftime("%Y-%m-%d
%H:%M:%S") /

endfunction

augroup TimeStamping
autocmd!

autocmd BufWritePre,FileWritePre,FileAppendPre * :call
UpdateTimestamp()
augroup END

:set expandtab
:set tabstop=3

" End .vimrc Last changed: 2019-08-22 15:43:18

As you can see, the .vimrc itself uses the technique.
.vimrc

Guido Milanese

unread,
Jun 23, 2020, 5:02:41 PM6/23/20
to vim_use
Thank you very much for this suggestion. I think I will map this 's' command to an available <F...> key. An additional question, if you do not mind: why

submatch(1), submatch(1)

and not

submatch(1), submatch(2)

Thank you again!
guido

Guido Milanese

unread,
Jun 23, 2020, 5:04:13 PM6/23/20
to vim_use
Very generous, thank you. I'll study your code and I'm sure it will help me a lot.
guido

Guido Milanese

unread,
Jun 23, 2020, 5:06:15 PM6/23/20
to vim_use
Good! Now I think I have understood (at last...) how to use autocmd!
guido

Tim Chase

unread,
Jun 23, 2020, 5:51:49 PM6/23/20
to Guido Milanese, vim...@googlegroups.com
On 2020-06-23 14:02, Guido Milanese wrote:
>> :%s/<\([^>]*\)>/\=get({'TODAY':strftime('%c'), 'AUTHOR':$USER},
>> submatch(1), submatch(1))
>
> if you do not mind: why
>
> submatch(1), submatch(1)
>
> and not
>
> submatch(1), submatch(2)

The \(...\) captures a sub-group, and I only capture one of them, so
there's only submatch(0) (the whole thing including the "<" and ">")
and submatch(1) (the term inside them). If you want to keep the
brackets, the second one could be "submatch(0)" instead of
"submatch(1)".

It does a get() on that static dictionary/mapping, looking up the
term that was in those angle-brackets (the first submatch(1)). If it
finds the term, it returns the corresponding replacement. If it
doesn't find the term in the dictionary/mapping, it returns the
default (the same thing we're looking up, that second submatch(1), or
as mentioned above, you could keep the angle-brackets by returning the
whole original text with submatch(0)).

Hope that helps,

-tim


Guido Milanese

unread,
Jun 24, 2020, 6:32:43 AM6/24/20
to Tim Chase, vim_use digest subscribers
crystal clear!
thanks again,
guido
--
Guido Milanese - Professor of Classics - Docteur H.C. Paris ICP
Universita' Cattolica d.S.C., Milano - Brescia
http://docenti.unicatt.it/ita/guido_fabrizio_milanese

Mateusz Okulus

unread,
Jun 27, 2020, 10:43:47 AM6/27/20
to vim...@googlegroups.com
On Tue, Jun 23, 2020 at 09:56:34AM -0700, Guido Milanese wrote:
> In a project I am developing, I have written some boilerplate files to be
> used as headers for Markdown/LaTeX documents. One of the lines contains the
> document date, and ideally it should be:
>
> date: <TODAY>

I think it's easier to use unix tools instead of vim for this.

sed "s/<TODAY>/$(date)/g" [file]...

You can specify multiple files, and make changes permanent
with -i flag.

Regards,
mat
signature.asc

Guido Milanese

unread,
Jun 28, 2020, 4:43:50 PM6/28/20
to vim_use digest subscribers
Thank you. I was indeed considering something like this -- perhaps a srot of "preprocessing" of the file.
Best regards,
guido

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/MMZU37fWr7g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200627144337.zjtirp4sceegzatc%40debian.

Gary Johnson

unread,
Jun 28, 2020, 8:34:40 PM6/28/20
to vim_use digest subscribers
On 2020-06-28, Guido Milanese wrote:

> Il giorno sab 27 giu 2020 alle ore 16:43 Mateusz Okulus ha
> scritto:
>
> On Tue, Jun 23, 2020 at 09:56:34AM -0700, Guido Milanese wrote:
> > In a project I am developing, I have written some boilerplate files to be
> > used as headers for Markdown/LaTeX documents. One of the lines contains
> the
> > document date, and ideally it should be:
> >
> > date: <TODAY>
>
> I think it's easier to use unix tools instead of vim for this.
>
> sed "s/<TODAY>/$(date)/g" [file]...
>
> You can specify multiple files, and make changes permanent
> with -i flag.

> Thank you. I was indeed considering something like this -- perhaps a srot of
> "preprocessing" of the file.

What I used to do was embed my boilerplate text as a here-document
in a shell script which the script cat'ed into a pipeline of sed
and/or awk scripts to add the date, author name, etc. Then I just
sourced the output of that script into Vim with a ":r!" command.
That kept everything in one file.

The only reason I no longer do that is that my recent jobs haven't
required the creation of a lot of new files with a common
boilerplate format.

Regards,
Gary

Ben Fritz

unread,
Jul 3, 2020, 1:16:28 AM7/3/20
to vim_use
You have some good suggestions already that use hard-coded substitute commands in the autocmd. A more versatile method would be to embed the expressions you want evaluated right in your template file: https://vim.fandom.com/wiki/Use_eval_to_create_dynamic_templates

Of course there are also a wide variety of template plugins that might be a good fit for you, too.
Reply all
Reply to author
Forward
0 new messages