Reply to message «Re: Suffix alias for README files»,
sent 21:25:43 20 April 2011, Wednesday
by John Eikenberry:
> Is there any way to get either of these to work without changing the
> command that is stored in history? So the history contains
> '/some/path/README' and not 'less /some/path/README'?
Yes, of course. I personally use the following code:
function zshaddhistory()
{
emulate -L zsh
if ! [[ -z "${_HISTLINE}" ]] ; then
print -sr -- "${_HISTLINE}"
unset _HISTLINE
else
print -sr -- "${1%%$'\n'}"
fi
fc -p
}
With this function if you set variable _HISTLINE somewhere before zshaddhistory
hook is run then contents of _HISTLINE will be put into the history. Widget
accept-line is called before zshaddhistory.
Here is where I actually use _HISTLINE (with just the same purpose: execute one
command, but add to history another):
function _-accept-line()
{
emulate -L zsh
local -r autopushd=${options[autopushd]}
options[autopushd]=off
cd $PWD || cd
options[autopushd]=$autopushd
if [[ ${BUFFER[1,3]} == ":h " ]] ; then
_HISTLINE=$BUFFER
BUFFER=":h ${(q)BUFFER[4,-1]}"
elif [[ ${BUFFER[1,4]} == "zmw " ]] ; then
_HISTLINE=$BUFFER
BUFFER="zmw "${(j. .)${(q)${(z)BUFFER[5,-1]}}}
fi
zle .accept-line
}
Original message:
> Bart Schaefer wrote:
> > > I can use suffix aliases to display various .txt files.
> > > Can I do a similar thing for README files? So the "command"
> > >
> > > /some/path/README
> > >
> > > will really run the command
> > >
> > > less /some/path/README
> >
> > Depending on your version of zsh, you can do this either with the
> > zle-line-finish widget or by replacing the accept-line widget.
> >
> > zle-line-finish() {
> >
> > setopt localoptions extendedglob
> > if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> > then BUFFER="less $BUFFER"
> > fi
> >
> > }
> > zle -N zle-line-finish
> >
> > Or
> >
> > accept-line() {
> >
> > setopt localoptions extendedglob
> > if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> > then BUFFER="less $BUFFER"
> > fi
> > zle .accept-line "$@"
> >
> > }
> > zle -N accept-line
>
> Is there any way to get either of these to work without changing the
> command that is stored in history? So the history contains
> '/some/path/README' and not 'less /some/path/README'?
Attachment:
signature.asc
Description: This is a digitally signed message part.