bash-completion icon indicating copy to clipboard operation
bash-completion copied to clipboard

cd with double quote can't auto-complete folder that has single quote in its name

Open Mark-Joy opened this issue 4 years ago • 0 comments

Describe the bug

Suppose we have below folder structure

$ tree -F "a'bbb"
a'bbb
├── c/
└── d.txt

1 directory, 1 file

Do exactly: cd "a'b<Tab>

Result: Nothing happens. In other words, auto-completion failed.

To reproduce

As above steps.

Expected behavior

cd "a'b<Tab> should give: cd "a'bbb"/

Versions ()

  • [x] Operating system name/distribution and version: Android Pie/Termux
  • [x] bash version, echo "$BASH_VERSION": 5.1.12(1)-release
  • [x] bash-completion version, (IFS=.; echo "${BASH_COMPLETION_VERSINFO[*]}"): 2.11.0

Additional context

The issue is resolved if function _filedir in bash_completion is modified as below:

_filedir()
     local -a toks
     local reset arg=${1-}

+    if [[ $cur == \'* || $cur == \"* ]]; then
+        # Leave out first character
+        cur="${cur:1}"
+    fi
+
     if [[ $arg == -d ]]; then
         reset=$(shopt -po noglob)
         set -o noglob
         toks=($(compgen -d -- "${cur-}"))
         IFS=' '
         $reset
         IFS=$'\n'
     else
-        local quoted
-        _quote_readline_by_ref "${cur-}" quoted
+        local quoted="${cur-}"


         # Munge xspec to contain uppercase version too
         # https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html

_quote_readline_by_ref seems to be not needed. According to this function's comments:

#     $ ls "a'b/"
#     c
#     $ compgen -f "a'b/"       # Wrong, doesn't return output
#     $ compgen -f "a\'b/"      # Good
#     a\'b/c

But as I tried (with folder a'b created), BOTH compgen -f "a'b/" and compgen -f "a\'b/" gave nothing

But if we do the below steps, compgen will generate result:

$ compgen -f "a'bbb/"		# 1
$ compgen -f "a\'bbb/"		# 2
$ ls "a'b<Tab>			# 3 Will give:  ls "a'bbb"/
$ ls "a'bbb"/Enter>		# 4 <Enter>
c  d.txt
$ compgen -f "a'bbb/"		# 5
a'bbb/c
a'bbb/d.txt
$ compgen -f "a\'bbb/"		# 6
$ ls a\'b<Tab>			# 7 Will give:  ls a\'bbb/
$ ls a\'bbb/<Enter>		# 8 <Enter>
c  d.txt
$ compgen -f "a\'bbb/"		# 9
a'bbb/c
a'bbb/d.txt

After step3-4 compgen -f "a'bbb/" will generate result. After step7-8 compgen -f "a\'bbb/" will generate result.

It seems that compgen is also based on auto-completion cache.

Debug trace

cd.log compgen.log

Mark-Joy avatar Dec 06 '21 07:12 Mark-Joy