Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: alias vl="vi !$"



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