Good, it works gracefully. your definition of ISEARCHDIR is the same with what I desired. Bart, thank you very much :)! On Tue, Mar 27, 2012 at 9:59 PM, Bart Schaefer wrote: > On Mar 27, 2:58pm, Madsen Zhang wrote: > > > > if the last isearch direction is available, the following script, based > on > > the one from Bart Schaefer, should work in both forward and backward > > isearch: > > > > zle-isearch-exit() { > > if [[ $ISEARCHDIR -eq 1 ]]; then > > local match mbegin mend > > setopt extendedglob > > [[ -n $LASTSEARCH ]] || return 0 > > : ${LBUFFER%(#b)(*)$LASTSEARCH} > > CURSOR=$mend[1] > > fi > > > > return 0 > > } > > zle -N zle-isearch-exit > > You can set ISEARCHDIR for yourself by putting a little wrapper function > around history-incremental-search-*. > > hist-inc-search-save-direction() { > if [[ $WIDGET = *-forward ]]; then > typeset -g ISEARCHDIR=1 > zle .history-incremental-search-forward "$@" > else > typeset -g ISEARCHDIR=-1 > zle .history-incremental-search-backward "$@" > fi > } > zle -N history-incremental-search-forward hist-inc-search-save-direction > zle -N history-incremental-search-backward hist-inc-search-save-direction > > I'm not sure I got the semantics of ISEARCHDIR the way you wanted them, > but you get the idea. >