
假設我想瀏覽文件列表並透過終端互動打開它,我一直在使用
locate filename | head -n 1 | xargs xdg-open
或類似的東西。另一種選擇是使用滑鼠點擊顯示的檔案名,然後貼上。
Tmux 讓我可以導航終端機、複製行並將它們合併到命令中。有沒有辦法在 bash 或 zsh 中或直接在終端模擬器中執行此操作?
答案1
首先將選項放入數組中。 bash 語法:
IFS=$'\n' read -r -d '' -a choices < <(locate filename)
Zsh 語法:
choices=("${(@f)$(locate filename)}")
讓用戶選擇其中一項的技術含量較低但不太用戶友好的方法是使用select
構造。
select choice in "${choices[@]}"; do
xdg-open "$choice"
break
done
(Bash 語法;這也適用於 zsh,但可以簡化。)
為了獲得更好的介面,您可以使用對話。
menu_args=()
for c in "${choices[@]}"; do
menu_args+=("$c" "$c")
done
if choice=$(dialog --menu "$title" "$LINES" "$COLUMNS" "$LINES" "${menu_args[@]}"); then
xdg-open "$choice"
fi