linuxbashtab-completioncompletionfzf

Bash prompt disappears after selecting a completion from the FZF list


I am trying to program Tab completion in Bash using FZF. The below code tries directory path completion for a custom command mycmd. For the first argument, upon hitting tab, directories found by the find command will be passed to FZF and the directory path that's selected will be assigned to the COMPREPLY array. This works well.

_mycmd_fzf_completions() {
    if (( COMP_CWORD == 1 )); then
        local dir_select && dir_select=$(find -L ${COMP_WORDS[COMP_CWORD]:-.} -maxdepth 1 -mindepth 1 -type d 2> /dev/null | fzf --height 100% --query="${COMP_WORDS[COMP_CWORD]}" )
        case "$?" in
                0) COMPREPLY=( ${dir_select} );;
                *) return 0;;
            esac
        fi
}

# Register the completion function for the custom command `mycmd`
complete -F _mycmd_fzf_completions mycmd

However, now when I change the height of FZF to less than 100%, the TAB completions works but the prompt disappears.

enter image description here

enter image description here

The prompt strangely disappears!

enter image description here

What could be the reason and how to resolve this?


Solution

  • Refactored code:

    _mycmd_fzf_completions() {
        bind '"\e[0n": redraw-current-line'
        if (( COMP_CWORD == 1 )); then
            local dir_select && dir_select=$(find -L ${COMP_WORDS[COMP_CWORD]:-.} -maxdepth 1 -mindepth 1 -type d 2> /dev/null | fzf --height 100% --query="${COMP_WORDS[COMP_CWORD]}" )
            case "$?" in
                    0) COMPREPLY=( ${dir_select} ); printf '\e[5n';;
                    *) printf '\e[5n'; return 0;;
                esac
            fi
    }
    
    # Register the completion function for the custom command `mycmd`
    complete -F _mycmd_fzf_completions mycmd