Skip to content
Myr

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.

authored by Junegunn Choi · committed by Junegunn Choi · parent 68c8426 · Browse files · 4 files+84 added−35 removed
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

4 files+84 added−35 removed

modified:shell/completion.bash+35 added-26 removed
8 unmodified lines
9# - $FZF_TMUX_HEIGHT (default: '40%')10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)1213# To use custom commands instead of find, override _fzf_compgen_{path,dir}14if ! 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 }21fi2223if ! 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 }29fi3031###########################################################3233_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 lines
132 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" ]; then
49 unmodified lines
190 fi191}192193_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}196197# 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}201202_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}205206_fzf_complete_kill() {207 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
45 unmodified lines
253254# fzf options255complete -o default -F _fzf_opts_completion fzf256242d_cmds="cd pushd rmdir"243f_cmds="257d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"258a_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"248a_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 lines
269# Preserve existing completion270if [ "$_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.8275fi276277if type _completion_loader > /dev/null 2>&1; then
13 unmodified lines
291 complete -F "$func" $opts "$cmd"292 fi293}294281# Directory282for cmd in $d_cmds; do283 _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"284done285286# File287for cmd in $f_cmds; do288 _fzf_defc "$cmd" _fzf_file_completion "-o default -o bashdefault"289done290295# Anything296for cmd in $a_cmds; do297 _fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"298done299300# Directory301for cmd in $d_cmds; do302 _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"303done304305unset _fzf_defc306307# Kill completion308complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill
6 unmodified lines
315complete -F _fzf_complete_unset -o default -o bashdefault unset316complete -F _fzf_complete_export -o default -o bashdefault export317complete -F _fzf_complete_unalias -o default -o bashdefault unalias318310unset cmd d_cmds f_cmds a_cmds x_cmds319unset cmd d_cmds a_cmds x_cmds
modified:shell/completion.zsh+26 added-8 removed
modified:test/test_go.rb+15 added-1 removed
1267 unmodified lines
1268 # /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 end
61 unmodified lines
1338 tmux.until do |lines|1339 tmux.send_keys 'C-L'1340 lines[-1] == "kill #{pid}"1341 end13421343 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 end1359end
53 unmodified lines
modified:README.md+8 added
257 unmodified lines
258export FZF_COMPLETION_TRIGGER='~~'259260# Options to fzf command261export FZF_COMPLETION_OPTS='+c -x'262263# 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```271272#### Supported commands273
136 unmodified lines