Skip to content

Instantly share code, notes, and snippets.

@3v1n0
Last active May 10, 2025 13:06
Show Gist options
  • Save 3v1n0/b61711512301c0d17a912c11b475f8d1 to your computer and use it in GitHub Desktop.
Save 3v1n0/b61711512301c0d17a912c11b475f8d1 to your computer and use it in GitHub Desktop.
Use bash completions in Fish Shell
#!/usr/bin/fish
# You can add this to your ~/.config/fish/config.fish
function __fish_complete_bash
set cmd (commandline -cp)
bash -c "source get-bash-completions.sh; get_completions '$cmd'"
end
# Set the tool to use bash completions
complete -xc git -a '(__fish_complete_bash)'
complete -xc gdbus -a '(__fish_complete_bash)'
complete -xc java -a '(__fish_complete_bash)'
# You could also apply this to all the completions, maybe ignoring the fish ones via
for completion in /usr/share/bash-completion/completions/*
complete -xc (basename $completion) -a '(__fish_complete_bash)'
end
#!/bin/bash
# Author: Brian Beffa <[email protected]>
# Original source: https://brbsix.github.io/2015/11/29/accessing-tab-completion-programmatically-in-bash/
# License: LGPLv3 (http://www.gnu.org/licenses/lgpl-3.0.txt)
#
get_completions(){
local completion COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS COMPREPLY=()
# load bash-completion if necessary
declare -F _completion_loader &>/dev/null || {
if [ -f /etc/bash_completion ]; then
source /etc/bash_completion
elif [ -f /usr/share/bash-completion/bash_completion ]; then
source /usr/share/bash-completion/bash_completion
fi
}
COMP_LINE=$*
COMP_POINT=${#COMP_LINE}
eval set -- "$@"
COMP_WORDS=("$@")
# add '' to COMP_WORDS if the last character of the command line is a space
[[ ${COMP_LINE[@]: -1} = ' ' ]] && COMP_WORDS+=('')
# index of the last word
COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 ))
# load completion
_completion_loader "$1"
# detect completion
completion=$(complete -p "$1" 2>/dev/null | awk '{print $(NF-1)}')
# ensure completion was detected
[[ -n $completion ]] || return 1
# execute completion function
"$completion"
# print completions to stdout
for ((i = 0; i < ${#COMPREPLY[@]}; i++)); do
echo "${COMPREPLY[$i]%%*( )}"
done
}
@ehausen
Copy link

ehausen commented Mar 15, 2023

Setting this up and trying with fish I get error:
get-bash-completions.sh: line 33: _completion_loader: command not found

Using macOS 12.2 (21D49)

@symgryph
Copy link

silly quesiton, how do I use it?

@JumpIn-Git
Copy link

JumpIn-Git commented Apr 3, 2025

use carapace

@pchr8
Copy link

pchr8 commented May 10, 2025

@JumpIn-Git Thank you for the carapace suggestion!
https://carapace-sh.github.io/carapace-bin/

I don't really like the documentation, but once set up it worked like a charm for me! Now I can install bash completions and use them as-is in fish!

@JumpIn-Git
Copy link

@JumpIn-Git Thank you for the carapace suggestion! https://carapace-sh.github.io/carapace-bin/

I don't really like the documentation, but once set up it worked like a charm for me! Now I can install bash completions and use them as-is in fish!

your welcome! tools like carapace are so amazing, everyone should know about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment