Excellent tutorial!
I could have done with a tutorial like that back in August - when I was trying to work out how to add bash completion to my trusty old terminal-based note-taking script I've been developing over the last few years.
Fortunately I managed to work out how to get my bash-completion working fairly quickly back then. So I don't need the tutorial now.... Heh heh. But congratulations anyway on a job well-done!
My note-taking application (imaginatively called note) stores all notes in the users home directory at:
$HOME/.note/notes/
So I wanted to be able to tab-complete the names of notes in the .note/notes directory. So my completion script needed to list any notes that have names that match whatever has been typed so far.
So this is what I came up with for my completion file (also called note) which went into /etc/bash_completion.d/:
Code:
__notes(){
tmp=( $(compgen -W "$( ls -F $HOME/.note/notes/ 2>/dev/null )" -- "${COMP_WORDS[$COMP_CWORD]}" ))
COMPREPLY=( "${tmp[@]// /\ }" )
}
complete -F __notes note
And it works like a charm!
I've been meaning to expand the tab-completion to complete some of the program options/switches too, but I haven't had enough motivation to add that feature yet! XD