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
Provenance
- Commit
2b8e445- Tree
80cf569- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
315499b
4 files changed+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)12+13+_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"22+23+ 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+}43+44+_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+}49+50+_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+}55+56+_fzf_dir_completion() {57+ _fzf_path_completion "$1" "$2" \58+ "-name .git -prune -o -name .svn -prune -o -type d -print" \59+ "" "/" ""60+}61+62+_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"69+70+ matches=$(eval "$src" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$prefix")71+ if [ -n "$matches" ]; then72+ LBUFFER="$lbuf$matches "73+ zle redisplay74+ fi75+}76+77+_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 -u80+EOF81+}82+83+_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 -u86+EOF87+}88+89+_fzf_env_var_completion() {90+ _fzf_list_completion "$1" "$2" '+m' << "EOF"91+ declare -xp | sed 's/=.*//' | sed 's/.* //'92+EOF93+}94+95+_fzf_alias_completion() {96+ _fzf_list_completion "$1" "$2" '+m' << "EOF"97+ alias | sed 's/=.*//'98+EOF99+}100+101+fzf-zsh-completion() {102+ local tokens cmd prefix trigger tail fzf matches lbuf d_cmds f_cmds a_cmds103+104+ # http://zsh.sourceforge.net/FAQ/zshfaq03.html105+ tokens=(${=LBUFFER})106+ if [ ${#tokens} -lt 1 ]; then107+ zle expand-or-complete108+ return109+ fi110+111+ cmd=${tokens[1]}112+ trigger=${FZF_COMPLETION_TRIGGER:-**}113+114+ # 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)128+129+ 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+}159+160+zle -N fzf-zsh-completion161+bindkey '^I' fzf-zsh-completion162+modified:test/test_go.rb+22 added-16 removed
568 unmodified lines569 tmux.until { |lines| lines[-1] == '3rd' }570 end571 end572 573-class TestBash < TestBase574- include TestShell575-576- def new_shell577- tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter578- tmux.prepare579- end580-581- def setup582- super583- @tmux = Tmux.new :bash584- end585-573+module 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: 021 unmodified lines599 end600 tmux.send_keys :xx601 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }602 615- # 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+ end608 609 # Fail back to plusdirs610 tmux.send_keys :BSpace, :BSpace, :BSpace611 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55' }17 unmodified lines629 end630 end631 end632 633+class TestBash < TestBase634+ include TestShell635+ include CompletionTest636+637+ def new_shell638+ tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter639+ tmux.prepare640+ end641+642+ def setup643+ super644+ @tmux = Tmux.new :bash645+ end646+end647+648 class TestZsh < TestBase649 include TestShell650+ include CompletionTest651 652 def new_shell653 tmux.send_keys "FZF_TMUX=0 #{Shell.zsh}", :Enter654 tmux.prepare23 unmodified linesmodified:install+1 added-1 removed
175 unmodified lines176 echo -n "Generate ~/.fzf.$shell ... "177 src=~/.fzf.${shell}178 179 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 fi183 184 fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""93 unmodified linesmodified:README.md+3 added-3 removed
25 unmodified lines26 - `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 plugin32 33 You can [download fzf executable][bin] alone, but it's recommended that you34 install the extra stuff using the attached install script.137 unmodified lines172 173 It will still work even when you're not on tmux, silently ignoring `-[udlr]`174 options, so you can invariably use `fzf-tmux` in your scripts.175 176-Fuzzy completion for bash177--------------------------176+Fuzzy completion for bash and zsh177+---------------------------------178 179 #### Files and directories180 181 Fuzzy completion for files and directories can be triggered if the word before252 unmodified lines