Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

ZSH has a powerful menu select autocompletion e.g. for choosing a directory, choosing a process with kill (kill[tab][tab]). You can use arrows to navigate around the choices.

Is it possible to have the menu pop-up for the history search (CTRL+R) also? Such a way that the menu would allow the navigate the matches of current CTRL+R search bar?

share|improve this question

2 Answers 2

  1. Not that I know of.

  2. Perhaps you wish to try predict-on, which does something related to what you are asking:

    autoload -U predict-on
    zle -N predict-on
    zle -N predict-off
    bindkey '^X1' predict-on
    bindkey '^X2' predict-off
    
share|improve this answer
    
Interesting function -- never heard of that one before. –  mpy Mar 11 '13 at 13:47

That is possible; in principle I modified the _kill completion to work also for history events.

Here's an example; after selecting a entry you hit [ENTER] once to accept the event number (the command line reads then f. e. hc 780) and hit [ENTER] again to execute the command. (Use fc instead of hc if you want to modify the command line prior to execution).

$ hc Makefile[TAB]
history event
 1906  9.3.2013 18:10  vi Makefile
 1867  9.3.2013 17:26  less Makefile
  780  3.2.2013 17:54  cat Makefile                
  776  3.2.2013 17:51  cat ../Makefile     

To use it, put the appended files to a directory in your $fpath and add these lines into your ~/.zshrc:

zstyle ':completion:*:historyevent' command 'fc -dE -lr 1'
zstyle ':completion:*:historyevent' sort no

Finally, start a new zsh session.

Beware, that's a quick&dirty solution, but it worked well for some time now.


_historyevent

#autoload

local out histevent list expl match desc listargs all nm ret=1

_tags historyevent || return 1

all=(-U)
match="*[[:blank:]]*[[/[:blank:]]$PREFIX*$SUFFIX*"
nm="$compstate[nmatches]"

out=( "${(@f)$(_call_program historyevent fc -l 2>/dev/null)}" )
desc="$out[1]"
out=( "${(@M)out[2,-1]:#${~match}}" )

histevent=( "${(@)${(@M)out##[^0-9]#[0-9]#}##*[[:blank:]]}" )

if zstyle -T ":completion:${curcontext}:historyevent" verbose; then
  list=( "${(@Mr:COLUMNS-1:)out}" )
  desc=(-ld list)
else
  desc=()
fi

_wanted processes expl 'history event' \
    compadd "$@" "$desc[@]" "$all[@]" -a - histevent && ret=0

if [[ -n "$all" ]]; then
  zstyle -s ":completion:${curcontext}:historyevent" insert-ids out || out=menu

  case "$out" in
  menu)   compstate[insert]=menu ;;
  single) [[ $compstate[nmatches] -ne nm+1 ]] && compstate[insert]= ;;
  *)      [[ ${#:-$PREFIX$SUFFIX} -gt ${#compstate[unambiguous]} ]] &&
              compstate[insert]=menu ;;
  esac
fi

return ret

_fc

#compdef fc

local curcontext="$curcontext" line state ret=1

_arguments -C \
   '-e[editor command]' \
   '-l[list last history events]' \
   '-n[suppress numbers]' \
   '-r[reverse order]' \
   '*:history event:->historyevent' && ret=0

if [[ -n "$state" ]]; then
  _alternative \
    'historyevent:: _historyevent' && ret=0
fi

return ret
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.