Skip to content
Myr

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

3 files+80 added−18 removed

modified:shell/completion.bash+30 added-7 removed
278 unmodified lines
279_fzf_proc_completion_post() {280 awk '{print $2}'281}282283__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}289290_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}293294# 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}311312_fzf_var_completion() {313 _fzf_complete -m -- "$@" < <(314 declare -xp | sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'315 )
54 unmodified lines
370for cmd in $d_cmds; do371 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"372done373374# ssh375__fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"376377unset cmd d_cmds a_cmds378379_fzf_setup_completion() {380 local kind fn cmd
17 unmodified lines
398399# 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' kill404405fi
modified:shell/completion.zsh+22 added-11 removed
213 unmodified lines
214 fi215 command rm -f "$fifo"216}217218__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}225226_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}229230# 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}245246_fzf_complete_export() {247 _fzf_complete -m -- "$@" < <(
89 unmodified lines
modified:test/test_go.rb+28 added
3363 unmodified lines
3364 ensure3365 tmux.prepare3366 tmux.send_keys 'unset -f _fzf_comprun', :Enter3367 end33683369 def test_ssh_completion3370 (1..5).each { |i| FileUtils.touch("/tmp/fzf-test-ssh-#{i}") }33713372 tmux.send_keys 'ssh jg@localhost**', :Tab3373 tmux.until do |lines|3374 assert lines.match_count >= 13375 end33763377 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-') }33903391 tmux.send_keys 'localhost**', :Tab3392 tmux.until do |lines|3393 assert lines.match_count >= 13394 end3395 end3396end33973398class TestBash < TestBase3399 include TestShell
136 unmodified lines