Don't understand german? Read or subscribe to my english-only feed.

zsh: previous command output

An absolutely great zsh-tip by Jesper Holmberg I would like to share with you because I find it very useful.

The motivation for the following snippet is the fact that I often do a ‘find’ or a ‘locate’ to find some files I’m interested in, and then want to do some action on one of the files I just found. This function provides a way to put completions from the output of the previous command on the command line.

What this does is that it repeats the previous command, saving the output in a string. It then splits the output string on new-lines, and quotes each element (so that for example file names containing spaces get properly quoted). I then bind the function to a menu-complete-like widget, so that I can step through the alternatives.

Test it together with ‘loocate’ or ‘find’. Perhaps someone finds this
useful, and any suggestions for improvement would be appreciated.

  — Jesper Holmberg on zsh-users mailinglist

Put this in your zsh-setup an press esc -e if you want to use this function:

_jh-prev-result () {
    hstring=$(eval `fc -l -n -1`)
    set -A hlist ${(@s/
/)hstring}
    compadd - ${hlist}
}
zle -C jh-prev-comp menu-complete _jh-prev-result
bindkey 'ee' jh-prev-comp

Comments are closed.