bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: path completion with cd <tab> - similar to tcsh


From: Chet Ramey
Subject: Re: path completion with cd <tab> - similar to tcsh
Date: Tue, 19 Apr 2011 09:39:21 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9

On 4/19/11 2:06 AM, Peter Toft wrote:
> On Mon, 18 Apr 2011 20:12:24 -0400, Chet Ramey wrote:
>> On 4/14/11 6:19 PM, Peter Toft wrote:
>>
>>> I have an annoying bash-problem on Red Hat Linux 5.x. If I e.g. try to move
>>> to a subdirectory of another directory (e.g. $HOME), where the tab-expand
>>> works poorly;
>>>
>>> Assume $HOME=/home/pto
>>
>> You should see whether or not you have a completion already defined by
>> running `complete -p cd'.  It would also help to know the version of bash
>> you're using.  That will help establish a baseline.  (And RHL 5.x?  That's
>> pretty old.)
> 
> Right! bash --version gives -> 3.2.25(1)-release (x86_64-redhat-linux-gnu)
> 
> RH5.6 is not cutting edge (but I need it for tools-reasons), but I also
> dislike
> the current working method on e.g. Ubuntu 10.10, so I really like to get
> into the dirt on this one :)

OK, here's the bottom line.  By default, with no completion specifications
installed for a particular command, bash will recognize a leading `$' and
attempt completion of shell variable names.  Only the name is completed:
bash does not expand the variable to check whether or not the value is a
directory name and does not treat it as a filename.

You can write a shell function and assign it as the completion spec for cd,
maybe augmenting the one that is distributed as part of the bash_completion
package I suspect you have installed on Ubuntu.  That function already does
a pretty good job of handling all the possible cases.

That part of the function would look something like (totally untested and
off the cuff):

case "$cur" in
\$*)    dirs=( $(compgen -v ${cur:1}) ) # complete to all variable names
        for d in "address@hidden" ; do
                if eval test -d \"\$${d}\"; then
                        compopt +o filenames    # suppress quoting
                        COMPREPLY+=(\$"${d}"/)
                fi
        done

        ;;
*)      ;;      # whatever else
        
esac


>>> "cd $HOME<TAB>" is expanded to "cd /home/pto " (without the quotes).
>>> I get $HOME expanded - quite ok - but I get an annoying space efter the
>>> path.

I can't reproduce this on bash-3.2.39.  I get the same sort of variable
name completion unless I add the trailing slash manually.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]