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

"Dedicated completion key" for bash #1804

Closed
5 of 10 tasks
connorbeckerle opened this issue Dec 28, 2019 · 1 comment
Closed
5 of 10 tasks

"Dedicated completion key" for bash #1804

connorbeckerle opened this issue Dec 28, 2019 · 1 comment

Comments

@connorbeckerle
Copy link

connorbeckerle commented Dec 28, 2019

  • I have read through the manual page (man fzf)
  • I have the latest version of fzf
  • I have searched through the existing issues

Info

  • OS
    • Linux
    • Mac OS X
    • Windows
    • Etc.
  • Shell
    • bash
    • zsh
    • fish

Problem / Steps to reproduce

Not sure how to implement "Dedicated completion key" (https://github.com/junegunn/fzf/wiki/Configuring-fuzzy-completion) for bash. Here is my attempt at it (complete with commented out failed attempts):

[ -f ~/.fzf.bash ] && source ~/.fzf.bash
[ -f ~/.git.bash ] && . ~/.git.bash

export FZF_COMPLETION_TRIGGER=''  # previously '**'
bind -x '"\C-e": fzf-completion'
# alias ^E='fzf-completion'  # doesn't work

Thanks!

@junegunn
Copy link
Owner

The exact equivalent is not possible in bash because unlike in zsh, you have to register a completion function for each command, command by command. So, we have to use a different mechanism to implement similar functionality.

Write a function for a specific type of list and bind it to a dedicated key like so:

# Complete word. Works on bash 4+
fzf-word-widget() {
  local prefix=${READLINE_LINE:0:$READLINE_POINT}
  local token=$(sed 's/.* //' <<< "$prefix")
  [ ${#token} -gt 0 ] && prefix=${prefix::-${#token}}

  local selected="$(fzf --height=10 --reverse --border --info=inline --query="$token" < /usr/share/dict/words)"
  [ -z "$selected" ] && return

  READLINE_LINE="$prefix$selected${READLINE_LINE:$READLINE_POINT}"
  READLINE_POINT=$(( READLINE_POINT - ${#token} + ${#selected} ))
}

bind -m emacs-standard -x '"\C-x\C-w": "fzf-word-widget"'

Similarly, you can write functions for directories, processes, etc and bind them to different keys. Or, you could write a smarter "one-size-fits-all" function that extracts the name of the command on the prompt and automatically starts fzf with the most appropriate type of list. That would be really cool.

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

No branches or pull requests

2 participants