[sf-lug] vi/ex: % and # in ex command arguments: Re: entering dates in vim text editor

Michael Paoli Michael.Paoli at cal.berkeley.edu
Mon Nov 16 00:10:31 PST 2020


> From: "Steve M Bibayoff" <bibayoff at gmail.com>
> Subject: Re: [sf-lug] entering dates in vim text editor
> Date: Sun, 15 Nov 2020 22:32:29 -0800

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

"Slide" 20, 2nd bullet point:
o in most file and shell commands, % will
   substitute current file name, and # will
   substitute "alternate" file name, and ex will do
   shell metasyntax expansion of file names given
http://www.mpaoli.net/~michael/unix/vi/vi.odp
I'm sure I read that in some definitive reference
documentation on vi(1) once upon a time somewhere.
Not super easily spotting right off-hand exactly
where that's documented.  But it's very long established
defacto standard behavior in vi ... though I don't see ...
oh wait, there it is - brain fart - of course it's well
and clearly documented ... on ex(1), even POSIX:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
"Non- <backslash>-escaped '%' characters appearing in file arguments
to any ex command shall be replaced by the current pathname;
unescaped '#' characters shall be replaced by the alternate pathname."
As, after all, ex and vi are the same programs - they just normally
start off in different modes - and one can switch modes, regardless of
which way vi/ex was invoked, e.g.,
in visual mode:
Q
to quit visual mode and drop to ex mode, or in ex mode:
visual
which can be abbreviated as short as the shortest unique
abbreviation for it:
vi
will take one from ex mode to visual mode.
So ...
$ cd $(mktemp -d)
$ echo a > a && echo b > b && ex [ab]
a: 2 files to edit: unmodified: line 1
:1,$p
a
:!echo %
!e
a
!
:!echo \%
%
!
:n
b: unmodified: line 1
:1,$p
b
:!echo \% \\#
% #
!
:e#
a: unmodified: line 1
:!echo % # \% \\#
!e
a b % #
!
:q
$
Oh, and why \\# rather than \#?  Because # is also special to the
shell, so \# to protect it from ex replacing it with filename,
and then atop that, some means of quoting it from shell interpretation.
ex (ex mode of vi), likewise ed, also very handy for documenting edit
changes, such as via script(1).
And another quick bit of example:
$ >a && >b && ex [ab]
a: 2 files to edit: unmodified: line 1
:arg
[a] b
:!echo % #
No filename to substitute for #
:n
b: unmodified: line 1
:e#
a: unmodified: line 1
:arg
a [b]
:!echo % #
!e
a b
!
:$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
$

> https://vim.fandom.com/wiki/Insert_current_date_or_time

Ugh, yek, don't need no steenkin' vim.  ;-)
I (and others too) find vim highly annoying!:
http://www.mpaoli.net/~michael/linux/vim/vim_annoyances.txt




More information about the sf-lug mailing list