completion.bash
This file
Path
shell/completion.bash
264 lines · 6.6 KiB · Shell
Last changed by
0.9.12Junegunn Choibefore adoption34946b7
History of this fileTake it
Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 1–7why, one step back1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
lines 8–9why, one step back8# - $FZF_TMUX (default: 1)9# - $FZF_TMUX_HEIGHT (default: '40%')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 10–18why, one step back10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)1213_fzf_orig_completion_filter() {14 sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |15 sed 's/[^a-z0-9_= ;]/_/g'16}1718_fzf_opts_completion() {Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 19why, one step back19 local cur prev optsMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 20–21why, one step back20 COMPREPLY=()21 cur="${COMP_WORDS[COMP_CWORD]}"Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 22why, one step back22 prev="${COMP_WORDS[COMP_CWORD-1]}"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 23–30why, one step back23 opts="24 -x --extended25 -e --extended-exact26 -i +i27 -n --nth28 -d --delimiter29 +s --no-sort30 --tacPrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 31why, one step back31 --tiebreak0.9.12Junegunn Choibefore adoption34946b7
line 32why, one step back32 --bindMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 33–34why, one step back33 -m --multi34 --no-mouseUpdate completion for fzf itselfJunegunn Choibefore adoption22ae7ad
line 35why, one step back35 --colorMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 36–37why, one step back36 --black37 --reversePrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 38why, one step back38 --no-hscrollUpdate completion for fzf itselfJunegunn Choibefore adoption22ae7ad
line 39why, one step back39 --inline-infoMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 40–45why, one step back40 --prompt41 -q --query42 -1 --select-143 -0 --exit-044 -f --filter45 --print-queryPrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 46–47why, one step back46 --expect47 --toggle-sortMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
line 48why, one step back48 --sync"Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 49–52why, one step back4950 case "${prev}" in51 --tiebreak)52 COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) )Update completion for fzf itselfJunegunn Choibefore adoption22ae7ad
lines 53–56why, one step back53 return 054 ;;55 --color)56 COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 57–59why, one step back57 return 058 ;;59 esacMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 60–86why, one step back6061 if [[ ${cur} =~ ^-|\+ ]]; then62 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )63 return 064 fi6566 return 067}6869_fzf_handle_dynamic_completion() {70 local cmd orig ret71 cmd="$1"72 shift7374 orig=$(eval "echo \$_fzf_orig_completion_$cmd")75 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then76 $orig "$@"77 elif [ -n "$_fzf_completion_loader" ]; then78 _completion_loader "$@"79 ret=$?80 eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)81 source $BASH_SOURCE82 return $ret83 fi84}8586_fzf_path_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 87why, one step back87 local cur base dir leftover matches trigger cmd fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 88why, one step back88 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 89–90why, one step back89 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')90 COMPREPLY=()[bash-completion] Allow specifying empty completion trigger (#230)Junegunn Choibefore adoption64949bf
line 91why, one step back91 trigger=${FZF_COMPLETION_TRIGGER-'**'}Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 92–103why, one step back92 cur="${COMP_WORDS[COMP_CWORD]}"93 if [[ ${cur} == *"$trigger" ]]; then94 base=${cur:0:${#cur}-${#trigger}}95 eval base=$base9697 dir="$base"98 while [ 1 ]; do99 if [ -z "$dir" -o -d "$dir" ]; then100 leftover=${base/#"$dir"}101 leftover=${leftover/#\/}102 [ "$dir" = './' ] && dir=''103 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 104why, one step back104 matches=$(find -L "$dir"* $1 2> /dev/null | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; doMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 105–127why, one step back105 printf "%q$3 " "$item"106 done)107 matches=${matches% }108 if [ -n "$matches" ]; then109 COMPREPLY=( "$matches" )110 else111 COMPREPLY=( "$cur" )112 fi113 tput rc114 return 0115 fi116 dir=$(dirname "$dir")117 [[ "$dir" =~ /$ ]] || dir="$dir"/118 done119 else120 shift121 shift122 shift123 _fzf_handle_dynamic_completion "$cmd" "$@"124 fi125}126127_fzf_list_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 128why, one step back128 local cur selected trigger cmd src fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 129why, one step back129 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 130–131why, one step back130 read -r src131 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')[bash-completion] Allow specifying empty completion trigger (#230)Junegunn Choibefore adoption64949bf
line 132why, one step back132 trigger=${FZF_COMPLETION_TRIGGER-'**'}Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 133–137why, one step back133 cur="${COMP_WORDS[COMP_CWORD]}"134 if [[ ${cur} == *"$trigger" ]]; then135 cur=${cur:0:${#cur}-${#trigger}}136137 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 138why, one step back138 selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 139–172why, one step back139 selected=${selected% }140 tput rc141142 if [ -n "$selected" ]; then143 COMPREPLY=("$selected")144 return 0145 fi146 else147 shift148 _fzf_handle_dynamic_completion "$cmd" "$@"149 fi150}151152_fzf_all_completion() {153 _fzf_path_completion \154 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \155 "-m" "" "$@"156}157158_fzf_file_completion() {159 _fzf_path_completion \160 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \161 "-m" "" "$@"162}163164_fzf_dir_completion() {165 _fzf_path_completion \166 "-name .git -prune -o -name .svn -prune -o -type d -print" \167 "" "/" "$@"168}169170_fzf_kill_completion() {171 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1172Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 173why, one step back173 local selected fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 174why, one step back174 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
line 175why, one step back175 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 176why, one step back176 selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 177–186why, one step back177 tput rc178179 if [ -n "$selected" ]; then180 COMPREPLY=( "$selected" )181 return 0182 fi183}184185_fzf_telnet_completion() {186 _fzf_list_completion '+m' "$@" << "EOF"[bash-completion] Ignore 0.0.0.0Junegunn Choibefore adoptioned8202e
line 187why, one step back187 \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -uMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 188–192why, one step back188EOF189}190191_fzf_ssh_completion() {192 _fzf_list_completion '+m' "$@" << "EOF"[zsh/bash-completion] Avoid caret expansionJunegunn Choibefore adoptionc14aa99
line 193why, one step back193 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 -uMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 194–225why, one step back194EOF195}196197_fzf_env_var_completion() {198 _fzf_list_completion '-m' "$@" << "EOF"199 declare -xp | sed 's/=.*//' | sed 's/.* //'200EOF201}202203_fzf_alias_completion() {204 _fzf_list_completion '-m' "$@" << "EOF"205 alias | sed 's/=.*//' | sed 's/.* //'206EOF207}208209# fzf options210complete -F _fzf_opts_completion fzf211212d_cmds="cd pushd rmdir"213f_cmds="214 awk cat diff diff3215 emacs ex file ftp g++ gcc gvim head hg java216 javac ld less more mvim patch perl python ruby217 sed sftp sort source tail tee uniq vi view vim wc"218a_cmds="219 basename bunzip2 bzip2 chmod chown curl cp dirname du220 find git grep gunzip gzip hg jar221 ln ls mv open rm rsync scp222 svn tar unzip zip"223x_cmds="kill ssh telnet unset unalias export"224225# Preserve existing completion0.9.12Junegunn Choibefore adoption34946b7
line 226why, one step back226if [ "$_fzf_completion_loaded" != '0.9.12' ]; thenMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 227–229why, one step back227 # Really wish I could use associative array but OSX comes with bash 3.2 :(228 eval $(complete | \grep '\-F' | \grep -v _fzf_ |229 \grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)0.9.12Junegunn Choibefore adoption34946b7
line 230why, one step back230 export _fzf_completion_loaded=0.9.12Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 231–264why, one step back231fi232233if type _completion_loader > /dev/null 2>&1; then234 _fzf_completion_loader=1235fi236237# Directory238for cmd in $d_cmds; do239 complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd240done241242# File243for cmd in $f_cmds; do244 complete -F _fzf_file_completion -o default -o bashdefault $cmd245done246247# Anything248for cmd in $a_cmds; do249 complete -F _fzf_all_completion -o default -o bashdefault $cmd250done251252# Kill completion253complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill254255# Host completion256complete -F _fzf_ssh_completion -o default -o bashdefault ssh257complete -F _fzf_telnet_completion -o default -o bashdefault telnet258259# Environment variables / Aliases260complete -F _fzf_env_var_completion -o default -o bashdefault unset261complete -F _fzf_env_var_completion -o default -o bashdefault export262complete -F _fzf_alias_completion -o default -o bashdefault unalias263264unset cmd d_cmds f_cmds a_cmds x_cmds