Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected bash autocompletion behavior within a symbolic link loop

I have the following directory structure:

base/
  dir/
    subdir/
    link -> ../dir

Now if I cd to dir/link and type:

cd ../subd[tab]

I get:

cd ../subdir[space] 
  1. I would understand if autocomplete fails (because it would canonize the path and look into base/ and not dir/).

  2. I would also understand if it autocompletes to cd ../subdir/ with the ending / (because it would interpret .. as go up one level and search into dir/).

But I do not understand the actual behaviour that is somewhere between the two. Ideally I would like bash to behave like 2. (autocomplete to cd ../subdir/). I am using fedora 14, bash version 4.1.7(1). Any idea how to accomplish this ?

like image 336
mbonnin Avatar asked Nov 13 '22 07:11

mbonnin


1 Answers

UPDATE: The program with which you can customize auto-completion is called complete.

You can find some good basic examples here: More on Using the Bash Complete Command

Using function and script names as per the above link, here is a script which appends the / to a symbolic link to a directory... It is just a rough sample, but it shows it can be done (I haven't tried it with the cd builtin...

Associate the function _mycomplete_ with executable myfoo

complete -F _mycomplete_ myfoo

The function to go in ~/.bashrc

function _mycomplete_()
{
    local cmd="${1##*/}"
    local word=${COMP_WORDS[COMP_CWORD]}
    local line=${COMP_LINE}
    local xpat='!*.foo'

    COMPREPLY=($(compgen -f -X "$xpat" -- "${word}"))
    if ((${#COMPREPLY[@]}==1)) ;then
       [[ -h $COMPREPLY ]] && COMPREPLY="$COMPREPLY/"
    fi
}

Original answer:

At the command-line, the main indicator of a auto-expansion to a symbolic link is shown on the last line of the following table, ie. a name expands but without the final /.

 on pressing TAB                                         on pressing TAB (again)  
  what happens?              meaning                        what happens?
===================      =======================     ==================================== 
Nothing is appended  1=> Multiple sub-dirs exist  => A list of possibilities is presented
                     2=> No sub-directory exists  => Nothing is appended (again)

Expands to end in /   => A uniquely matching dir  => ...as per first column (repeat)
Expands text only     => Current name is a link   => Expands to end in /

In your example, if you have already primed the command-line to the full name, ie. cd link then the indicator is not obvious. Also you won't know it is a symbolic link via the list of possibilities.

To be able to cd to the link's target, you can use cd -P link, or set -P; cd link

like image 133
Peter.O Avatar answered Dec 25 '22 13:12

Peter.O