そしてこのプラグイン適切なパッケージ候補を使用してファジー検索を実行できます。
実行結果は次のとおりです。
# apt zzuf zziplib-bin zytrax
リンクのコード(私は if [[ "${BASH_<...> fi
関数に入れましたmy-fuzzy-test
):
#!/usr/bin/env bash
function insert_stdin {
# if this wouldn't be an external script
# we could use 'print -z' in zsh to edit the line buffer
stty -echo
perl -e 'ioctl(STDIN, 0x5412, $_) for split "", join " ", @ARGV;' \
"$@"
}
function my-fuzzy-test() {
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
packages="$(apt list --verbose 2>/dev/null | \
# remove "Listing..."
tail --lines +2 | \
# place the description on the same line
# separate the description and the other information
# with a ^
sd $'\n {2}([^ ])' $'^$1' | \
# place the package information and the package description
# in a table with two columns
column -t -s^ | \
# select packages with fzf
fzf --multi | \
# remove everything except the package name
cut --delimiter '/' --fields 1 | \
# escape selected packages (to avoid unwanted code execution)
# and remove line breaks
xargs --max-args 1 --no-run-if-empty printf "%q ")"
if [[ -n "${packages}" ]]; then
insert_stdin "# apt ${@}" "${packages}"
fi
fi
}
上記のコードを入れてキーバインディングに~/.zshrc
マッピングしました。my-fuzzy-test
zle -N my-fuzzy-test
bindkey "^[k" my-fuzzy-test
プレスalt-k
トリガーmy-fuzzy-test
機能のため、 を押しても何も表示されませんalt-k
。最後に行if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
合計を削除すると、fi
関数が実行される可能性がありますが、上記の期待される出力は表示されず、エラーが表示されます。stty: 'standard input': Inappropriate ioctl for device
次のように、端末プロンプトに候補者を入力できることがわかります。このコード次のように:
# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\s+(.*)/, $1)}++' |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
local ret=$?
if [ -n "$selected" ]; then
num=$selected[1]
if [ -n "$num" ]; then
zle vi-fetch-history -n $num
fi
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
それでは、端末プロンプトに候補を正しく入力するにはどうすればよいですかmy-fuzzy-test
?
ベストアンサー1
Ctrl-Rウィジェットはzle vi-fetch-history
過去の一致を挿入するために使用され、ここで実行する操作には機能しません。
あなたがすべきことは、生成された一致を編集バッファに挿入することです。ここで同じコードで他のウィジェットがやっているように:https://github.com/junegunn/fzf/blob/master/shell/key-bounds.zsh#L62
echo
このウィジェットは、関数によって挿入された一致を__fsel
バッファの左側の右側に挿入します(つまり、一致をカーソルの現在の位置に挿入し、カーソルは挿入された内容の右側に終わります)。
LBUFFER="${LBUFFER}$(__fsel)"