LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to stop bash from replacing "~/" with "/home/username"? (http://www.linuxquestions.org/questions/linux-software-2/how-to-stop-bash-from-replacing-%7E-with-home-username-643162/)

daihard 05-18-2008 11:56 PM

How to stop bash from replacing "~/" with "/home/username"?
 
Hi.

I am wondering how I can do this. In Red Hat / Fedora, hitting the tab key to complete directory/file names does NOT replace the "~" character with the actual home directory path. For instance, let's say my $HOME is "/home/dtoyama" and I have the directory named "/home/dtoyama/documents". Here's what I do.

Code:

[daihard-exp:/home/dtoyama]$ cp test.txt ~/doc
Here if I hit the tab key, it will expand to this.
Code:

[daihard-exp:/home/dtoyama]$ cp test.txt ~/documents
That's exactly what I expect to happen.

Now, however, Kubuntu does not exhibit the same behaviour. Instead of simply completing the directory, it will replace "~" with /home/dtoyama.

Code:

[daihard-exp:/home/dtoyama]$ cp test.txt ~/doc
Hitting the tab key will result in this.
Code:

[daihard-exp:/home/dtoyama]$ cp test.txt /home/dtoyama/documents
I do NOT want that to happen. I did some googling and found that I needed to modify /etc/bash_completion. I haven't gotten much farther, though. The file is huge and has lots of stuff in it. I could choose to not load the file at login, but then I may not load something that I should have. I'd like to be able to just disable this one annoying feature without affecting the others defined in /etc/bash_completion. Would anyone be kind enough to help me?

TIA,
Dai

daihard 05-19-2008 12:15 AM

Okay, I am replying to myself. This behaviour that I want to disable is apparently called "tilde expansion." I looked at /etc/bash_completion a bit more closely and found the following function.
Code:

# This function expands tildes in pathnames
#
_expand()
{
    # FIXME: Why was this here?
    # [ "$cur" != "${cur%\\}" ] && cur="$cur\\"

    # expand ~username type directory specifications
    if [[ "$cur" == \~*/* ]]; then
        eval cur=$cur
    elif [[ "$cur" == \~* ]]; then
        cur=${cur#\~}
        COMPREPLY=( $( compgen -P '~' -u $cur ) )
        return ${#COMPREPLY[@]}
    fi
}

I commented out everything inside the if block and replaced it with a "return 0" (because leaving the entire function empty inside caused an error), and restarted KDE. The behaviour is gone!

Now, my question is, is this the right way of going about it? This is more like a hack to me; if there's a more legitimate way, I'd rather choose that over manually editing /etc/bash_completion. Any thoughts?

vadkutya 05-19-2008 03:51 AM

hey daihard,

to alter the behaviour of the shell edit either /etc/inputrc or ~/.inputrc. try the following. type:
Code:

bind -v
you should find a line like
Code:

set expand-tilde on
if so edit one of the two files above. for example write the following line in your ~/.inputrc:
Code:

set expand-tilde off
that's it. next time the bash reads .inputrc autoexpansion of the '~' will be disabled.

vadkutya

daihard 05-21-2008 10:17 PM

Quote:

Originally Posted by vadkutya (Post 3157673)
hey daihard,

to alter the behaviour of the shell edit either /etc/inputrc or ~/.inputrc. try the following. type:
Code:

bind -v
you should find a line like
Code:

set expand-tilde on
if so edit one of the two files above. for example write the following line in your ~/.inputrc:
Code:

set expand-tilde off
that's it. next time the bash reads .inputrc autoexpansion of the '~' will be disabled.

Hi Vadkutya.

Thanks for the advice. Strangely, bind -v shows that "expand-tilde" is already off on my computer.
Code:

[daihard-x2:/home/dtoyama]$ bind -v | grep tilde
set expand-tilde off

I created ~/.inputrc and added "set expand-tilde off" in it, but that did not help.

Is there anything else I need to pay attention to? Again, the OS is Kubuntu 8.04.

nowshining 05-22-2008 04:11 AM

try a re-login to bash - see my site ubuntu/kubuntu gatherings in my sig to learn how to do this.

vadkutya 05-22-2008 07:02 AM

hey daihard,

did you log off and log in again, after you created ~/.inputrc? if so, the problem might have to do with KDE. are you using xterm? how does tilde expansion work in xterm. same or different? i think KDE has it's own terminal. maybe it has it's own config or is per default started explicitly with tilde-expansion so that it overwrites the default values. i have no clue about KDE and it's terminal. however this shouldn't happen. if you turn off tilde expansion in your ~/.inputrc than it should not be allowed to overwrite this value by another program. this makes debugging extremly complicate. if KDE is really doing this than this is another reason why NOT to use desktop environments. however, i am aware that this is not the problem here :). look under "settings" or "help". if you can't find anything apropriate you can try to ask the konsole guys per mailing list.

good luck, vadkutya

p.s.: a friend of mine uses kubuntu as well. bind -v gave tilde expansion set to off. however, the tilde was expanded in konsole. this really seems to be a KDE issue and must be solved by tuning the configuration of konsole via the gui or whatever other way KDE supports.

p.p.s.: get rid of it! this is a really bad behaviour from KDE. it kreeps into your system like a virus. the config's that are meant to control such behaviour are disabled by KDE! wtf! really, get rid of it...

[edit: forget what i said. xterm does tilde expansion as well. it doesn't seem to be a prob with KDE. i grep'ed through /etc/inputrc on kubuntu and there was no set tilde-expansion off. although bind -v has shown so. right now i'm giong to play football but i'm coming back to you asap.]

daihard 05-22-2008 11:09 PM

Hi!

Problem solved. The issue was the inclusion of /etc/bash_completion at the end of ~/.bashrc. The /etc/bash_completion file forces tilde expansion, regardless of the value of "set expand-tilde." This is the last few lines of my ~/.bashrc, which has been like this since day one.
Code:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

Inside the file is the function called _expand(), which forces tilde expansion. In order to disable it, I needed to make the following modification to my ~/.bashrc.
Code:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

## my addition
_expand()
{
    return 0;
}

This effectively nullifies the original _expand() function.

I truly appreciate all your help and suggestions, in any case. Hope you can sleep peacefully now. :)

Mike.lifeguard 10-09-2009 03:21 PM

Quote:

Originally Posted by daihard (Post 3162026)
Hi!
Code:

## my addition
_expand()
{
    return 0;
}

This effectively nullifies the original _expand() function.

I truly appreciate all your help and suggestions, in any case. Hope you can sleep peacefully now. :)

Has anyone thought about simply fixing /etc/bash_completion such that it checks if expand-tilde is off before clobbering your settings and expanding whether you like it or not?

GeneC 08-22-2010 02:32 PM

Quote:

Originally Posted by Mike.lifeguard (Post 3713875)
Has anyone thought about simply fixing /etc/bash_completion such that it checks if expand-tilde is off before clobbering your settings and expanding whether you like it or not?

Done. https://bugs.launchpad.net/ubuntu/+s...on/+bug/622403

As I said in the bug report, I only know that it worked for me, it's probably not the most effective way and may need testing in scripts and what not.


All times are GMT -5. The time now is 10:23 AM.