I've been trying to construct a method for bash to auto complete a file argument. so when you type vi not[tab][tab]
it auto completes to notes.md if it is within the history or path or gives a list of files when not. I could not figure out how to do this with bash_autocomplete but was able to create the following function that works by executing x vi not[enter]
. Anyone know how to translate this into auto complete:
function x () {
# last arg is file to find
FILEARG=${@: -1}
read -a FILES <<< $(timeout -k 1s 1s \
find . -maxdepth 4 -name "*${FILEARG}*" -type f \
-printf "%A@ %p\n" | sort -nr | awk '{print $2}')
#A@ access time epoc timestamp
#C@ status change time
# use this to only take latest:
#FILES="${FILES[0]}"
if [ ${#FILES[@]} -gt 1 ]; then
select FILE in ${FILES[@]}; do
break
done
else
FILE="${FILES[0]}"
fi
echo $FILE
eval ${@:1:$#-1} $FILE
}