From e35c9b4b67afe73385671dfd3a84b3763054bb6c Mon Sep 17 00:00:00 2001 From: Rasmus Skovgaard Lauritsen Date: Tue, 24 Nov 2026 00:56:41 +0100 Subject: [PATCH] feat(bitwarden): add completions for bw and bws (#14261) * feat(bws): add completion plugin * feat(bitwarden): unify bw and bws completions --- plugins/bitwarden/README.md | 16 +++++++++ plugins/bitwarden/bitwarden.plugin.zsh | 45 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 plugins/bitwarden/README.md create mode 100644 plugins/bitwarden/bitwarden.plugin.zsh diff --git a/plugins/bitwarden/README.md b/plugins/bitwarden/README.md new file mode 100644 index 000000000..63e844eb7 --- /dev/null +++ b/plugins/bitwarden/README.md @@ -0,0 +1,16 @@ +# Bitwarden plugin + +This plugin adds Zsh completions for whichever Bitwarden CLIs are installed: + +- [Bitwarden CLI (`bw`)](https://bitwarden.com/help/cli/) +- [Bitwarden Secrets Manager CLI (`bws`)](https://bitwarden.com/help/secrets-manager-cli/) + +To enable it, add `bitwarden` to the plugins array in your `.zshrc`: + +```zsh +plugins=(... bitwarden) +``` + +Each installed CLI generates and refreshes its own completion cache in the +background when the plugin loads. The cache files are +`$ZSH_CACHE_DIR/completions/_bw` and `$ZSH_CACHE_DIR/completions/_bws`. diff --git a/plugins/bitwarden/bitwarden.plugin.zsh b/plugins/bitwarden/bitwarden.plugin.zsh new file mode 100644 index 000000000..8eddaf09a --- /dev/null +++ b/plugins/bitwarden/bitwarden.plugin.zsh @@ -0,0 +1,45 @@ +# Autocompletion for the Bitwarden CLI (bw) and Secrets Manager CLI (bws). +() { + local cli completion cache_file + local -a generator_args + for cli in bw bws; do + completion="_$cli" + cache_file="$ZSH_CACHE_DIR/completions/$completion" + + if (( ! $+commands[$cli] )); then + # compinit can register a cached completion after the CLI is removed. + if [[ -f "$cache_file" && ${_comps[$cli]-} == "$completion" ]]; then + unset "_comps[$cli]" + fi + continue + fi + + case "$cli" in + bw) + generator_args=(completion --shell zsh) + # bw emits function definitions. Use ksh-style autoload to run _bw + # on the first Tab press as well as loading its helper functions. + autoload -Uk "$completion" + ;; + bws) + generator_args=(completions zsh) + autoload -Uz "$completion" + ;; + esac + # Register the completion if compinit hasn't picked it up yet. + if [[ -z ${_comps[$cli]-} ]]; then + _comps[$cli]=$completion + fi + + () { + zmodload -F zsh/files b:zf_mv || return + local temp_file + temp_file=$(mktemp "${cache_file:h}/.$completion.XXXXXXXX") || return + if command "$cli" "$@" >| "$temp_file" && [[ -s "$temp_file" ]] && + zf_mv -f -- "$temp_file" "$cache_file"; then + return + fi + rm -f -- "$temp_file" + } "${generator_args[@]}" &| + done +}