Skip to content

Instantly share code, notes, and snippets.

@r10r
Last active August 6, 2025 15:23
Show Gist options
  • Save r10r/cfcb4e48c0d24c691ca0c913c05acc52 to your computer and use it in GitHub Desktop.
Save r10r/cfcb4e48c0d24c691ca0c913c05acc52 to your computer and use it in GitHub Desktop.
BASH completion for ssh-add */etc/bash_completion.d/ssh-add*
# Bash completion for ssh-add
_ssh_add() {
local cur prev words cword
_init_completion || return
local flags=(
-l --list
-L --list-public
-d --delete
-D --delete-all
-x --lock
-X --unlock
-c --confirm
-k --no-confirm
-q --quiet
-t --life
-E --hash
-h --help
)
local time_examples="2h 4h 8h"
local hash_algos="md5 sha1 sha256"
# Provide completions for options expecting arguments
case "$prev" in
-t | --life)
COMPREPLY=($(compgen -W "$time_examples" -- "$cur"))
return 0
;;
-E | --hash)
COMPREPLY=($(compgen -W "$hash_algos" -- "$cur"))
return 0
;;
esac
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "${flags[*]}" -- "$cur"))
return 0
fi
# Complete identity files in ~/.ssh matching _id patterns
local ssh_dir=~/.ssh
if [[ -d "$ssh_dir" ]]; then
local file
local matches=()
for file in "$ssh_dir"/*; do
[[ -f "$file" ]] || continue
local base=$(basename "$file")
if [[ "$base" == id_* || "$base" == *_id || "$base" == *_id_* ]] && [[ "$base" != *.pub ]]; then
[[ "$file" == "$cur"* ]] && matches+=("$file")
fi
done
COMPREPLY=("${matches[@]}")
fi
return 0
}
complete -F _ssh_add ssh-add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment