completion.bash
This file
Path
shell/completion.bash
324 lines · 8.2 KiB · Shell
Last changed by
Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9
History of this fileTake it
The walk stopped after 200 commits: lines older than a3244c4 point at it.
Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 1–153why, one step back1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#8# - $FZF_TMUX (default: 0)9# - $FZF_TMUX_HEIGHT (default: '40%')10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)1213# To use custom commands instead of find, override _fzf_compgen_{path,dir}14if ! declare -f _fzf_compgen_path > /dev/null; then15 _fzf_compgen_path() {16 echo "$1"17 command find -L "$1" \18 -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \19 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'20 }21fi2223if ! declare -f _fzf_compgen_dir > /dev/null; then24 _fzf_compgen_dir() {25 command find -L "$1" \26 -name .git -prune -o -name .svn -prune -o -type d \27 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'28 }29fi3031###########################################################3233# To redraw line after fzf closes (printf '\e[5n')34bind '"\e[0n": redraw-current-line'3536__fzfcmd_complete() {37 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&38 echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"39}4041_fzf_orig_completion_filter() {42 sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |43 awk -F= '{gsub(/[^A-Za-z0-9_= ;]/, "_", $1); print $1"="$2}'44}4546_fzf_opts_completion() {47 local cur prev opts48 COMPREPLY=()49 cur="${COMP_WORDS[COMP_CWORD]}"50 prev="${COMP_WORDS[COMP_CWORD-1]}"51 opts="52 -x --extended53 -e --exact54 --algo55 -i +i56 -n --nth57 --with-nth58 -d --delimiter59 +s --no-sort60 --tac61 --tiebreak62 -m --multi63 --no-mouse64 --bind65 --cycle66 --no-hscroll67 --jump-labels68 --height69 --literal70 --reverse71 --margin72 --inline-info73 --prompt74 --header75 --header-lines76 --ansi77 --tabstop78 --color79 --no-bold80 --history81 --history-size82 --preview83 --preview-window84 -q --query85 -1 --select-186 -0 --exit-087 -f --filter88 --print-query89 --expect90 --sync"9192 case "${prev}" in93 --tiebreak)94 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )95 return 096 ;;97 --color)98 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )99 return 0100 ;;101 --history)102 COMPREPLY=()103 return 0104 ;;105 esac106107 if [[ "$cur" =~ ^-|\+ ]]; then108 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )109 return 0110 fi111112 return 0113}114115_fzf_handle_dynamic_completion() {116 local cmd orig_var orig ret orig_cmd117 cmd="$1"118 shift119 orig_cmd="$1"120 orig_var="_fzf_orig_completion_$cmd"121 orig="${!orig_var##*#}"122 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then123 $orig "$@"124 elif [ -n "$_fzf_completion_loader" ]; then125 _completion_loader "$@"126 ret=$?127 eval "$(complete | command grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)"128 source "${BASH_SOURCE[0]}"129 return $ret130 fi131}132133__fzf_generic_path_completion() {134 local cur base dir leftover matches trigger cmd fzf135 fzf="$(__fzfcmd_complete)"136 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"137 COMPREPLY=()138 trigger=${FZF_COMPLETION_TRIGGER-'**'}139 cur="${COMP_WORDS[COMP_CWORD]}"140 if [[ "$cur" == *"$trigger" ]]; then141 base=${cur:0:${#cur}-${#trigger}}142 eval "base=$base"143144 dir="$base"145 while true; do146 if [ -z "$dir" ] || [ -d "$dir" ]; then147 leftover=${base/#"$dir"}148 leftover=${leftover/#\/}149 [ -z "$dir" ] && dir='.'150 [ "$dir" != "/" ] && dir="${dir/%\//}"151 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $2 -q "$leftover" | while read -r item; do152 printf "%q$3 " "$item"153 done)Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9
line 154why, one step back154 matches=${matches% }Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 155–200why, one step back155 if [ -n "$matches" ]; then156 COMPREPLY=( "$matches" )157 else158 COMPREPLY=( "$cur" )159 fi160 printf '\e[5n'161 return 0162 fi163 dir=$(dirname "$dir")164 [[ "$dir" =~ /$ ]] || dir="$dir"/165 done166 else167 shift168 shift169 shift170 _fzf_handle_dynamic_completion "$cmd" "$@"171 fi172}173174_fzf_complete() {175 local cur selected trigger cmd fzf post176 post="$(caller 0 | awk '{print $2}')_post"177 type -t "$post" > /dev/null 2>&1 || post=cat178 fzf="$(__fzfcmd_complete)"179180 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"181 trigger=${FZF_COMPLETION_TRIGGER-'**'}182 cur="${COMP_WORDS[COMP_CWORD]}"183 if [[ "$cur" == *"$trigger" ]]; then184 cur=${cur:0:${#cur}-${#trigger}}185186 selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ')187 selected=${selected% } # Strip trailing space not to repeat "-o nospace"188 printf '\e[5n'189190 if [ -n "$selected" ]; then191 COMPREPLY=("$selected")192 return 0193 fi194 else195 shift196 _fzf_handle_dynamic_completion "$cmd" "$@"197 fi198}199200_fzf_path_completion() {Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9
line 201why, one step back201 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 202–209why, one step back202}203204# Deprecated. No file only completion.205_fzf_file_completion() {206 _fzf_path_completion "$@"207}208209_fzf_dir_completion() {Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9
line 210why, one step back210 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 211–236why, one step back211}212213_fzf_complete_kill() {214 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1215216 local selected fzf217 fzf="$(__fzfcmd_complete)"218 selected=$(ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap $FZF_COMPLETION_OPTS" $fzf -m | awk '{print $2}' | tr '\n' ' ')219 printf '\e[5n'220221 if [ -n "$selected" ]; then222 COMPREPLY=( "$selected" )223 return 0224 fi225}226227_fzf_complete_telnet() {228 _fzf_complete '+m' "$@" < <(229 command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |230 awk '{if (length($2) > 0) {print $2}}' | sort -u231 )232}233234_fzf_complete_ssh() {235 _fzf_complete '+m' "$@" < <(236 cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \Add support for IPv6 addresses in ssh completion (#877)Sam Van Den Bergebefore adoption2ba1007
line 237why, one step back237 <(command grep -oE '^[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 238–301why, one step back238 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |239 awk '{if (length($2) > 0) {print $2}}' | sort -u240 )241}242243_fzf_complete_unset() {244 _fzf_complete '-m' "$@" < <(245 declare -xp | sed 's/=.*//' | sed 's/.* //'246 )247}248249_fzf_complete_export() {250 _fzf_complete '-m' "$@" < <(251 declare -xp | sed 's/=.*//' | sed 's/.* //'252 )253}254255_fzf_complete_unalias() {256 _fzf_complete '-m' "$@" < <(257 alias | sed 's/=.*//' | sed 's/.* //'258 )259}260261# fzf options262complete -o default -F _fzf_opts_completion fzf263264d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"265a_cmds="266 awk cat diff diff3267 emacs emacsclient ex file ftp g++ gcc gvim head hg java268 javac ld less more mvim nvim patch perl python ruby269 sed sftp sort source tail tee uniq vi view vim wc xdg-open270 basename bunzip2 bzip2 chmod chown curl cp dirname du271 find git grep gunzip gzip hg jar272 ln ls mv open rm rsync scp273 svn tar unzip zip"274x_cmds="kill ssh telnet unset unalias export"275276# Preserve existing completion277eval $(complete |278 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |279 _fzf_orig_completion_filter)280281if type _completion_loader > /dev/null 2>&1; then282 _fzf_completion_loader=1283fi284285_fzf_defc() {286 local cmd func opts orig_var orig def287 cmd="$1"288 func="$2"289 opts="$3"290 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"291 orig="${!orig_var}"292 if [ -n "$orig" ]; then293 printf -v def "$orig" "$func"294 eval "$def"295 else296 complete -F "$func" $opts "$cmd"297 fi298}299300# Anything301for cmd in $a_cmds; doRevert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9
line 302why, one step back302 _fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 303–306why, one step back303done304305# Directory306for cmd in $d_cmds; do[bash-completion] Use -o dirnames instead of -o plusdirsJunegunn Choibefore adoptiona5862d4
line 307why, one step back307 _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"Delete every line below the cursorJunegunn Choibefore adoptiona3244c4
lines 308–324why, one step back308done309310unset _fzf_defc311312# Kill completion313complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill314315# Host completion316complete -F _fzf_complete_ssh -o default -o bashdefault ssh317complete -F _fzf_complete_telnet -o default -o bashdefault telnet318319# Environment variables / Aliases320complete -F _fzf_complete_unset -o default -o bashdefault unset321complete -F _fzf_complete_export -o default -o bashdefault export322complete -F _fzf_complete_unalias -o default -o bashdefault unalias323324unset cmd d_cmds a_cmds x_cmds