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
@calvinf
Copy link
Author

calvinf commented Jan 30, 2025

Made this pretty quickly with feeding the output of bun --help to an LLM and asking it to generate a Bash autocomplete script and follow-up to adjust the run output to limit it just to scripts.

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>

@calvinf
Copy link
Author

calvinf commented Jan 30, 2025

In case it's not obvious, you source this file from your main Bash profile.

@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