zsh-users
 help / color / mirror / code / Atom feed
* doing regex on history
@ 2004-07-08  2:17 damon
  2004-07-08  7:40 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: damon @ 2004-07-08  2:17 UTC (permalink / raw)
  To: zsh-users

G'day,

I am after help to enable/use? regex on the history (using vi) so i can
go, eg to find "ifconfig eth0 10.10.1.11" i can match it with:

"ifconfig.*11"

?

cheers,
Damon



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: doing regex on history
  2004-07-08  2:17 doing regex on history damon
@ 2004-07-08  7:40 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2004-07-08  7:40 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-07-08  7:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-08  2:17 doing regex on history damon
2004-07-08  7:40 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).