Skip to content
Myr

Fuzzy completion for zsh (#227)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 315499b · Browse files · 4 files+188 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+188 added−20 removed

  • tests:+22 added−16 removed
  • other:+166 added−4 removed

4 files+188 added−20 removed

added:shell/completion.zsh+162 added
1#!/bin/zsh2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.zsh7#8# - $FZF_TMUX (default: 1)9# - $FZF_TMUX_HEIGHT (default: '40%')10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)1213_fzf_path_completion() {14 local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches15 base=$116 lbuf=$217 find_opts=$318 fzf_opts=$419 suffix=$520 tail=$621 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"2223 dir="$base"24 while [ 1 ]; do25 if [ -z "$dir" -o -d ${~dir} ]; then26 leftover=${base/#"$dir"}27 leftover=${leftover/#\/}28 [ "$dir" = './' ] && dir=''29 matches=$(find -L ${~dir}* ${=find_opts} 2> /dev/null | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$leftover" | while read item; do30 printf "%q$suffix " "$item"31 done)32 matches=${matches% }33 if [ -n "$matches" ]; then34 LBUFFER="$lbuf$matches$tail"35 zle redisplay36 fi37 return38 fi39 dir=$(dirname "$dir")40 [[ "$dir" =~ /$ ]] || dir="$dir"/41 done42}4344_fzf_all_completion() {45 _fzf_path_completion "$1" "$2" \46 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \47 "-m" "" " "48}4950_fzf_file_completion() {51 _fzf_path_completion "$1" "$2" \52 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \53 "-m" "" " "54}5556_fzf_dir_completion() {57 _fzf_path_completion "$1" "$2" \58 "-name .git -prune -o -name .svn -prune -o -type d -print" \59 "" "/" ""60}6162_fzf_list_completion() {63 local prefix lbuf fzf_opts src fzf matches64 prefix=$165 lbuf=$266 fzf_opts=$367 read -r src68 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"6970 matches=$(eval "$src" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$prefix")71 if [ -n "$matches" ]; then72 LBUFFER="$lbuf$matches "73 zle redisplay74 fi75}7677_fzf_telnet_completion() {78 _fzf_list_completion "$1" "$2" '+m' << "EOF"79 \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u80EOF81}8283_fzf_ssh_completion() {84 _fzf_list_completion "$1" "$2" '+m' << "EOF"85 cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u86EOF87}8889_fzf_env_var_completion() {90 _fzf_list_completion "$1" "$2" '+m' << "EOF"91 declare -xp | sed 's/=.*//' | sed 's/.* //'92EOF93}9495_fzf_alias_completion() {96 _fzf_list_completion "$1" "$2" '+m' << "EOF"97 alias | sed 's/=.*//'98EOF99}100101fzf-zsh-completion() {102 local tokens cmd prefix trigger tail fzf matches lbuf d_cmds f_cmds a_cmds103104 # http://zsh.sourceforge.net/FAQ/zshfaq03.html105 tokens=(${=LBUFFER})106 if [ ${#tokens} -lt 1 ]; then107 zle expand-or-complete108 return109 fi110111 cmd=${tokens[1]}112 trigger=${FZF_COMPLETION_TRIGGER:-**}113114 # Trigger sequence given115 tail=${LBUFFER:$(( ${#LBUFFER} - ${#trigger} ))}116 if [ ${#tokens} -gt 1 -a $tail = $trigger ]; then117 d_cmds=(cd pushd rmdir)118 f_cmds=(119 awk cat diff diff3120 emacs ex file ftp g++ gcc gvim head hg java121 javac ld less more mvim patch perl python ruby122 sed sftp sort source tail tee uniq vi view vim wc)123 a_cmds=(124 basename bunzip2 bzip2 chmod chown curl cp dirname du125 find git grep gunzip gzip hg jar126 ln ls mv open rm rsync scp127 svn tar unzip zip)128129 prefix=${tokens[-1]:0:-${#trigger}}130 lbuf=${LBUFFER:0:-${#tokens[-1]}}131 if [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then132 _fzf_dir_completion "$prefix" $lbuf133 elif [ ${f_cmds[(i)$cmd]} -le ${#f_cmds} ]; then134 _fzf_file_completion "$prefix" $lbuf135 elif [ ${a_cmds[(i)$cmd]} -le ${#a_cmds} ]; then136 _fzf_all_completion "$prefix" $lbuf137 elif [ $cmd = telnet ]; then138 _fzf_telnet_completion "$prefix" $lbuf139 elif [ $cmd = ssh ]; then140 _fzf_ssh_completion "$prefix" $lbuf141 elif [ $cmd = unset -o $cmd = export ]; then142 _fzf_env_var_completion "$prefix" $lbuf143 elif [ $cmd = unalias ]; then144 _fzf_alias_completion "$prefix" $lbuf145 fi146 # Kill completion (do not require trigger sequence)147 elif [ $cmd = kill -a ${LBUFFER[-1]} = ' ' ]; then148 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"149 matches=$(ps -ef | sed 1d | ${=fzf} ${=FZF_COMPLETION_OPTS} -m | awk '{print $2}' | tr '\n' ' ')150 if [ -n "$matches" ]; then151 LBUFFER="$LBUFFER$matches"152 zle redisplay153 fi154 # Fall back to default completion155 else156 zle expand-or-complete157 fi158}159160zle -N fzf-zsh-completion161bindkey '^I' fzf-zsh-completion162
modified:test/test_go.rb+22 added-16 removed
568 unmodified lines
569 tmux.until { |lines| lines[-1] == '3rd' }570 end571end572573class TestBash < TestBase574 include TestShell575576 def new_shell577 tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter578 tmux.prepare579 end580581 def setup582 super583 @tmux = Tmux.new :bash584 end585573module CompletionTest574 def test_file_completion575 tmux.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter576 tmux.prepare577 tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0
21 unmodified lines
599 end600 tmux.send_keys :xx601 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }602615 # Should not match regular files616 tmux.send_keys :Tab617 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }603 # Should not match regular files (bash-only)604 if self.class == TestBash605 tmux.send_keys :Tab606 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }607 end608609 # Fail back to plusdirs610 tmux.send_keys :BSpace, :BSpace, :BSpace611 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55' }
17 unmodified lines
629 end630 end631end632633class TestBash < TestBase634 include TestShell635 include CompletionTest636637 def new_shell638 tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter639 tmux.prepare640 end641642 def setup643 super644 @tmux = Tmux.new :bash645 end646end647648class TestZsh < TestBase649 include TestShell650 include CompletionTest651652 def new_shell653 tmux.send_keys "FZF_TMUX=0 #{Shell.zsh}", :Enter654 tmux.prepare
23 unmodified lines
modified:install+1 added-1 removed
175 unmodified lines
176 echo -n "Generate ~/.fzf.$shell ... "177 src=~/.fzf.${shell}178179 fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/shell/completion.${shell}\""180 if [ $shell != bash -o $auto_completion -ne 0 ]; then180 if [ $auto_completion -ne 0 ]; then181 fzf_completion="# $fzf_completion"182 fi183184 fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
93 unmodified lines
modified:README.md+3 added-3 removed
25 unmodified lines
26- `fzf` executable27- `fzf-tmux` script for launching fzf in a tmux pane28- Shell extensions29 - Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)30 - Fuzzy auto-completion (bash only)30 - Fuzzy auto-completion (bash, zsh)31- Vim/Neovim plugin3233You can [download fzf executable][bin] alone, but it's recommended that you34install the extra stuff using the attached install script.
137 unmodified lines
172173It will still work even when you're not on tmux, silently ignoring `-[udlr]`174options, so you can invariably use `fzf-tmux` in your scripts.175176Fuzzy completion for bash177-------------------------176Fuzzy completion for bash and zsh177---------------------------------178179#### Files and directories180181Fuzzy completion for files and directories can be triggered if the word before
252 unmodified lines