Nice fzf package search; write actual command in prompt ? ZLE?

From the reddit thread about pacseek (@moson), I got a cool fzf command to search for packages (source).

I modified it slightly

  • AUR activated by default
  • minor aesthetics (no --black background)
  • addition of installed packages browsing with CTRL+I
  • to avoid straining Arch servers unreasonably, I found that a simple sleep in the review command works nicely – I didn’t analyse my traffic, but I tested that by writing to a local file before the query; only the packages I lingered on were logged, so I assume fzf terminates the preview command when switching entry. Good to know.
# fzf aided package installer;
# originally from https://github.com/IBArbitrary/dotfiles/blob/master/.config/fish/config.fish#L72-L78
function pacs {
    yay -Slq | fzf --prompt 'yay> ' \
        --header 'Install packages. CTRL+(Pacman/Aur/Installed)' \
        --bind 'ctrl-p:change-prompt(pacman> )+reload(pacman -Slq)' \
        --bind 'ctrl-a:change-prompt(yay> )+reload(yay -Slq)' \
        --bind 'ctrl-i:change-prompt(inst> )+reload(yay -Qq)' \
        --multi --height=80% --preview 'sleep 2; yay -Si {1}' \
        --preview-window bottom | xargs -ro yay -S
}

There is one other thing I’d like to do: instead of just passing the packages directly to yay -S through xarg, I’d like to prewrite “yay -S [packages]” in the prompt, for me to read and manually alter if needed, and so that it shows up in the command logs as though I had manually typed it from the get go, instead of just “pacs” showing up.

I use ZSH, and I assume ZLE is what I should look into, but it’s not immediately clear to me how to operate it.

zle -U <string>

seems to be what I’m looking for, but requires a “widget”, whatever that is.

I could R more of TFM, but I’m feeling a bit lazy, so if anybody knows ZLE like the back of their hand (or knows it’s not the right/simplest tool for the job) I’ll be glad for tips and advice.

1 Like
fh () {
        print -z $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf -e +s --tac | sed 's/ *[0-9]* *//' | sed 's/ *[*]* *//')
}

Does what you want for history.

fh () {
        print -z yay -S $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf -e +s --tac | sed 's/ *[0-9]* *//' | sed 's/ *[*]* *//')
}

Adds yay -S so adapt to your function. Probably only works for zsh.

1 Like

So what I was looking for was simply print -z…

This new version appears to work just the way I want. Thanks!

function pacs {
    cmd=$(yay -Slq | fzf --prompt 'yay> ' \
        --header 'Install packages. CTRL+(Pacman/Aur/Installed)' \
        --bind 'ctrl-p:change-prompt(pacman> )+reload(pacman -Slq)' \
        --bind 'ctrl-a:change-prompt(yay> )+reload(yay -Slq)' \
        --bind 'ctrl-i:change-prompt(inst> )+reload(yay -Qq)' \
        --multi --height=80% --preview 'sleep 2; yay -Si {1}' \
        --preview-window bottom) #| xargs -ro yay -S
    cmd=${cmd//$'\n'/ } # newline -> space
    if ! [ -z "$cmd" ];
    then print -z yay -S $cmd ;
    fi
}

edit: add test to avoid writing empty yay -S if no package selected

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.