Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(question) Is there a basic example for a custom completion function? #48

Open
slayer152 opened this issue Jul 26, 2022 · 1 comment
Open

Comments

@slayer152
Copy link

Hello,

I've been using https://github.com/lincheney/fzf-tab-completion/ but want to try fzf-obc as it supports completion post functions.

Is there a basic example to make a custom completion function work with fzf-obc?

Bash version: GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

_foo() {
  COMPREPLY=()
  local cur
  cur=$(_get_cword)
  local completions="10foo - first foo
11foo - second foo
12foo - third foo"
  local OLDIFS="$IFS"
  local IFS=$'\n'
  COMPREPLY=( $( compgen -W "$completions" -- "$cur" ) )
  IFS="$OLDIFS"
  if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
    COMPREPLY=( ${COMPREPLY[0]%% - *} ) #Remove ' - ' and everything after
  fi
  return 0
} && complete -F _foo foo

After source'ing fzf-obc

source <path-to-fzf-obc>/fzf-obc.bash

When I press foo <tab-key>, I don't see the completions using the fzf-filter. I only see the default method of completion of bash.

complete -p foo returns complete -F __fzf_obc_wrapper__foo foo , so fzf-obc seems to be doing something but doesn't show the candidates in fzf.

However, when I use it one of the standard linux commands, e.g. ls -, then I see the ls options using the fzf window.

Am I missing an important step to make fzf-obc work with custom completion functions?

Thanks in advance.

@rockandska
Copy link
Owner

Hi,

It is indeed not specified, but completion functions need to use __get_cword_at_cursor_by_ref to work with fzf-obc.
A typical example on how to start a custom function is to check default ones

bash-4.4$ declare -f _cd
_cd () 
{ 
    local cur prev words cword;
    _init_completion || return;
   .....
}

That said, a completion as your example will need a post function as for gradle due to the necessity to clean up the result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants