Make fuzzy completion customizable with _fzf_compgen_{path,dir}
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Notes: - You can now override _fzf_compgen_path and _fzf_compgen_dir functions to use custom commands such as ag instead of find for listing completion candidates. - The first argument is the base path to start traversal - Removed file-only completion in bash, i.e. _fzf_file_completion. Maintaining a list of commands that only expect files, not directories, is cumbersome (there are too many) and error-prone. TBD: - Added $FZF_COMPLETION_DIR_COMMANDS to customize the list of commands which use directory-only completion. The default is "cd pushd rmdir". Not sure if it's the best approach to address the requirement, I'll leave it as an undocumented feature.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Related: #406 (@thomcom), #456 (@frizinak)
Diff stats
4 files+84 added−35 removed
- tests:+15 added−1 removed
- other:+69 added−34 removed
Provenance
- Commit
9617647- Tree
81d1594- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
68c8426
4 files changed+84 added−35 removed
modified:shell/completion.bash+35 added-26 removed
8 unmodified lines9 # - $FZF_TMUX_HEIGHT (default: '40%')10 # - $FZF_COMPLETION_TRIGGER (default: '**')11 # - $FZF_COMPLETION_OPTS (default: empty)12 13+# To use custom commands instead of find, override _fzf_compgen_{path,dir}14+if ! declare -f _fzf_compgen_path > /dev/null; then15+ _fzf_compgen_path() {16+ echo "$1"17+ \find -L "$1" \18+ -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \19+ -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'20+ }21+fi22+23+if ! declare -f _fzf_compgen_dir > /dev/null; then24+ _fzf_compgen_dir() {25+ \find -L "$1" \26+ -name .git -prune -o -name .svn -prune -o -type d \27+ -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'28+ }29+fi30+31+###########################################################32+33 _fzf_orig_completion_filter() {34 sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |35 awk -F= '{gsub(/[^a-z0-9_= ;]/, "_", $1); print $1"="$2}'36 }95 unmodified lines132 leftover=${leftover/#\/}133 [ -z "$dir" ] && dir='.'134 [ "$dir" != "/" ] && dir="${dir/%\//}"135 tput sc116- matches=$(\find -L "$dir" $1 -a -not -path "$dir" -print 2> /dev/null | sed 's@^\./@@' | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do136+ matches=$(eval "$1 $(printf %q "$dir")" | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do137 printf "%q$3 " "$item"138 done)139 matches=${matches% }140 if [ -n "$matches" ]; then49 unmodified lines190 fi191 }192 193 _fzf_path_completion() {174- __fzf_generic_path_completion \175- "-name .git -prune -o -name .svn -prune -o ( -type d -o -type f -o -type l )" \176- "-m" "" "$@"194+ __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"195 }196 197+# Deprecated. No file only completion.198 _fzf_file_completion() {180- __fzf_generic_path_completion \181- "-name .git -prune -o -name .svn -prune -o ( -type f -o -type l )" \182- "-m" "" "$@"199+ _fzf_path_completion "$@"200 }201 202 _fzf_dir_completion() {186- __fzf_generic_path_completion \187- "-name .git -prune -o -name .svn -prune -o -type d" \188- "" "/" "$@"203+ __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"204 }205 206 _fzf_complete_kill() {207 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 145 unmodified lines253 254 # fzf options255 complete -o default -F _fzf_opts_completion fzf256 242-d_cmds="cd pushd rmdir"243-f_cmds="257+d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"258+a_cmds="259 awk cat diff diff3260 emacs emacsclient ex file ftp g++ gcc gvim head hg java261 javac ld less more mvim nvim patch perl python ruby247- sed sftp sort source tail tee uniq vi view vim wc xdg-open"248-a_cmds="262+ sed sftp sort source tail tee uniq vi view vim wc xdg-open263 basename bunzip2 bzip2 chmod chown curl cp dirname du264 find git grep gunzip gzip hg jar265 ln ls mv open rm rsync scp266 svn tar unzip zip"2 unmodified lines269 # Preserve existing completion270 if [ "$_fzf_completion_loaded" != '0.10.8' ]; then271 # Really wish I could use associative array but OSX comes with bash 3.2 :(272 eval $(complete | \grep '\-F' | \grep -v _fzf_ |259- \grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)273+ \grep -E " ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)274 export _fzf_completion_loaded=0.10.8275 fi276 277 if type _completion_loader > /dev/null 2>&1; then13 unmodified lines291 complete -F "$func" $opts "$cmd"292 fi293 }294 281-# Directory282-for cmd in $d_cmds; do283- _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"284-done285-286-# File287-for cmd in $f_cmds; do288- _fzf_defc "$cmd" _fzf_file_completion "-o default -o bashdefault"289-done290-295 # Anything296 for cmd in $a_cmds; do297 _fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"298 done299 300+# Directory301+for cmd in $d_cmds; do302+ _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"303+done304+305 unset _fzf_defc306 307 # Kill completion308 complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill6 unmodified lines315 complete -F _fzf_complete_unset -o default -o bashdefault unset316 complete -F _fzf_complete_export -o default -o bashdefault export317 complete -F _fzf_complete_unalias -o default -o bashdefault unalias318 310-unset cmd d_cmds f_cmds a_cmds x_cmds319+unset cmd d_cmds a_cmds x_cmdsmodified:shell/completion.zsh+26 added-8 removed
8 unmodified lines910111213141516171819202122232425262728293031323314343536371838394041429 unmodified lines525354553656575859608 unmodified lines69707172535473747576775960787980818279 unmodified lines16216316416514816616716816917018 unmodified lines8 unmodified lines# - $FZF_TMUX_HEIGHT (default: '40%')# - $FZF_COMPLETION_TRIGGER (default: '**')# - $FZF_COMPLETION_OPTS (default: empty)
# To use custom commands instead of find, override _fzf_compgen_{path,dir}if ! declare -f _fzf_compgen_path > /dev/null; then _fzf_compgen_path() { echo "$1" \find -L "$1" \ -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \ -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' }fi
if ! declare -f _fzf_compgen_dir > /dev/null; then _fzf_compgen_dir() { \find -L "$1" \ -name .git -prune -o -name .svn -prune -o -type d \ -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' }fi
###########################################################
__fzf_generic_path_completion() { local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm local base lbuf compgen fzf_opts suffix tail fzf dir leftover matches nnm # (Q) flag removes a quoting level: "foo\ bar" => "foo bar" base=${(Q)1} lbuf=$2 find_opts=$3 compgen=$3 fzf_opts=$4 suffix=$5 tail=$6 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"9 unmodified lines leftover=${leftover/#\/} [ -z "$dir" ] && dir='.' [ "$dir" != "/" ] && dir="${dir/%\//}" dir=${~dir} matches=$(\find -L "$dir" ${=find_opts} -a -not -path "$dir" -print 2> /dev/null | sed 's@^\./@@' | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$leftover" | while read item; do matches=$(eval "$compgen $(printf %q "$dir")" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$leftover" | while read item; do printf "%q$suffix " "$item" done) matches=${matches% } if [ -n "$matches" ]; then8 unmodified lines [ -n "$nnm" ] && unsetopt nonomatch}
_fzf_path_completion() { __fzf_generic_path_completion "$1" "$2" \ "-name .git -prune -o -name .svn -prune -o ( -type d -o -type f -o -type l )" \ __fzf_generic_path_completion "$1" "$2" _fzf_compgen_path \ "-m" "" " "}
_fzf_dir_completion() { __fzf_generic_path_completion "$1" "$2" \ "-name .git -prune -o -name .svn -prune -o -type d" \ __fzf_generic_path_completion "$1" "$2" _fzf_compgen_dir \ "" "/" ""}
_fzf_feed_fifo() (79 unmodified lines fi zle redisplay # Trigger sequence given elif [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then d_cmds=(cd pushd rmdir) d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir})
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}} [ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
18 unmodified linesmodified:test/test_go.rb+15 added-1 removed
1267 unmodified lines1268 # /tmp/fzf\ test**<TAB>1269 tmux.send_keys 'C-u'1270 tmux.send_keys 'cat /tmp/fzf\ test/**', :Tab, pane: 01271 tmux.until(1) { |lines| lines.item_count > 0 }1272- tmux.send_keys :Enter1272+ tmux.send_keys 'C-K', :Enter1273 tmux.until do |lines|1274 tmux.send_keys 'C-L'1275 lines[-1].end_with?('/tmp/fzf\ test/foobar')1276 end61 unmodified lines1338 tmux.until do |lines|1339 tmux.send_keys 'C-L'1340 lines[-1] == "kill #{pid}"1341 end1342+1343+ def test_custom_completion1344+ tmux.send_keys '_fzf_compgen_path() { echo "\$1"; seq 10; }', :Enter1345+ tmux.prepare1346+ tmux.send_keys 'ls /tmp/**', :Tab, pane: 01347+ tmux.until(1) { |lines| lines.item_count == 11 }1348+ tmux.send_keys :BTab, :BTab, :BTab1349+ tmux.until(1) { |lines| lines[-2].include? '(3)' }1350+ tmux.send_keys :Enter1351+ tmux.until do |lines|1352+ tmux.send_keys 'C-L'1353+ lines[-1] == "ls /tmp 1 2"1354+ end1355+ end1356 ensure1357 Process.kill 'KILL', pid.to_i rescue nil if pid1358 end1359 end53 unmodified linesmodified:README.md+8 added
257 unmodified lines258 export FZF_COMPLETION_TRIGGER='~~'259 260 # Options to fzf command261 export FZF_COMPLETION_OPTS='+c -x'262+263+# Use ag instead of the default find command for listing candidates.264+# - The first argument to the function is the base path to start traversal265+# - Note that ag only lists files not directories266+# - See the source code (completion.{bash,zsh}) for the details.267+_fzf_compgen_paths() {268+ ag -g "" "$1"269+}270 ```271 272 #### Supported commands273 136 unmodified lines