Last active
January 25, 2026 18:22
-
-
Save slawekzachcial/347e4187676072fe1e8efb61512767be to your computer and use it in GitHub Desktop.
FZF used to select from multiple possible completions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| alias k=kubectl | |
| complete -o default -o bashdefault -F __kubectl_fzf k | |
| __kubectl_fzf() { | |
| # Required to prevent BASH prompt from dissapearing after selecting a completion | |
| # from the FZF list when using "--height" less that 100%. | |
| # "\e[0n" is an ANSI Device Status Report (DSR) sequence that requests the terminal | |
| # to report readiness. | |
| # Binding \e[0n to redraw-current-line ensures the terminal redraws the current | |
| # command line after FZF closes. | |
| bind '"\e[0n": redraw-current-line' | |
| # Let kubectl generate candidates | |
| __start_kubectl | |
| # Use FZF only if more than one completion | |
| if (( ${#COMPREPLY[@]} > 1 )); then | |
| # Run fzf | |
| local selected | |
| if selected=$(printf "%s\n" "${COMPREPLY[@]}" | \ | |
| fzf --height 40% --layout reverse --border); then | |
| # If selected has multiple words, take the first one | |
| selected="${selected%% *}" | |
| # Replace only the current word | |
| COMPREPLY=("$selected") | |
| fi | |
| fi | |
| # Required to prevent BASH prompt from dissapearing after selecting a completion | |
| # from the FZF list when using "--height" less that 100%. | |
| # "printf '\e[5n'" sends the DSR response (\e[5n) to signal the terminal is ready, | |
| # triggering the redraw. | |
| printf '\e[5n' | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment