Skip to content

Instantly share code, notes, and snippets.

@calvinf
Last active January 31, 2025 01:48
Show Gist options
  • Save calvinf/05685d1bbbd0dc3235936a8259d0fde3 to your computer and use it in GitHub Desktop.
Save calvinf/05685d1bbbd0dc3235936a8259d0fde3 to your computer and use it in GitHub Desktop.
Bun autocomplete for bash
#!/usr/bin/env bash
_bun_complete() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Define the main commands
local commands="add build create exec init install link outdated patch pm publish remove repl run test unlink update upgrade x"
# Define some common flags
local flags="--watch --hot --no-clear-screen --smol -r --preload --inspect --inspect-wait --inspect-brk --if-present --no-install --install -i -e --eval -p --print --prefer-offline --prefer-latest --port --conditions --fetch-preconnect --max-http-header-size --dns-result-order --expose-gc --no-deprecation --throw-deprecation --title --silent --elide-lines -v --version --revision -F --filter -b --bun --shell --env-file --cwd -c --config -h --help"
local scripts=$(bun getcompletes s)
case "${prev}" in
bun)
COMPREPLY=( $(compgen -W "${commands} ${scripts}" -- ${cur}) )
return 0
;;
run)
COMPREPLY=( $(compgen -W "${scripts}" -- ${cur}) )
return 0
;;
*)
# For other commands, suggest flags or nothing
COMPREPLY=( $(compgen -W "${flags}" -- ${cur}) )
return 0
;;
esac
}
complete -F _bun_complete bun
@mangs
Copy link

mangs commented Jan 30, 2025

Nice! FYI you can get the current list of script entries from package.json by running bun getcompletes s

@mangs
Copy link

mangs commented Jan 30, 2025

@calvinf
Copy link
Author

calvinf commented Jan 31, 2025

Thanks, @mangs!

@mangs
Copy link

mangs commented Jan 31, 2025

You're very welcome. I've been down this rabbit hole before. 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment