• Rob's side project: I recently started Gun-Forums.com and we currently have a $100 raffle - if you're into guns, join up and enter the raffle to win $100.00. Raffle Link

Creating a bash completion script



JasKinasis

Well-Known Member
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
 

iridakos

New Member
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
Thank you!

notes look very interesting, good job! :D
 

JasKinasis

Well-Known Member
Thank you!

notes look very interesting, good job! :D
Thanks!
If I ever publicly release note, I'll post an announcement here on Linux.org.
It's a personal tool I've been developing and using at home and at work since 2013. It's currently sat in a private git repo on notabug.org. A while back I added GPL3 licence text to the source files, in anticipation of a public release, but there are still a few things I want to tidy up before I do.
I just haven't got around to it yet! :/
 

iridakos

New Member
Thanks!
If I ever publicly release note, I'll post an announcement here on Linux.org.
It's a personal tool I've been developing and using at home and at work since 2013. It's currently sat in a private git repo on notabug.org. A while back I added GPL3 licence text to the source files, in anticipation of a public release, but there are still a few things I want to tidy up before I do.
I just haven't got around to it yet! :/
Cool!
 


Staff online

Members online


Top