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

The number of the most recent history event



$#history is the number of history -entries-, NOT the number of history
-events-; it never exceeds $HISTSIZE.  So, how does one get the number of
the most recent event?  I've come up with two ways:

1.	$[${(%):-%h}-1]

The $[...-1] is necessary because %h expands to the number of the entry
that is -about to be- filled in.

2.	$[${${(@O)${(@kl:5:)history}}[1]}]

The outer $[...] is necessary to force integer interpretation, thereby
stripping off the leading spaces from (l:5:), and the two (@) are needed
because $[...] causes a double-quote context.  Of course (l:5:) assumes
you haven't executed more than 99999 commands in the current shell.

If we were to add an (n) flag, e.g. (On) and (on) sort numerically (like
(Oi) and (oi) do case-independently), ${${(Onk)history}[1]} would do it,
as would ${${(onk)history}[-1]} which might be a bit clearer.

Or maybe we should just add the history number to the parameter module?

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author