[sf-lug] entering dates in vim text editor

Michael Paoli Michael.Paoli at cal.berkeley.edu
Mon Nov 16 11:53:14 PST 2020


> From: alexkleider <alexkleider at protonmail.com>
> Subject: Re: [sf-lug] entering dates in vim text editor
> Date: Mon, 16 Nov 2020 16:31:35 +0000

> On Sunday, November 15, 2020 10:32 PM, Steve M Bibayoff  
> <bibayoff at gmail.com> wrote:
>
>> On Sun, Nov 15, 2020 at 10:21 PM alexkleider  
>> alexkleider at protonmail.com wrote:
>>
>> > !!date +%c
>> > results in an error.
>> > Can anyone shed any light?
>>
>> https://vim.fandom.com/wiki/Insert_current_date_or_time
>
> Yes, thank you, it does help in that I've learned a way to do what I wanted.
> It doesn't however address the not explicitly expressed question of  
> how to specify a +FORMAT parameter for the date command when using  
> the !!<cmd> vim method of entering the output of a command.

No special snowflake vim variant way of doing it needed,
standard ex means works in vi/nvi/vim/ex, e.g. as shown on:
http://linuxmafia.com/pipermail/sf-lug/2020q4/015078.html
we have:
:$r !TZ=GMT0 date +'\%c files:% #'
!T
Mon Nov 16 07:53:19 2020 files:a b
:1,$p
Mon Nov 16 07:53:19 2020 files:a b
:w
a: 1 lines, 35 characters
:q!
$ cat a
Mon Nov 16 07:53:19 2020 files:a b
$
And yes, even works in vim, e.g.:
$ cd $(mktemp -d) && TZ=GMT0 vim -e foo bar
2 files to edit
"foo" [New File]
Entering Ex mode.  Type "visual" to go to Normal mode.
:r !date +'the time is now: \%c, and this file is named %'
:w
"foo" [New] 2L, 71C written
:n
"bar" [New File]
:r !date +'time: \%c, file:%, alt file:#'
:w
"bar" [New] 2L, 56C written
:e#
"foo" 2L, 71C
:$r !echo and now file is:% and alt is:#
:w
"foo" 3L, 106C written
:q
$ more foo bar | cat
::::::::::::::
foo
::::::::::::::

the time is now: Mon Nov 16 18:56:54 2020, and this file is named foo
and now file is:foo and alt is:bar
::::::::::::::
bar
::::::::::::::

time: Mon Nov 16 18:58:43 2020, file:bar, alt file:foo
$
vim is also relatively funky in that it gives us a blank line that we
didn't at all explicitly add.  Classic/historic vi would do that,
but modern vi/nvi/ex doesn't do that:
$ ex baz
baz: new file: line 1
:r !echo expect this should be the only line in %
!e
expect this should be the only line in baz
:w
baz: new file: 1 lines, 43 characters
:q
$ cat baz
expect this should be the only line in baz
$




More information about the sf-lug mailing list