Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: alias vl="vi !$"
- X-seq: zsh-users 7149
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: alias vl="vi !$"
- Date: Thu, 11 Mar 2004 17:17:35 +0000
- In-reply-to: <4im0509toihq2ef1vlnnid6t93j90f6gor@xxxxxxx>
- In-reply-to: <20040311125558.GA24218@xxxxxxxxxxxxxxxxx>Comments: In reply to Thomas Köhler <jean-luc@xxxxxxxxxxxxxxxxx> "Re: alias vl="vi !$"" (Mar 11, 1:55pm)
- In-reply-to: <svp0509rfu4iecet83auc9pel2i9n4mi9c@xxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <65a050lf2u4bjch4u2uv68q56u5co0gknp@xxxxxxx> <20040311091003.GD844@xxxxxxxxxxxxxxxxxxxxx> <4im0509toihq2ef1vlnnid6t93j90f6gor@xxxxxxx> <15272.1079008435@xxxxxxx> <39n050dv0evle58451ugj2m1vfktsj1e1b@xxxxxxx> <20040311125558.GA24218@xxxxxxxxxxxxxxxxx> <svp0509rfu4iecet83auc9pel2i9n4mi9c@xxxxxxx>
On Mar 11, 12:26pm, zzapper wrote:
> Subject: Re: alias vl="vi !$"
>
> On Thu, 11 Mar 2004 17:10:03 +0800, James Devenish
> >on Thu, Mar 11, 2004 at 08:59:16AM +0000, zzapper wrote:
> >> alias vl="vi !$" where !$ is the command line "last parameter"
> >
> >Make your own 'function' named `vl`?
> >
> Do I have access to the positional parameters of the previous command
> line /history from inside a function?
This brings up an interesting point.
On the command line, $_ is the last word of the previous command, very
much like !$ is. (In the external process environment, $_ is the path
name of the currently executing command.)
Inside a function, however, $_ has already been changed to be the last
word of the _currently executing_ command, the same as as $argv[-1].
Which is not really all that useful, though maybe more useful than if
it had been changed to the path name of zsh or some such.
I'm usually a stickler for backwards compatibility, but does anyone
think anything would break if the changing of $_ were delayed until
after shell functions have been called, so that it would remain the
last word of the _previous_ command?
On Mar 11, 1:55pm, Thomas Köhler wrote:
>
> accept-line-or-empty-cmd() {
> if [ "$LBUFFER$RBUFFER" = vl ] ; then
> LBUFFER="vi !$"
> RBUFFER=""
> fi
> builtin zle .accept-line
> }
Cute, but unecessarily complicated:
if [[ $BUFFER = vl ]]; then BUFFER='vi !$'; fi
It also doesn't really work like an alias. Consider (in csh) that
vl foo
would become
vi !$ foo
whereas your example above would grumble about vl not found.
On Mar 11, 1:23pm, zzapper wrote:
>
> Tejmo emailed me this solution which works!!
>
> vl () {
> gvim.exe $(history -1 | sed "s/.* //g")
> }
That's the right idea, but it's a lot of processes for something you could
do entirely in zsh.
vl () {
zmodload -i zsh/parameter # Just in case
vi "${${(z)history[${${(@kno)history}[-1]}]}[-1]}" "$@"
}
I'm tempted to just leave that unexplained, but I wont't.
${(@kno)history} history numbers in ascending order (note 1)
${${...}[-1]} last element (largest histno) (note 2)
${(z)history[...]} corresponding history entry, split to words
${${...}[-1]} last element (last word of last history entry)
Note 1: The @ is only necessary because the whole thing is inside the
square brackets of ${history[...]}, which forces string context. Thus
the @ converts back to array context.
Note 2: I'd have used ${${(@knO)history}[1]} except that's affected by
the KSH_ARRAYS option. Negative subscripting isn't.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author