4

I am trying to implement a custom version of zsh history search when pressing ctrl-r (although my function will map to different shortcut).

So far I have tried to use read , vared and read-command to read input after user presses the bespoke shortcut. My code looks like:

# Bind \eg to `git status`
function _cust-hist {
    zle -I
    local line
    read -r line
    echo $line
    zle accept-line
}
zle -N _cust-hist
bindkey '\eg' _cust-hist

But nothing seems to work. I must be missing something obvious, is this possible with zsh?

1 Answers1

0

Yes, that's definitely possible.

This should get you started:

function _cust-hist {
  local REPLY
  autoload -Uz read-from-minibuffer

Create a sub-prompt, pre-populated with the current contents of the command line.

read-from-minibuffer 'History search: ' $LBUFFER $RBUFFER

Use the modified input to search history & update the command line with it.

LBUFFER=$(echo "$(fc -ln $REPLY $REPLY)" ) RBUFFER=''

Put some informational text below the command line.

zle -M "History result for '$REPLY'." } zle -N _cust-hist bindkey '\eg' _cust-hist

If you're interested in how read-from-minibuffer works, you can find its source code over here. As you can see, it uses read -k or zle recursive-edit to get input from the user.