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

Re: Interactive search on the command line?



On Dec 3, 11:39pm, Daniel Shahaf wrote:
}
} So, you could do something like this (using the (b::) subscript flag as
} well):
} 
}     % () {
}         local haystack="$1"
}         local needle="a"
}         integer idx
}         while (( idx = ${haystack[(ib:idx+1:)${needle}]} ))
}               (( idx <= $#haystack ))
}         do
}           print -r - $haystack[idx,idx+$#needle-1]
}         done
}       } foobar
}     a
}     % 

This example might be a little clearer if we show the index where the
match was found and use a match pattern longer than one character:

% () {
    local haystack=$1 needle=$2
    integer idx
    while idx=${haystack[(ib:idx+1:)$needle]}
	  (( idx <= $#haystack ))
    do
      print -r - $idx $haystack[idx,idx+$#needle-1]
    done
  } foobar 'o??'
2 oob
3 oba
% 

(I also removed all redundant quotes and braces, since the original
would only work in native zsh emulation anyway.)



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