completion.bash
This file
Path
shell/completion.bash
271 lines · 6.7 KiB · Shell
Last changed by
Add support for search historyJunegunn Choibefore adoption3b52811
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-sortAdd support for search historyJunegunn Choibefore adoption3b52811
lines 48–51why, one step back48 --sync49 --null50 --history51 --history-max"Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 52–55why, one step back5253 case "${prev}" in54 --tiebreak)55 COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) )Update completion for fzf itselfJunegunn Choibefore adoption22ae7ad
lines 56–59why, one step back56 return 057 ;;58 --color)59 COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 60–61why, one step back60 return 061 ;;Add support for search historyJunegunn Choibefore adoption3b52811
lines 62–65why, one step back62 --history)63 COMPREPLY=()64 return 065 ;;Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 66why, one step back66 esacMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 67–93why, one step back6768 if [[ ${cur} =~ ^-|\+ ]]; then69 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )70 return 071 fi7273 return 074}7576_fzf_handle_dynamic_completion() {77 local cmd orig ret78 cmd="$1"79 shift8081 orig=$(eval "echo \$_fzf_orig_completion_$cmd")82 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then83 $orig "$@"84 elif [ -n "$_fzf_completion_loader" ]; then85 _completion_loader "$@"86 ret=$?87 eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)88 source $BASH_SOURCE89 return $ret90 fi91}9293_fzf_path_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 94why, one step back94 local cur base dir leftover matches trigger cmd fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 95why, one step back95 [ ${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 96–97why, one step back96 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')97 COMPREPLY=()[bash-completion] Allow specifying empty completion trigger (#230)Junegunn Choibefore adoption64949bf
line 98why, one step back98 trigger=${FZF_COMPLETION_TRIGGER-'**'}Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 99–110why, one step back99 cur="${COMP_WORDS[COMP_CWORD]}"100 if [[ ${cur} == *"$trigger" ]]; then101 base=${cur:0:${#cur}-${#trigger}}102 eval base=$base103104 dir="$base"105 while [ 1 ]; do106 if [ -z "$dir" -o -d "$dir" ]; then107 leftover=${base/#"$dir"}108 leftover=${leftover/#\/}109 [ "$dir" = './' ] && dir=''110 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 111why, one step back111 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 112–134why, one step back112 printf "%q$3 " "$item"113 done)114 matches=${matches% }115 if [ -n "$matches" ]; then116 COMPREPLY=( "$matches" )117 else118 COMPREPLY=( "$cur" )119 fi120 tput rc121 return 0122 fi123 dir=$(dirname "$dir")124 [[ "$dir" =~ /$ ]] || dir="$dir"/125 done126 else127 shift128 shift129 shift130 _fzf_handle_dynamic_completion "$cmd" "$@"131 fi132}133134_fzf_list_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 135why, one step back135 local cur selected trigger cmd src fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 136why, one step back136 [ ${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 137–138why, one step back137 read -r src138 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')[bash-completion] Allow specifying empty completion trigger (#230)Junegunn Choibefore adoption64949bf
line 139why, one step back139 trigger=${FZF_COMPLETION_TRIGGER-'**'}Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 140–144why, one step back140 cur="${COMP_WORDS[COMP_CWORD]}"141 if [[ ${cur} == *"$trigger" ]]; then142 cur=${cur:0:${#cur}-${#trigger}}143144 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 145why, one step back145 selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 146–179why, one step back146 selected=${selected% }147 tput rc148149 if [ -n "$selected" ]; then150 COMPREPLY=("$selected")151 return 0152 fi153 else154 shift155 _fzf_handle_dynamic_completion "$cmd" "$@"156 fi157}158159_fzf_all_completion() {160 _fzf_path_completion \161 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \162 "-m" "" "$@"163}164165_fzf_file_completion() {166 _fzf_path_completion \167 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \168 "-m" "" "$@"169}170171_fzf_dir_completion() {172 _fzf_path_completion \173 "-name .git -prune -o -name .svn -prune -o -type d -print" \174 "" "/" "$@"175}176177_fzf_kill_completion() {178 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1179Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 180why, one step back180 local selected fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 181why, one step back181 [ ${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 182why, one step back182 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 183why, one step back183 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 184–193why, one step back184 tput rc185186 if [ -n "$selected" ]; then187 COMPREPLY=( "$selected" )188 return 0189 fi190}191192_fzf_telnet_completion() {193 _fzf_list_completion '+m' "$@" << "EOF"[bash-completion] Ignore 0.0.0.0Junegunn Choibefore adoptioned8202e
line 194why, one step back194 \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 195–199why, one step back195EOF196}197198_fzf_ssh_completion() {199 _fzf_list_completion '+m' "$@" << "EOF"[zsh/bash-completion] Avoid caret expansionJunegunn Choibefore adoptionc14aa99
line 200why, one step back200 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 201–216why, one step back201EOF202}203204_fzf_env_var_completion() {205 _fzf_list_completion '-m' "$@" << "EOF"206 declare -xp | sed 's/=.*//' | sed 's/.* //'207EOF208}209210_fzf_alias_completion() {211 _fzf_list_completion '-m' "$@" << "EOF"212 alias | sed 's/=.*//' | sed 's/.* //'213EOF214}215216# fzf optionsAdd support for search historyJunegunn Choibefore adoption3b52811
line 217why, one step back217complete -o default -F _fzf_opts_completion fzfMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 218–232why, one step back218219d_cmds="cd pushd rmdir"220f_cmds="221 awk cat diff diff3222 emacs ex file ftp g++ gcc gvim head hg java223 javac ld less more mvim patch perl python ruby224 sed sftp sort source tail tee uniq vi view vim wc"225a_cmds="226 basename bunzip2 bzip2 chmod chown curl cp dirname du227 find git grep gunzip gzip hg jar228 ln ls mv open rm rsync scp229 svn tar unzip zip"230x_cmds="kill ssh telnet unset unalias export"231232# Preserve existing completion0.9.12Junegunn Choibefore adoption34946b7
line 233why, one step back233if [ "$_fzf_completion_loaded" != '0.9.12' ]; thenMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 234–236why, one step back234 # Really wish I could use associative array but OSX comes with bash 3.2 :(235 eval $(complete | \grep '\-F' | \grep -v _fzf_ |236 \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 237why, one step back237 export _fzf_completion_loaded=0.9.12Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 238–271why, one step back238fi239240if type _completion_loader > /dev/null 2>&1; then241 _fzf_completion_loader=1242fi243244# Directory245for cmd in $d_cmds; do246 complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd247done248249# File250for cmd in $f_cmds; do251 complete -F _fzf_file_completion -o default -o bashdefault $cmd252done253254# Anything255for cmd in $a_cmds; do256 complete -F _fzf_all_completion -o default -o bashdefault $cmd257done258259# Kill completion260complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill261262# Host completion263complete -F _fzf_ssh_completion -o default -o bashdefault ssh264complete -F _fzf_telnet_completion -o default -o bashdefault telnet265266# Environment variables / Aliases267complete -F _fzf_env_var_completion -o default -o bashdefault unset268complete -F _fzf_env_var_completion -o default -o bashdefault export269complete -F _fzf_alias_completion -o default -o bashdefault unalias270271unset cmd d_cmds f_cmds a_cmds x_cmds