Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash completion for all files in all subdirectories of a directory
function _subfiles() {
FILES="$(find ${_COMP_DIR}/* -type f|awk -F ${_COMP_DIR}/ '{print $2}')"
COMPREPLY=( $(compgen -W "${FILES}" -- ${CUR}) )
return 0

This comment has been minimized.

Copy link
Owner Author

sinewalker commented May 10, 2019

this TAB-completion bash function searches for all files that are children of $_COMP_DIR.

This is not generic because $_COMP_DIR will be searched base on it's current value, not the one when the completion function was defined or specifed to bash with complete -F _subfiles somefunction. So it will search whichever directory the current value of $_COMP_DIR is at the time the function is called (i.e. when TAB is pressed to complete for whichever function it's bound to).

So, you will need to copy this function for different specific completions (say, your key files):

export KEY_DIR=~/key
function _keyfiles() {
    local CUR KEYS
    KEYS="$(find ${KEY_DIR}/* -type f|awk -F ${KEY_DIR}/ '{print $2}')"
    COMPREPLY=( $(compgen -W "${KEYS}" -- ${CUR}) )
    return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.