knqyf263 / pet

Simple command-line snippet manager
MIT License
4.43k stars 222 forks source link

[Feature Request] Enter first match with shortcut key #164

Open JakeElder opened 3 years ago

JakeElder commented 3 years ago

We can use the pet-select function to use the current line as a search string, IE

$ cat .zshrc
function pet-select() {
  BUFFER=$(pet search --query "$LBUFFER")
  CURSOR=$#BUFFER
  zle redisplay
}
zle -N pet-select
stty -ixon
bindkey '^s' pet-select

It would be useful to be able to select the first match automatically, bypassing the fuzzy finder. Something like

pet exec --first-match checkexp

This would invoke exec for the first match for the provided string, (IE for if you have a snippet with the description of "Check SSL Certificate Expiry Date"

Then we could make a function like this;

function pet-exec-first() {
  BUFFER=$(pet exec --first-match "$LBUFFER")
  CURSOR=$#BUFFER
  zle redisplay
}
zle -N pet-exec-first
stty -ixon
bindkey '^e' pet-exec-first

which would enable entering

$ checkexp

then pressing <ctrl-e> to have it expanded to the Check SSL Certificate command

Combine this with https://github.com/knqyf263/pet/issues/163 to have a much lighter workflow, IE, I'd like to be able to enter

$ commitfeat

then press <ctrl-e> to expand to

git commit -am "feat(ui): <cursor is here>"

and be able to type my commit message, without going through the interactive fuzzy find step and entering parameters via the pet form, while still retaining that great functionality when needed for more obscure snippets that have more than one parameter

RamiAwar commented 6 months ago

This is very interesting @JakeElder , such a good idea!

Wonder if we can make it work for multiple parameters too..

I've seen a lot of people want to bypass the pet parameter filler for speed purposes as well as shell autocomplete. Maybe that's the problem and we can make the pet parameter filler faster and supporting autocomplete somehow? Would be hard though.

I think it's nice to be able to support your functionality optionally though.