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

Tip of the day: previous command output



Someone asked for zsh tips a couple of weeks ago. Here's something I use
a lot. I got the basic idea from some user group somewhere, and then I've
tweaked the idea to suit my needs.

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.

_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


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



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