Ben
Ben

Reputation: 71535

Use pattern history search, but gracefully degrade to non-pattern history search in older zsh versions

I'm keeping some of my dotfiles in version control, so I can get improvements I make to my environment on all of the numerous servers we have at work. Naturally, this includes my .zshrc.

I've just found out about binding ctrl-R to histroy-incremental-pattern-search-backward to get pattern searches through history, and it's been incredibly useful, so I stuck it in my .zshrc and have been gleefully deploying it anywhere.

The trouble is that some of our servers have zsh version 4.2.6 installed, which doesn't have this feature; on those servers ctrl-R is now just a particularly efficient way to print No such widget `history-incremental-pattern-search-backward'.

So I need something in my .zshrc that give me pattern history searching where available, and leaves history searching alone where it's not; either only conditionally rebinding the key or binding it to something that works either way.

I can use is-at-least to test the zsh version, if I can identify the earliest version that supported history-incremental-pattern-search-backward, but if it's possible I'd rather test more directly (similar to the logic behind feature detection rather than browser/version detection in Javascript).

I've figured out that zle -l gives me a list of widgets that are defined, but apparently only user defined widgets. I haven't been able to find any other way of testing whether a widget is defined.

Any suggestions (or definitive knowledge that the only way to do it is with version number testing) would be appreciated.

Upvotes: 1

Views: 254

Answers (1)

Adam Spiers
Adam Spiers

Reputation: 17916

You were very close to the right answer, but you forgot to RTFM ;-) The section of the manual describing zle says:

-l [ -L | -a ]
      List all existing user-defined widgets.  If the -L option is
      used, list in the form of zle commands to create the widgets.

      When combined with the -a option, all widget names are listed,
      including the builtin ones. In this case the -L option is
      ignored.

and sure enough:

$ zle -al | grep history-incremental-pattern-search-backward 
.history-incremental-pattern-search-backward
history-incremental-pattern-search-backward

so the test you are looking for is:

zle -al | grep -q history-incremental-pattern-search-backward

Upvotes: 2

Related Questions