Autocomplete of filename in directory

This question is quite similar to this one, but I can’t understand the solution. I also know this tutorial, but still I am unable to understand what I am doing wrong.

I am trying to autocomplete the list of directories that are placed in /something/:

$ ls /something/

One    Other    Three

in a way that this will happen:

$ hi [TAB]

One Other Three
$ hi O[TAB]

One Other

Only when the first word is hi and not only inside /something/.

This is what I am doing in .profile:

_codeComplete()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen `ls /something/` -- $cur) )

}
complete -F _codeComplete "hi "

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try:

_codeComplete()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(ls /something/)" -- $cur) )
}

complete -F _codeComplete hi

You need to specify the -W option in compgen and also quote the command which produces the wordlist.

More information on how to write completion functions on this blog: Writing your own Bash Completion Function

Method 2

Let me try to improve upon the accepted answer.

The original answer will fail with filenames containing whitespace. After some investigation I realize you can change the input separator to end-of-line:

IFS=$'n'

making the function handle spaces correctly.

_codeComplete()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    IFS=$'n' tmp=( $(compgen -W "$(ls ~/something/ )" -- $cur))
    COMPREPLY=( "${tmp[@]// / }" )
}

complete -F _codeComplete hi

Method 3

Here’s a version that not only handles spaces, but escapes them, too:

_codeComplete()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local AUTO_COMPLETE_DIRS=$(ls /something/)

    IFS=$'n' COMPREPLY1=( $(compgen -W "$AUTO_COMPLETE_DIRS" -- $cur))
    COMPREPLY2=( "${COMPREPLY1[@]// / }" )
    COMPREPLY=($(printf "%qn" "${COMPREPLY2[@]}"))

}
complete -F _codeComplete "hi "

(Based on dogbane‘s and Eduardo Almeida dos Santos‘ answers, but with escaping as provided by antak on SO: https://stackoverflow.com/a/11536437/1536933)

Method 4

_hi() {
    COMPREPLY=(cd /something/ && compgen -A directory -S / -- $2)
}

complete -o nospace -F _hi hi

Method 5

(this could be a comment but I have <50 reputation)

A fix for EM0 answer, his version handle spaces but also escape them.
But when you try to autocomplete a word that does not match any directory the word is changed to ” instead of providing no completion. Here is a simple fix.

_codeComplete(){
    local cur=${COMP_WORDS[COMP_CWORD]}

    IFS=$'n' tmp=( $(compgen -W "$(ls /home/matutu/.templates)" -- $cur))
    COMPREPLY=( "${tmp[@]// / }" )
    # check if completion array is not empty
    if [ ${#tmp[@]} -ne 0 ]; then
        COMPREPLY=( $(printf "%qn" "${tmp[@]}") )
    fi
}
complete -F _codeComplete "hi "


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x