如何自动完成特定目录下的文件? [英] How to autocomplete files under specific directory?
本文介绍了如何自动完成特定目录下的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个命令 memo
,如下所示:
I created a command memo
as follows:
memo() {
vi $HOME/memo/$1
}
我想对我的 memo
应用bash-completion来打开 $ HOME/memo
目录中的文件:
I want to apply bash-completion to my memo
to open files that is already in $HOME/memo
directory:
$ memo [TAB] # to show files in $HOME/memo
$ HOME/memo
包含目录,因此仅在 memo
下列出文件是不够的.换句话说,我想将 $ HOME/memo
中 ls
命令中使用的内容应用于 memo
:
$HOME/memo
contains directory, so listing the file under memo
is not sufficient.
In other words, I want to apply what is used in ls
command in $HOME/memo
to memo
:
$ ls [TAB]
foo.md bar/
我尝试了以下操作,但不适用于嵌套目录:
I tried the below but it doesn't work for nested directories:
_memo() {
local cur
local files
_get_comp_words_by_ref -n : cur
files=$(ls $MEMODIR)
COMPREPLY=( $(compgen -W "${files}" -- "${cur}") )
}
complete -F _memo memo
MEMODIR = $ HOME/memo
推荐答案
下面是一个简单的示例:
Here's a simple example:
_memo()
{
local MEMO_DIR=$HOME/memo
local cmd=$1 cur=$2 pre=$3
local arr i file
arr=( $( cd "$MEMO_DIR" && compgen -f -- "$cur" ) )
COMPREPLY=()
for ((i = 0; i < ${#arr[@]}; ++i)); do
file=${arr[i]}
if [[ -d $MEMO_DIR/$file ]]; then
file=$file/
fi
COMPREPLY[i]=$file
done
}
complete -F _memo -o nospace memo
这篇关于如何自动完成特定目录下的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文