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

incr. search: skip duplicates on the same history event



Hey list,

Someone on IRC came up with the following question:

Say, you got a few history events that got the string 'make' in them:

  % make depend
  % make clean ; make all ; sudo make install

After a few more commands, you do a
'history-incremental-search-backward' for 'make' and get back the last
event with the three make calls on one line. Now you can reinvoke
'history-incremental-search-backward' again to jump back to earlier
occurrences of 'make' in the history:

  % make clean ; make all ; sudo make install
    ^            ^               ^
    |            |               |
    |            |               \-- ^Rmake
    |            |
    |            \-- 1st time you hit ^R after making
    |                your initial search.
    |
    \-- after hitting ^R for the 2nd time you end up here.

Now, the idea is to after searching backwards for the first time
(^Rmake), hitting ^R again, to go further backwards should ignore any
other occurrences of 'make' in the current history event and jump
directly to the next history event containing 'make' ('make depend')
in the example above.

I have been playing around to get this working and had a few rather
complex solutions, that all failed somehow. :-)

So, I took another approach and the closest I got (which works) is the
following. It exists the incremental search and re-enters it if you
continue typing. That is not an exceptionally nice solution and it
would require additional tweaking to become usable.

[snip]
self-insert () {
  if [[ $LASTWIDGET == incsearch ]] ; then
    zle history-incremental-search-backward $LASTSEARCH
  fi
  zle .self-insert
}
zle -N self-insert
incsearch () {
  if [[ $LASTWIDGET == incsearch ]] ; then
    zle history-search-backward
  else
    zle history-incremental-search-backward
  fi
}
zle -N incsearch
bindkey '^r' incsearch
[snap]

However, I hope that someone has got a better idea to solve this.

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925



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