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

# Tip of the day: previous command output

• X-seq: zsh-users 7884
• From: Jesper Holmberg <jeho5791@xxxxxxxxxxxxx>
• To: Zsh-users List <zsh-users@xxxxxxxxxx>
• Subject: Tip of the day: previous command output
• Date: Thu, 19 Aug 2004 10:58:12 +0200
• Mail-followup-to: Zsh-users List <zsh-users@xxxxxxxxxx>
• Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
• Sender: Jesper Holmberg <jesper.holmberg.5791@xxxxxxxxxxxxx>

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