@@ -459,12 +459,30 @@ _comp_cmd_sftp()
459
459
# shellcheck disable=SC2089
460
460
_comp_cmd_scp__path_esc=' [][(){}<>"' " '" ' ,:;^&!$=?`\\|[:space:]]'
461
461
462
- # Complete remote files with ssh. If the first arg is -d, complete on dirs
463
- # only. Returns paths escaped with three backslashes.
462
+ # Complete remote files with ssh. Returns paths escaped with three backslashes
463
+ # (unless -l option is provided).
464
+ # Options:
465
+ # -d Complete on dirs only.
466
+ # -l Return paths escaped with one backslash instead of three.
464
467
# @since 2.12
465
468
# shellcheck disable=SC2120
466
469
_comp_xfunc_scp_compgen_remote_files ()
467
470
{
471
+ local _dirs_only=" "
472
+ local _less_escaping=" "
473
+
474
+ local _flag OPTIND=1 OPTARG=" " OPTERR=0
475
+ while getopts " dl" _flag " $@ " ; do
476
+ case $_flag in
477
+ d) _dirs_only=set ;;
478
+ l) _less_escaping=set ;;
479
+ * )
480
+ echo " bash_completion: $FUNCNAME : usage error: $* " >&2
481
+ return 1
482
+ ;;
483
+ esac
484
+ done
485
+
468
486
# remove backslash escape from the first colon
469
487
cur=${cur/ \\ :/: }
470
488
@@ -480,20 +498,25 @@ _comp_xfunc_scp_compgen_remote_files()
480
498
_path=$( ssh -o ' Batchmode yes' " $_userhost " pwd 2> /dev/null)
481
499
fi
482
500
501
+ local _escape_replacement=' \\\\\\&'
502
+ if [[ $_less_escaping ]]; then
503
+ _escape_replacement=' \\&'
504
+ fi
505
+
483
506
local _files
484
- if [[ ${1-} == -d ]]; then
507
+ if [[ $_dirs_only ]]; then
485
508
# escape problematic characters; remove non-dirs
486
509
# shellcheck disable=SC2090
487
510
_files=$( ssh -o ' Batchmode yes' " $_userhost " \
488
511
command ls -aF1dL " $_path *" 2> /dev/null |
489
- command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /\\& /g' -e ' /[^\/]$/d' )
512
+ command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /' " $_escape_replacement " ' /g' -e ' /[^\/]$/d' )
490
513
else
491
514
# escape problematic characters; remove executables, aliases, pipes
492
515
# and sockets; add space at end of file names
493
516
# shellcheck disable=SC2090
494
517
_files=$( ssh -o ' Batchmode yes' " $_userhost " \
495
518
command ls -aF1dL " $_path *" 2> /dev/null |
496
- command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /\\& /g' -e ' s/[*@|=]$//g' \
519
+ command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /' " $_escape_replacement " ' /g' -e ' s/[*@|=]$//g' \
497
520
-e ' s/[^\/]$/& /g' )
498
521
fi
499
522
_comp_compgen_split -l -- " $_files "
0 commit comments