Date in a boilerplate file

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Date in a boilerplate file

Guido Milanese-4
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)


--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/34e799de-be9e-41ed-8452-eb766daf7ea7o%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Paul-7
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 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.
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 :)

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200623171701.GA18678%40rainslide.net.

signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Tim Chase
In reply to this post by Guido Milanese-4
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






--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200623123352.666d0829%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

arocker
In reply to this post by Guido Milanese-4

> 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.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5fadb327a881c23be100b084a82f404b.squirrel%40webmail.vybenetworks.com.

.vimrc (644 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Guido Milanese-4
In reply to this post by Tim Chase
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

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






--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/974851d2-a371-4ad7-a919-fc462cde026eo%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Guido Milanese-4
In reply to this post by arocker
Very generous, thank you. I'll study your code and I'm sure it will help me a lot.
guido


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.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/8c995f65-b6e9-49ec-b3c4-957d9546e0cfo%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Guido Milanese-4
In reply to this post by Paul-7
Good! Now I think I have understood (at last...) how to use autocmd!
guido

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 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.

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 :)

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/a98f5862-0120-4baf-a4e2-9b20876c2f1do%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Tim Chase
In reply to this post by Guido Milanese-4
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


--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200623165139.21134202%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Guido Milanese-4
crystal clear!
thanks again,
guido

Il giorno mar 23 giu 2020 alle ore 23:51 Tim Chase <[hidden email]> ha scritto:
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 - Professor of Classics - Docteur H.C. Paris ICP
Universita' Cattolica d.S.C., Milano - Brescia
http://docenti.unicatt.it/ita/guido_fabrizio_milanese

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAFsD75THtGthR7GETDwvThVGrxhkQRtV6LuzwNXnx1yBQob6og%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Mateusz Okulus
In reply to this post by Guido Milanese-4
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

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200627144337.zjtirp4sceegzatc%40debian.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Guido Milanese-4
Thank you. I was indeed considering something like this -- perhaps a srot of "preprocessing" of the file.
Best regards,
guido

Il giorno sab 27 giu 2020 alle ore 16:43 Mateusz Okulus <[hidden email]> 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.

Regards,
mat

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200627144337.zjtirp4sceegzatc%40debian.


--
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

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAFsD75TCJW2nBP4VFzd5DvR7G0Yz4VGqSpOFbFhvQK%2BBSyVUOQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Gary Johnson-4
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

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200629003332.GA17697%40phoenix.
Reply | Threaded
Open this post in threaded view
|

Re: Date in a boilerplate file

Benjamin Fritz
In reply to this post by Guido Milanese-4


On Tuesday, June 23, 2020 at 11:56:34 AM UTC-5, 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 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)



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.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/e14f3cb1-e4a4-4488-9879-ca2b377b005eo%40googlegroups.com.