I use the Tab key a lot when I use the shell (bash).

But I'm getting annoyed that ~ always gets expanded to /home/"user". I don't think it's always been like this; is there any way to stop this behaviour?

An example:

  1. cj@zap:~$ ls ~/
  2. Press Tab
  3. cj@zap:~$ ls /home/cj/

I would like to continue to have ~/ and not end up with /home/cj/.

  • 2
    "I don't think it always has been like this." - Programmable completion overrides the readline setting set expand-tilde off (default or set in ~/.inputrc). Jan 14 '10 at 9:45
  • "bind -v | grep tilde" returns "set expand-tilde off" ... so I don't think it will help.
    – Johan
    Jan 14 '10 at 12:30
  • You might have a look at shopt -p direxpand.
    – gam3
    Oct 25 '18 at 7:12

Disabling tilde expansion is quick and painless. Open up ~/.bashrc and insert this:

    return 0;

This will override the expand function from /etc/bash_completion. I'd recommend commenting on what it does above the function in case you want the expansion back in the future. Changes will take effect in a new instance.

  • though _expand(){ true; } is shorter :)
    – tig
    Dec 23 '10 at 18:21
  • would it not be _expand(){ false; }? @tig
    – John T
    Dec 23 '10 at 20:07
  • 2
    @John: no it should be true. true returns successful result and successful result is 0, so return 0 is equal to true in exit status, and return 1 is equal to false. just try true; echo $? and false; echo $?.
    – tig
    Dec 24 '10 at 7:40
  • @tig too much programming has confused me... codepad.org/Frb3RyAN Similarly, you find this in lots of code (see top): cs.nthu.edu.tw/~tingting/DS_mid_solution.pdf I would assume it's switched up in the GNU tools to indicate a more realistic meaning, i.e. "True, the program ran successfully" or "false -- the program ran incorrectly".
    – John T
    Dec 25 '10 at 0:48
  • @John: that is ok :), «Even John T can be wrong» (don't be offended :) )
    – tig
    Dec 27 '10 at 8:56

With newer bash_completion it seems you also need to override __expand_tilde_by_ref:

__expand_tilde_by_ref() {
  return 0

Even more compactly:

_expand() { :; }

...as ":" is a shell built-in equivalent to "true" :-)


A more precise customization would be

_filedir_xspec () { :; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.