shell 如何从fzf中删除列表项?

rn0zuynd  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(134)

我有以下脚本,通过fzf管道git stash list

git stash list\
    --color=always\
    --pretty=format:'%Cred%gd%Creset - %s%C(yellow)%d%Creset %Cgreen(%cr) %C(bold blue)' |\
  fzf\
    --ansi\
    --tiebreak=index\
    --header "<enter> view stash | <alt-enter> apply stash | <alt-d> drop stash | <ctrl-p> toggle preview window | <esc> quit"\
    --preview "echo {} | grep -o 'stash@{[0-9]}' | head -1 | xargs -I % sh -c 'git show --color=always %' | diff-so-fancy"\
    --preview-window "hidden"\
    --bind "ctrl-p:toggle-preview"\
    --bind "alt-d:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash drop %')+abort"\
    --bind "alt-enter:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash apply %')+abort"\
    --bind "enter:execute: (grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash show -p --color=always %' | diff-so-fancy | less -R) << 'FZF-EOF'
  {} FZF-EOF"

我想做的是按Alt+d并将选中的行项目传递给git stash drop并从列表中删除此项目。
我只能通过以下绑定实现部分功能:
--bind "alt-d:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash drop %')+abort"\
虽然这确实将项目传递给git stash drop,但它也会中止(即关闭)列表。
如何在将项目传递给git stash drop后将其从列表中删除,使其不再出现在列表中,但列表保持打开状态?

ktca8awb

ktca8awb1#

执行完成后,可以使用reload()操作刷新输入列表:

--bind "alt-d:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash drop %')+reload(git stash list)

不过,这并不包括git stash list的初始参数。为了在不输入相同命令的情况下保留这些命令,请设置并使用FZF_DEFAULT_COMMAND变量,而不是将命令通过管道传输到fzf

FZF_DEFAULT_COMMAND="
    --color=always
    --pretty=format:'%Cred%gd%Creset - %s%C(yellow)%d%Creset %Cgreen(%cr) %C(bold blue)'" \
  fzf\
    --ansi\
    --tiebreak=index\
    --header "<enter> view stash | <alt-enter> apply stash | <alt-d> drop stash | <ctrl-p> toggle preview window | <esc> quit"\
    --preview "echo {} | grep -o 'stash@{[0-9]}' | head -1 | xargs -I % sh -c 'git show --color=always %' | diff-so-fancy"\
    --preview-window "hidden"\
    --bind "ctrl-p:toggle-preview"\
    --bind "alt-d:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash drop %')+reload($FZF_DEFAULT_COMMAND)"\
    --bind "alt-enter:execute(echo {} | grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash apply %')+abort"\
    --bind "enter:execute: (grep -o 'stash@{[0-9]}' | xargs -I % sh -c 'git stash show -p --color=always %' | diff-so-fancy | less -R) << 'FZF-EOF'
{} FZF-EOF"

相关问题