From cabba02b2720d597d56168e9524faa18a64ecce4 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 5 Oct 2015 23:55:53 +0900 Subject: [PATCH] Created Examples (completion) (markdown) --- Examples-(completion).md | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Examples-(completion).md diff --git a/Examples-(completion).md b/Examples-(completion).md new file mode 100644 index 0000000..e6ea9d0 --- /dev/null +++ b/Examples-(completion).md @@ -0,0 +1,50 @@ +_**Custom completion API is experimental and subject to change**_ + + +Writing custom fuzzy completion +------------------------------- + +First define `_fzf_complete_COMMAND` function using `_fzf_complete` helper. + +```sh +# Custom fuzzy completion for "doge" command +# e.g. doge ** +_fzf_complete_doge() { + _fzf_complete "--multi --reverse" "$@" < <( + echo very + echo wow + echo such + echo doge + ) +} +``` + +The first argument to `_fzf_complete` is the options to fzf. The second argument is the usual arguments passed to the completion function, you can simply pass `"$@"` here. + +You can see that the output of some arbitrary commands (4 echos) are fed into the function using process substitution `< <(...)`. The lines from the output of the enclosed commands become the completion candidates. + +zsh will automatically pick up the command but in bash you have to connect the function to the command using `complete` command. + +```sh +[ -n "$BASH" ] && complete -F _fzf_complete_doge -o default -o bashdefault doge +``` + +Examples +-------- + +### [pass](http://www.passwordstore.org/) + +```sh +# pass completion suggested by @d4ndo (#362) +_fzf_complete_pass() { + _fzf_complete '+m' "$@" < <( + local pwdir=${PASSWORD_STORE_DIR-~/.password-store/} + local stringsize="${#pwdir}" + find "$pwdir" -name "*.gpg" -print | + cut -c "$((stringsize + 1))"- | + sed -e 's/\(.*\)\.gpg/\1/' + ) +} + +[ -n "$BASH" ] && complete -F _fzf_complete_pass -o default -o bashdefault pass +``` \ No newline at end of file