Skip to content
Myr

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 file

Take it

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 1–7why, one step back
1#!/bin/bash
2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#

FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba

lines 8–9why, one step back
8# - $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 back
10# - $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 back
19 local cur prev opts

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 20–21why, one step back
20 COMPREPLY=()
21 cur="${COMP_WORDS[COMP_CWORD]}"

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

line 22why, one step back
22 prev="${COMP_WORDS[COMP_CWORD-1]}"

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 23–30why, one step back
23 opts="
24 -x --extended25 -e --extended-exact26 -i +i27 -n --nth28 -d --delimiter29 +s --no-sort30 --tac

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

line 31why, one step back
31 --tiebreak

0.9.12Junegunn Choibefore adoption34946b7

line 32why, one step back
32 --bind

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 33–34why, one step back
33 -m --multi
34 --no-mouse

Update completion for fzf itselfJunegunn Choibefore adoption22ae7ad

line 35why, one step back
35 --color

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 36–37why, one step back
36 --black
37 --reverse

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

line 38why, one step back
38 --no-hscroll

Update completion for fzf itselfJunegunn Choibefore adoption22ae7ad

line 39why, one step back
39 --inline-info

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 40–45why, one step back
40 --prompt
41 -q --query42 -1 --select-143 -0 --exit-044 -f --filter45 --print-query

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

lines 46–47why, one step back
46 --expect
47 --toggle-sort

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

line 48why, one step back
48 --sync"

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

lines 49–52why, one step back
49
50 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 back
53 return 0
54 ;;55 --color)56 COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )

Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c

lines 57–59why, one step back
57 return 0
58 ;;59 esac

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 60–86why, one step back
60
61 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 back
87 local cur base dir leftover matches trigger cmd fzf

FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba

line 88why, one step back
88 [ ${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 back
89 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 back
91 trigger=${FZF_COMPLETION_TRIGGER-'**'}

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 92–103why, one step back
92 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 sc

Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09

line 104why, one step back
104 matches=$(find -L "$dir"* $1 2> /dev/null | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 105–127why, one step back
105 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 back
128 local cur selected trigger cmd src fzf

FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba

line 129why, one step back
129 [ ${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 back
130 read -r src
131 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 back
132 trigger=${FZF_COMPLETION_TRIGGER-'**'}

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 133–137why, one step back
133 cur="${COMP_WORDS[COMP_CWORD]}"
134 if [[ ${cur} == *"$trigger" ]]; then135 cur=${cur:0:${#cur}-${#trigger}}136137 tput sc

Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09

line 138why, one step back
138 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 back
139 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 1172

Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09

line 173why, one step back
173 local selected fzf

FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba

line 174why, one step back
174 [ ${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 back
175 tput sc

Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09

line 176why, one step back
176 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 back
177 tput rc
178179 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 back
187 \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 188–192why, one step back
188EOF
189}190191_fzf_ssh_completion() {192 _fzf_list_completion '+m' "$@" << "EOF"

[zsh/bash-completion] Avoid caret expansionJunegunn Choibefore adoptionc14aa99

line 193why, one step back
193 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 -u

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 194–225why, one step back
194EOF
195}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 completion

0.9.12Junegunn Choibefore adoption34946b7

line 226why, one step back
226if [ "$_fzf_completion_loaded" != '0.9.12' ]; then

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 227–229why, one step back
227 # 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 back
230 export _fzf_completion_loaded=0.9.12

Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac

lines 231–264why, one step back
231fi
232233if 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