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

Re: why won't this function work?



gj@xxxxxxxxxxxxx wrote:
> I'm trying to convert a function I wrote in bash to zsh.  How can I get this 
> to work?

It's a bit more than you asked for, but here it is rewritten to use some
additional zsh features.  I've put comments where it's obscure.  With
the right options, you could get the same function to work in bash and
zsh.  I haven't bothered to try, it doesn't sound like that's what you
were after.


selhist ()
{
     # The following ensures a consistent environment for the function.
     emulate -L zsh

     # $'-style quoting avoids using explicit special characters.
     # (That works in bash, too.)
     # Added `local' variable definitions for tidiness.
     local TAB=$'\t';
     (( $# < 1 )) && {
         echo "Usage: selhist [command]"
         return 1
     };

     local -a cmd
     # Use zsh's hacky but useful split-into-lines syntax.
     # The (f) means `split input lines on newlines.'
     # This means we can avoid messing with IFS.  (That should work, too.)
     # Note the padding elements are unnecessary.
     cmd=(${(f)"$(grep -w $1 $HISTFILE | sort | uniq | pr -tn)"})
     # The following version is necessary if you are using zsh's
     # extended_history option, which puts extra information at
     # the start of history lines.  (It's harmless in other cases
     # unless you are in the habit of re-executing colon commands.)
     # cmd=(${(f)"$(sed -e 's/^:[^;]*;//' $HISTFILE | grep -w $1 |
     # sort | uniq | pr -tn)"})

     # Slightly simplified output possible in zsh, which won't
     # split variables on spaces unless sh_word_split is set.
     # (It would be simpler to use the pr -tn at this point, then
     # it doesn't have to be stripped off later.)
     print -l $cmd | less -F

     # Note the renumbering here.
     echo -n "enter number of desired command [1 - $(( ${#cmd[@]} - 1 ))]: "
     local answer
     read answer

     # The eval works, but the following is a little more flexible:
     # it loads the line into the line editor, so you can edit
     # further, or just hit return.  (It's a little like using the
     # hist_verify option with !-style history.)
     print -z "${cmd[$answer]#*$TAB}"
     # Original version.
     # eval "${cmd[$answer]#*$TAB}"
}


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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