Stop typing custom command args like a chump

Environment & Shell Customization (Aliases/Functions/.Bashrc)

Stop typing custom command args like a chump

🧩 The Challenge

You’ve got that awesome helper function or alias, right? The one that wraps some ugly command line for a specific service or project. But you still have to type out all its custom arguments every damn time. What a pain.

💡 The Fix

Build your own completion for it. Give your custom command some tab-completion magic so you don’t have to remember all the options or paths. It’s a quick win for your fingers and your sanity.

# Example: a function `myctl` that takes 'start', 'stop', 'restart'
# and also specific config files like 'config-prod', 'config-dev'
_myctl_completions() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Define your specific options here
opts="start stop restart status config-prod config-dev backup restore"

# Only offer these options if we are at the first argument
if [[ ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
}

# Attach the completion function to your command/function
complete -F _myctl_completions myctl

# And here's your actual function this completion applies to
myctl() {
echo "Running myctl with args: $@"
# In a real scenario, you'd parse $1, $2, etc.
}

⚙️ Why It Works

The `complete -F` command points your shell to a custom function (`_myctl_completions` here) whenever you try to complete `myctl`. This function gets special shell variables like `COMP_WORDS` and `COMP_CWORD` which let it figure out what you’re typing, then it populates `COMPREPLY` with the options it should suggest.

🚀 Pro-Tip: Toss this whole block into your `.bashrc` or a dedicated `.bash_completion` file, source it, and thank yourself later.

Linux Tips & Tricks | © ngelinux.com | 10/16/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted