@@ -431,11 +431,29 @@ _sftp()
431
431
# shellcheck disable=SC2089
432
432
_comp_cmd_scp__path_esc=' [][(){}<>"' " '" ' ,:;^&!$=?`\\|[:space:]]'
433
433
434
- # Complete remote files with ssh. If the first arg is -d, complete on dirs
435
- # only. Returns paths escaped with three backslashes.
434
+ # Complete remote files with ssh. Returns paths escaped with three backslashes
435
+ # (unless -l option is provided).
436
+ # Options:
437
+ # -d Complete on dirs only.
438
+ # -l Return paths escaped with one backslash instead of three.
436
439
# shellcheck disable=SC2120
437
440
_comp_xfunc_ssh_scp_remote_files ()
438
441
{
442
+ local dirs_only=false
443
+ local less_escaping=false
444
+
445
+ local flag OPTIND=1 OPTARG=" " OPTERR=0
446
+ while getopts " dl" flag " $@ " ; do
447
+ case $flag in
448
+ d) dirs_only=true ;;
449
+ l) less_escaping=true ;;
450
+ * )
451
+ echo " bash_completion: $FUNCNAME : usage error: $* " >&2
452
+ return 1
453
+ ;;
454
+ esac
455
+ done
456
+
439
457
# remove backslash escape from the first colon
440
458
cur=${cur/ \\ :/: }
441
459
@@ -451,20 +469,25 @@ _comp_xfunc_ssh_scp_remote_files()
451
469
path=$( ssh -o ' Batchmode yes' " $userhost " pwd 2> /dev/null)
452
470
fi
453
471
472
+ local escape_replacement=' \\\\\\&'
473
+ if " $less_escaping " ; then
474
+ escape_replacement=' \\&'
475
+ fi
476
+
454
477
local files
455
- if [[ ${1-} == -d ]] ; then
478
+ if " $dirs_only " ; then
456
479
# escape problematic characters; remove non-dirs
457
480
# shellcheck disable=SC2090
458
481
files=$( ssh -o ' Batchmode yes' " $userhost " \
459
482
command ls -aF1dL " $path *" 2> /dev/null |
460
- command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /\\& /g' -e ' /[^\/]$/d' )
483
+ command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /' " $escape_replacement " ' /g' -e ' /[^\/]$/d' )
461
484
else
462
485
# escape problematic characters; remove executables, aliases, pipes
463
486
# and sockets; add space at end of file names
464
487
# shellcheck disable=SC2090
465
488
files=$( ssh -o ' Batchmode yes' " $userhost " \
466
489
command ls -aF1dL " $path *" 2> /dev/null |
467
- command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /\\& /g' -e ' s/[*@|=]$//g' \
490
+ command sed -e ' s/' " $_comp_cmd_scp__path_esc " ' /' " $escape_replacement " ' /g' -e ' s/[*@|=]$//g' \
468
491
-e ' s/[^\/]$/& /g' )
469
492
fi
470
493
_comp_split -la COMPREPLY " $files "
0 commit comments