Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

  1. Note: we recently updated out site software, please report any unseen issues - we do this often to insure your information is secure.
    Dismiss Notice

Creating a bash completion script

Discussion in 'General Linux' started by iridakos, Mar 1, 2018.

  1. iridakos

    iridakos New Member

    Joined:
    Jan 28, 2018
    Messages:
    11
    Likes Received:
    9
    nuna, wizardfromoz and JasKinasis like this.
  2. JasKinasis

    JasKinasis Well-Known Member

    Joined:
    Apr 25, 2017
    Messages:
    244
    Likes Received:
    440
    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
     
    nuna, iridakos and wizardfromoz like this.
  3. iridakos

    iridakos New Member

    Joined:
    Jan 28, 2018
    Messages:
    11
    Likes Received:
    9
    Thank you!

    notes look very interesting, good job! :D
     
  4. nuna

    nuna Member

    Joined:
    Aug 29, 2017
    Messages:
    36
    Likes Received:
    57
    Thanks for sharing, some day could come in handy.
    It's becoming a fact that every programmer needs a cat.
     
    iridakos likes this.
  5. iridakos

    iridakos New Member

    Joined:
    Jan 28, 2018
    Messages:
    11
    Likes Received:
    9
    Thank you!

    Yes, everybody needs a cat! :D
     
  6. JasKinasis

    JasKinasis Well-Known Member

    Joined:
    Apr 25, 2017
    Messages:
    244
    Likes Received:
    440
    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 likes this.
  7. iridakos

    iridakos New Member

    Joined:
    Jan 28, 2018
    Messages:
    11
    Likes Received:
    9
    Cool!
     

Share This Page