less
has not a clue as to what is on the other side of the pipe that is feeding data to it via standard input, unless someone greatly complicates the code for less
to find the process group that less
is running in and other processes in that process group and then who knows how those all fit together (the shell knows this information but may not make it available).
From some reading in less(1)
one may find the -P
option to customize the prompt
$ echo hi | less -P '?f%f:Standard input'
hi
Standard input
so in theory if we can replace the Standard input
bit with the current shell pipeline...let's see what is set
when less
is run...
$ function less { set > whatisset; command less "$@"; }
$ echo foo | less
foo
$ egrep 'echo|foo' whatisset
$
so nothing obvious to use for mksh
, as echo
or foo
(equivalent to your grep
) do not appear anywhere in the shell environment (same story for zsh
and oh wow does bash
on Linux ever spam the environment with settings but again same story). Actually, with zsh
we can use a preexec
function to make the command line available:
$ zsh
% function preexec { shift; SHORT=$1; }
% function less { set > whatisset; command less "$@"; }
% echo foo | less
foo
% grep SHORT whatisset
SHORT='echo foo | less'
%
so one could do something like
% less() { =less -P '?f%f:'${SHORT%% | less} "$@"; }
% echo hi|less
hi
echo hi
grep -H
? Isn't it misleading to show the filename anyway, since it's only grep's output...less -M
to be the grep command used; in my example the command wasgrep asdf text.txt
which would show as the file name in less, just as a simple reminder of what I looked for in the first place.