Improve bash completion: `[DIRECTORY/][FUZZY_PATTERN]**<TAB>`

This commit is contained in:
Junegunn Choi 2013-11-23 20:09:56 +09:00
parent c61738ae43
commit f660ad35b2
2 changed files with 38 additions and 26 deletions

View File

@ -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]**<TAB>`
#### Examples
@ -277,7 +280,10 @@ followed by the trigger sequence which is by default `**`.
vim **<TAB>
# Files under parent directory
vim ..**<TAB>
vim ../**<TAB>
# Files under parent directory that match `fzf`
vim ../fzf**<TAB>
# Files under your home directory
vim ~/**<TAB>
@ -286,8 +292,8 @@ vim ~/**<TAB>
# Directories under current directory (single-selection)
cd **<TAB>
# Directories under parent directory
cd ../**<TAB>
# Directories under ~/github that match `fzf`
cd ~/github/fzf**<TAB>
```
#### Settings

View File

@ -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
}