Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash completion for certain types of files in a special directory

I have a list of unison profiles which exists in ~/.unison/*.prf.

I'd like to have bash completion so that when I type unison or unison-gtk and hit tab it would list the .prf files from that folder without the .prf part.

Perhaps an example would be clearer:

$ ls ~/.unison/*.prf
default.prf dot-mozilla.prf to-desktop.prf

$ cd  ~  # just to show you don't have to be in the ~/.unison folder
$ unison to<tab>
$ unison to-desktop

I foresee needing this for another tool as well, so it would be convenient if there were parts that could be reused.

like image 502
Scott Kirkwood Avatar asked Apr 29 '09 18:04

Scott Kirkwood


People also ask

How do you write a completion in bash?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

Where do bash completion files go?

Put them in the completions subdir of $BASH_COMPLETION_USER_DIR (defaults to $XDG_DATA_HOME/bash-completion or ~/. local/share/bash-completion if $XDG_DATA_HOME is not set) to have them loaded automatically on demand when the respective command is being completed.

Does bash have tab completion?

Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.


1 Answers

If you are running debian/ubuntu or possibly other GNU/Linux distros, there should be examples of this type of completion in your /etc/bash_completion.d/ directory. Here is an example of how you might set up a completion script for unison:

have unison &&
_unison_show()
{
        local cur

        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=($( compgen -W "$(for x in ~/.unison/*.prf; do echo $(basename ${x%.prf}); done)" -- $cur ) )
}
complete -F _unison_show unison
like image 128
vezult Avatar answered Oct 14 '22 10:10

vezult