Custom autocomplete in BASH/ZSH functions


New Member
Feb 19, 2022
Reaction score
I have written a script, and I need the tab autocomplete in my shell to only select files of a certain type (mp4, mkv, avi), just like rmdir's autocomplete only selects directories. Here's my (inelegant) script:


[ -z "$1" ] && return
command mpv "$1" || exit
fd -q donefile && exit
fd -q done && done_exists="y" || done_exists="n"
[ "$done_exists" = "n" ] && choose_no_done="$(echo -e 'Create done marker\nCreate done dir\nDo nothing' | fzf --prompt='No done dir exists, what to do? ')"
[ "$choose_no_done" = "Do nothing" ] && echo ":: Doing nothing." && exit
[ "$choose_no_done" = "Create done marker" ] && echo "$1 is done on $(date +"%Y-%m-%d %H:%M")." > ./"donefile-$(date +"%Y-%m-%d-%H-%M")" && echo ":: Done marker created." && exit
[ "$choose_no_done" = "Create done dir" ] && mkdir -v "done"
fd -q done && done_exists="y" || done_exists="n"
[ "$done_exists" = "y" ] && choose_done="$(echo -e 'yes\nno' | fzf --prompt='Move to done? ')"
ext_alone="$(echo $1 | awk -F '.' '{print $NF}')"
allrelatedfiles="$(echo $1 | sed "s/\.$ext_alone//")"
[ "$choose_done" = "yes" ] && mv -v "$allrelatedfiles"* done/

Thank you for your time.
Last edited:

You'll need to write a script to go into /etc/bash_completion.d/
you can give the script any name, but it's better to give it a name that identifies it as the auto-completion routine for your script.

e.g. if your script is called myscript - then write a script in /etc/bash_completion.d/ called something like myscript-completion.
Off the top of my head, for the file-extensions you've specified, you'll need to do something like this:
complete -f -o plusdirs -X '!*.@(avi|mp4|mkv)' myscript
NOTE: Where myscript is the name of your script.
That should autocomplete directory names AND any avi, mp4, or mkv files in the currently typed path.
At least I think it will. Again, I just wrote that completely off the top of my head, it should be more or less correct though. I'll leave you to test it!

And as for zsh - I have no idea. I haven't used zsh in a long time!
On a tangent—why not? I've been on ZSH since day one (around a year ago). Are there any reasons I should ditch ZSH for BASH?
Bash is the default shell on pretty much ALL Linux distros and I'm extremely lazy. That's all there is to it!
zsh has some cool features, but I find it easier to just write all scripts for Bash. You go into any Linux system and Bash is pretty much always guaranteed to be there. Whereas zsh is not at all guaranteed to be there.
I don't want to have to install a different shell every time I try out a new system, or log into a system.
Especially if it's somebody else's system that I'm using. I don't want to have to pester them to install zsh just so I can run some of my scripts. So I just stick rigidly with Bash.

It could be argued that if I stuck rigidly to the POSIX standards for Shellscript - then any shell-script should work on pretty much any compatible shell. But I've invested so much time in learning Bash, I probably use a ton of Bash's shellscript extensions without even realising it! So again, I use just stick with Bash.

There are no compelling reasons here. Literally just pure laziness on my part!
In case anyone cares, Apple switched the default shell from bash to zsh for macOS 10.15 "Catalina" and subsequent versions of macOS. Catalina was released in 2019. Everyone assumed that it was a GPLv3 licensing issue, but Apple never publicly acknowledged it.

(I just did a web search. What I did not know until now is that before the change, Apple was shipping a very old bash version 3.2 from 2007, which was the last version to be licensed under GPLv2.)