问题描述
自从我开始使用 zsh 的 z
命令已经有一段时间了:它基于频率的功能非常有用!我现在正在寻找类似的自动完成功能,以便在使用 cp
时完成目标:
预期:行为:
$: ls
-> foo.txt bar.txt
$: cp + TAB
-> suggests iteratively foo.txt and bar.txt
$: cp foo.txt rece + TAB
-> suggest /home/user/Documents/recent_directory
有什么办法吗?感谢您的帮助!
解决方法
是的,有:只需安装 zsh-autocomplete
。
(免责声明:我是 zsh-autocomplete
的维护者。)
另一种方法是使用 Dynamic named directories
http://zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Expansion .
假设您有一个函数 ffz-get
,它使用 fzf
来选择最近使用的目录。然后你可以:
function zsh_directory_name_1() {
local type="$1" arg="$2"
if [[ "$type" == n ]] ; then
# Dynamic named directories
# e.g.,`ll ~[dl]`
o="$(ffz-get "$arg")"
typeset -ga reply
reply=("$o")
return 0
elif [[ "$type" == c ]]; then
return
elif [[ "$type" == d ]]; then
return 1
else
echo "$0: Unknown type '$type'"
return 1
fi
return 1
}
typeset -ag zsh_directory_name_functions=()
zsh_directory_name_functions+='zsh_directory_name_1'
您可以在 https://github.com/NightMachinary/.shells/blob/master/scripts/zshlang/auto-load/others/fuzzy/z.zsh 看到我自己的 ffz-get
实现,但您需要对其进行调整才能使用它,而无需使用我的所有点文件。
这整件事允许你像这样做你的例子:
cp foo.txt ~[rece
# typing the closing ']' will open `fzf` so that you select the candidate you want
cp foo.txt ~[rece]/bar.txt
请注意,我自己的 ffz-get
缓存了每个查询的用户选择(在 redis
中),我不确定这是否有必要。