Basic context-aware completion for ssh command (#3424)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Timofei Bredov · committed by GitHub · parent 3982c9a · Browse files · 3 files·+80 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
Diff stats
3 files+80 added−18 removed
- tests:+28 added−0 removed
- other:+52 added−18 removed
Provenance
- Commit
edfdcc8- Tree
bc608c1- Author
Timofei Bredov <12125537+rebrendov@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
3982c9a
3 files changed+80 added−18 removed
modified:shell/completion.bash+30 added-7 removed
278 unmodified lines279 _fzf_proc_completion_post() {280 awk '{print $2}'281 }282 283+__fzf_list_hosts() {284+ command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \285+ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \286+ <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |287+ awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | sort -u288+}289+290 _fzf_host_completion() {284- _fzf_complete +m -- "$@" < <(285- command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \286- <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \287- <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |288- awk '{if (length($2) > 0) {print $2}}' | sort -u289- )291+ _fzf_complete +m -- "$@" < <(__fzf_list_hosts "")292 }293 294+# Values for $1 $2 $3 are described here295+# https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html296+# > the first argument ($1) is the name of the command whose arguments are being completed,297+# > the second argument ($2) is the word being completed,298+# > and the third argument ($3) is the word preceding the word being completed on the current command line.299+_fzf_complete_ssh() {300+ case $3 in301+ -i|-F|-E)302+ _fzf_path_completion "$@"303+ ;;304+ *)305+ local user=306+ [[ "$2" =~ '@' ]] && user="${2%%@*}@"307+ _fzf_complete +m -- "$@" < <(__fzf_list_hosts "$user")308+ ;;309+ esac310+}311+312 _fzf_var_completion() {313 _fzf_complete -m -- "$@" < <(314 declare -xp | sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'315 )54 unmodified lines370 for cmd in $d_cmds; do371 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"372 done373 374+# ssh375+__fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"376+377 unset cmd d_cmds a_cmds378 379 _fzf_setup_completion() {380 local kind fn cmd17 unmodified lines398 399 # Environment variables / Aliases / Hosts / Process400 _fzf_setup_completion 'var' export unset printenv401 _fzf_setup_completion 'alias' unalias379-_fzf_setup_completion 'host' ssh telnet402+_fzf_setup_completion 'host' telnet403 _fzf_setup_completion 'proc' kill404 405 fimodified:shell/completion.zsh+22 added-11 removed
213 unmodified lines214 fi215 command rm -f "$fifo"216 }217 218+__fzf_list_hosts() {219+ setopt localoptions nonomatch220+ command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \221+ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \222+ <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |223+ awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | sort -u224+}225+226 _fzf_complete_telnet() {219- _fzf_complete +m -- "$@" < <(220- command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |221- awk '{if (length($2) > 0) {print $2}}' | sort -u222- )227+ _fzf_complete +m -- "$@" < <(__fzf_list_hosts "")228 }229 230+# The first and the only argument is the LBUFFER without the current word that contains the trigger.231+# The current word without the trigger is in the $prefix variable passed from the caller.232 _fzf_complete_ssh() {226- _fzf_complete +m -- "$@" < <(227- setopt localoptions nonomatch228- command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \229- <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \230- <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |231- awk '{if (length($2) > 0) {print $2}}' | sort -u232- )233+ local tokens=(${(z)1})234+ case ${tokens[-1]} in235+ -i|-F|-E)236+ _fzf_path_completion "$prefix" "$1"237+ ;;238+ *)239+ local user=240+ [[ $prefix =~ @ ]] && user="${prefix%%@*}@"241+ _fzf_complete +m -- "$@" < <(__fzf_list_hosts "$user")242+ ;;243+ esac244 }245 246 _fzf_complete_export() {247 _fzf_complete -m -- "$@" < <(89 unmodified linesmodified:test/test_go.rb+28 added
3363 unmodified lines3364 ensure3365 tmux.prepare3366 tmux.send_keys 'unset -f _fzf_comprun', :Enter3367 end3368+3369+ def test_ssh_completion3370+ (1..5).each { |i| FileUtils.touch("/tmp/fzf-test-ssh-#{i}") }3371+3372+ tmux.send_keys 'ssh jg@localhost**', :Tab3373+ tmux.until do |lines|3374+ assert lines.match_count >= 13375+ end3376+3377+ tmux.send_keys :Enter3378+ tmux.until { |lines| assert lines.any_include?('ssh jg@localhost') }3379+ tmux.send_keys ' -i /tmp/fzf-test-ssh**', :Tab3380+ tmux.until do |lines|3381+ assert lines.match_count >= 53382+ assert_equal 0, lines.select_count3383+ end3384+ tmux.send_keys :Tab, :Tab, :Tab3385+ tmux.until do |lines|3386+ assert_equal 3, lines.select_count3387+ end3388+ tmux.send_keys :Enter3389+ tmux.until { |lines| assert lines.any_include?('ssh jg@localhost -i /tmp/fzf-test-ssh-') }3390+3391+ tmux.send_keys 'localhost**', :Tab3392+ tmux.until do |lines|3393+ assert lines.match_count >= 13394+ end3395+ end3396 end3397 3398 class TestBash < TestBase3399 include TestShell136 unmodified lines