From f660ad35b2a28fd21778ba0a80e880e27ab32a9b Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 23 Nov 2013 20:09:56 +0900 Subject: [PATCH] Improve bash completion: `[DIRECTORY/][FUZZY_PATTERN]**` --- README.md | 28 +++++++++++++++++----------- fzf-completion.bash | 36 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 35c8fd9..7cbed42 100644 --- a/README.md +++ b/README.md @@ -73,12 +73,13 @@ Usage ``` usage: fzf [options] - -m, --multi Enable multi-select - -x, --extended Extended-search mode - -s, --sort=MAX Maximum number of matched items to sort. Default: 1000 - +s, --no-sort Do not sort the result. Keep the sequence unchanged. - +i Case-sensitive match - +c, --no-color Disable colors + -m, --multi Enable multi-select + -x, --extended Extended-search mode + -q, --query=STR Initial query + -s, --sort=MAX Maximum number of matched items to sort. Default: 1000 + +s, --no-sort Do not sort the result. Keep the sequence unchanged. + +i Case-sensitive match + +c, --no-color Disable colors ``` fzf will launch curses-based finder, read the list from STDIN, and write the @@ -266,8 +267,10 @@ over time* ### bash -fuzzy-finder-completion can be triggered if you type in a directory name -followed by the trigger sequence which is by default `**`. +Fuzzy completion can be triggered if the word before the cursor ends +with the trigger sequence which is by default `**`. + +- `COMMAND [DIRECTORY/][FUZZY_PATTERN]**` #### Examples @@ -277,7 +280,10 @@ followed by the trigger sequence which is by default `**`. vim ** # Files under parent directory -vim ..** +vim ../** + +# Files under parent directory that match `fzf` +vim ../fzf** # Files under your home directory vim ~/** @@ -286,8 +292,8 @@ vim ~/** # Directories under current directory (single-selection) cd ** -# Directories under parent directory -cd ../** +# Directories under ~/github that match `fzf` +cd ~/github/fzf** ``` #### Settings diff --git a/fzf-completion.bash b/fzf-completion.bash index e52a8f9..bdb8568 100644 --- a/fzf-completion.bash +++ b/fzf-completion.bash @@ -31,31 +31,37 @@ _fzf_opts_completion() { } _fzf_generic_completion() { - local cur prev opts base matches + local cur prev opts base dir leftover matches COMPREPLY=() FZF_COMPLETION_TRIGGER=${FZF_COMPLETION_TRIGGER:-**} cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" if [[ ${cur} == *"$FZF_COMPLETION_TRIGGER" ]]; then base=${cur:0:${#cur}-${#FZF_COMPLETION_TRIGGER}} - base=${base%/} eval base=$base - find_opts="-name .git -prune -o -name .svn -prune -o" - if [ -z "$base" -o -d "$base" ]; then - matches=$(find ${base:-*} $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 | while read item; do - if [[ ${item} =~ \ ]]; then - echo -n "\"$item\" " - else - echo -n "$item " + dir="$base" + while [ 1 ]; do + if [ -z "$dir" -o -d "$dir" ]; then + leftover=${base/#"$dir"} + leftover=${leftover/#\/} + [ "$dir" = '.' ] && dir='' + matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do + if [[ ${item} =~ \ ]]; then + echo -n "\"$item\" " + else + echo -n "$item " + fi + done) + matches=${matches% } + if [ -n "$matches" ]; then + COMPREPLY=( "$matches" ) + return 0 fi - done) - matches=${matches% } - if [ -n "$matches" ]; then - COMPREPLY=( "$matches" ) - return 0 + return 1 fi - fi + dir=$(dirname "$dir") + done fi }