13

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/.

Gaff
  • 18,569
  • 15
  • 57
  • 68
Johan
  • 5,337
  • 3
  • 34
  • 55

4 Answers4

11

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

_expand()
{
    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.

John T
  • 163,373
  • 27
  • 341
  • 348
  • 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 07:40
  • @tig too much programming has confused me... http://codepad.org/Frb3RyAN Similarly, you find this in lots of code (see top): http://www.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 00:48
  • @John: that is ok :), «Even John T can be wrong» (don't be offended :) ) – tig Dec 27 '10 at 08:56
  • @tig I never use those GNU tools to be honest, I didn't know they were separate binaries instead of bash builtins LOL. I was thinking they must've been out of there mind to do it backwards... – John T Dec 27 '10 at 18:53
  • Or just: `_expand() { :; }`. – kenorb Aug 21 '15 at 10:50
7

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

__expand_tilde_by_ref() {
  return 0
}
mjmt
  • 171
  • 1
  • 1
1

A more precise customization would be

_filedir_xspec () { :; }
1

Even more compactly:

_expand() { :; }

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

Gaff
  • 18,569
  • 15
  • 57
  • 68
Joe
  • 19
  • 1