How to add tab autocompletion for a custom awsctx command in Zsh (like kubectx)
If you frequently switch between multiple AWS profiles, you’ve likely wished for a quicker, more intuitive way to manage them from the command line. Inspired by tools like kubectx for Kubernetes, this guide walks you through setting up a custom awsctx command with tab autocompletion using Zsh and Bash completion utilities.
By the end of this guide, pressing <Tab> after typing awsctx will autocomplete AWS profile names—dramatically improving speed and accuracy when working with the AWS CLI.
Step 1: Create the awsctx Function
In your ~/.zshrc, define the awsctx function. This will let you switch AWS profiles using either an argument or an interactive FZF-based selector:
function awsctx() { if [ -z "$1" ]; then profile=$(aws configure list-profiles | fzf) if [[ $profile != "" ]]; then export AWS_PROFILE=$profile fi else export AWS_PROFILE=$1 fi echo -e "\033[32m$AWS_PROFILE\033[0m selected" }- If no profile name is given, it launches fzf for fuzzy selection.
- Otherwise, it sets AWS_PROFILE directly.
- The selected profile is displayed in green for clarity.
Step 2: Enable Bash Completion in Zsh
To use traditional Bash completion utilities inside Zsh, you need to enable them:
autoload -Uz compinit && compinit autoload -Uz bashcompinit && bashcompinitPlace these in your ~/.zshrc before sourcing the completion script.
Step 3: Write the Autocompletion Script
Create a file named ~/.zsh/completions/_awsctx_completion with the following contents:
#/usr/bin/env bash _awsctx_completion() { if [ "${#COMP_WORDS[@]}" != "2" ]; then return fi local profiles=($(compgen -W "$(aws configure list-profiles)" "${COMP_WORDS[1]}")) if [ "${#profiles[@]}" == "1" ]; then local profile=$(echo ${profiles[0]/%\ */}) COMPREPLY=("$profile") else COMPREPLY=("${profiles[@]}") fi } complete -F _awsctx_completion awsctxThen make it executable:
chmod +x ~/.zsh/completions/_awsctx_completionThis script leverages compgen, a Bash builtin used to generate possible completion matches. It filters AWS profile names using the current partial input.
Step 4: Source the Script in .zshrc
Still in your ~/.zshrc, source the autocompletion script so it’s loaded into your shell session:
source $HOME/.zsh/completions/_awsctx_completionYour final .zshrc additions should look like this:
function awsctx() { if [ -z "$1" ]; then profile=$(aws configure list-profiles | fzf) if [[ $profile != "" ]]; then export AWS_PROFILE=$profile fi else export AWS_PROFILE=$1 fi echo -e "\033[32m$AWS_PROFILE\033[0m selected" } autoload -Uz compinit && compinit autoload -Uz bashcompinit && bashcompinit source $HOME/.zsh/completions/_awsctx_completionTest It Out
Restart your terminal or reload your .zshrc:
source ~/.zshrcThen type:
awsctx <Tab>You should see all available AWS profiles populated dynamically!
How It Works
- compgen dynamically generates matching profile names using your current input.
- COMP_WORDS contains the current command-line arguments; this is used to determine context.
- COMPREPLY returns the list of completion options to Zsh.
- complete -F registers your function for the awsctx command.
Although written in Bash, this works in Zsh via bashcompinit, which bridges Bash-style completion scripts into Zsh environments. For more background, check out resources like:
- compgen and tab completion in Bash/Zsh
- How to use Bash completion in Zsh
- Understanding COMP_WORDS in completion scripts
Bonus Tips
- Use a plugin manager like zinit, antidote, or oh-my-zsh to manage your custom functions and completions more cleanly.
- For cross-shell compatibility (Bash, Zsh, etc.), abstract completion into reusable scripts.
- Combine with tools like direnv to auto-load profiles per project directory.
With this setup, switching AWS profiles becomes just as easy as working with Kubernetes contexts using kubectx. Give it a try—and never mistype an AWS profile again.
.png)


