From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17133 invoked from network); 8 Jul 2004 07:41:33 -0000 Received: from odin.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.85) by ns1.primenet.com.au with SMTP; 8 Jul 2004 07:41:33 -0000 Received: (qmail 5245 invoked from network); 8 Jul 2004 07:41:57 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 8 Jul 2004 07:41:57 -0000 Received: (qmail 17420 invoked by alias); 8 Jul 2004 07:40:56 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7669 Received: (qmail 17410 invoked from network); 8 Jul 2004 07:40:55 -0000 Received: from odin.dotsrc.org (HELO a.mx.sunsite.dk) (qmailr@130.225.247.85) by sunsite.dk with SMTP; 8 Jul 2004 07:40:55 -0000 Received: (qmail 4196 invoked from network); 8 Jul 2004 07:41:26 -0000 Received: from unknown (HELO moonbase.zanshin.com) (@167.160.213.139) by a.mx.sunsite.dk with SMTP; 8 Jul 2004 07:41:13 -0000 Received: from toltec.zanshin.com (toltec.zanshin.com [64.84.47.166]) by moonbase.zanshin.com (8.12.11/8.12.11) with ESMTP id i687eev2002498 for ; Thu, 8 Jul 2004 00:40:40 -0700 Date: Thu, 8 Jul 2004 00:40:40 -0700 (PDT) From: Bart Schaefer Reply-To: zsh-users@sunsite.dk To: zsh-users@sunsite.dk Subject: Re: doing regex on history In-Reply-To: <1089253050.30333.5.camel@tp> Message-ID: References: <1089253050.30333.5.camel@tp> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, hits=0.0 required=6.0 tests=none autolearn=no version=2.63 X-Spam-Hits: 0.0 On Wed, 8 Jul 2004, damon wrote: > I am after help to enable/use? regex on the history (using vi) By "using vi" I assume you mean that you're using ZLE in vi emulation mode, and that the vi-history-search-{forward,backward} widgets are therefore of interest. There's no direct support for regular expressions in history search, in vi emulation or otherwise. ZLE's vi emulation can be though of as operating as if ":se nomagic" is always in effect. If you have e.g. the PCRE library available and have built the zsh/pcre module, you can write your own widgets that do the searching. This gets pretty tricky as there is no way to override the searching part of the builtin vi-history-* widgets without also overriding the read-the-pattern part, but fortunately there's a read-from-minibuffer function supplied in the zsh distribution. So you might do: autoload -U read-from-minibuffer vi-history-re-search-backward() { zmodload -i zsh/pcre || return 1 local REPLY event read-from-minibuffer "?" [[ -z $REPLY ]] && return 0 history -n -r 0 | while read event; do if [[ $event -pcre-match $REPLY ]]; then BUFFER=$event return 0 fi done return 1 } If you don't have pcre, you can settle for glob patterns, which (if you setopt EXTENDED_GLOB) can be regex-equivalent but do not use compatible syntax. In that case it's even easier: vi-history-glob-search-backward() { local event read-from-minibuffer '?' [[ -z $REPLY ]] && return 0 if history -n -r -m \*$REPLY\* 0 | read event; then BUFFER=$event return 0 fi return 1 } Left as excercises are to use the zsh/parameter module's $history hash instead of forking off "history -r 0" (and, can you find a bug in my example that would fixed by using the hash?), and then extending that to remember where you last found a match and to begin again from there the next time. Oh, and for more fun, try doing it with an incremental search, which needs a loop around "read-from-minibuffer -k 1" with a call to "zle -R", plus some special handling for backspace, return, etc.