From a12cdd9e7b65a7117946fba936730e5e59746b1a Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 13 Dec 2016 15:44:32 +0530 Subject: [PATCH] Add completion widget --- Examples-(fish).md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Examples-(fish).md b/Examples-(fish).md index e6c0df2..ca4c168 100644 --- a/Examples-(fish).md +++ b/Examples-(fish).md @@ -41,6 +41,38 @@ function fzf-select -d 'fzf commandline job and print unescaped selection back t end ``` +### Completion + +The following can replace fish completion menu with fzf. Because of a [fish bug](https://github.com/fish-shell/fish-shell/issues/3469) this will fail on variables and other elements that need escaping (or not). + +``` +function fzf-complete -d 'fzf completion and print selection back to commandline' + set -l complist (complete -C(commandline -c)) + set -l result + string join -- \n $complist | sort | eval (__fzfcmd) -m --tiebreak=index --select-1 --exit-0 --header '(commandline)' | cut -f1 | while read -l r; set result $result $r; end + + for i in (seq (count $result)) + set -l r $result[$i] + ## We need to escape the result. + switch (string sub -s 1 -l 1 -- (commandline -t)) + case "'" + commandline -t -- (string escape -- (eval "printf '%s' '$r'")) + case '"' + set -l quoted (string escape -- (eval "printf '%s' '$r'")) + set -l len (string length $quoted) + commandline -t -- '"'(string sub -s 2 -l (math $len - 2) (string escape -- (eval "printf '%s' '$r'")))'"' + case '~' + commandline -t -- (string sub -s 2 (string escape -n -- (eval "printf '%s' '$r'"))) + case '*' + commandline -t -- (string escape -n -- (eval "printf '%s' '$r'")) + end + [ $i -lt (count $result) ]; and commandline -i ' ' + end + + commandline -f repaint +end +``` + ### Git ```