Bash shell tab completion, don't expand the ~

12

3

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

Johan

Posted 2010-01-14T06:16:22.563

Reputation: 4 827

You might have a look at shopt -p direxpand. – gam3 – 2018-10-25T07:12:14.487

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). – Paused until further notice. – 2010-01-14T09:45:08.493

"bind -v | grep tilde" returns "set expand-tilde off" ... so I don't think it will help. – Johan – 2010-01-14T12:30:14.397

Answers

10

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

Posted 2010-01-14T06:16:22.563

Reputation: 149 037

Or just: _expand() { :; }. – kenorb – 2015-08-21T10:50:28.867

though _expand(){ true; } is shorter :) – tig – 2010-12-23T18:21:18.113

would it not be _expand(){ false; }? @tig – John T – 2010-12-23T20:07:41.147

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 – 2010-12-24T07:40:55.180

@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 – 2010-12-25T00:48:31.940

@John: that is ok :), «Even John T can be wrong» (don't be offended :) ) – tig – 2010-12-27T08:56:07.247

@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 – 2010-12-27T18:53:58.420

5

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

__expand_tilde_by_ref() {
  return 0
}

mjmt

Posted 2010-01-14T06:16:22.563

Reputation: 151

1

A more precise customization would be

_filedir_xspec () { :; }

Alexander Shcheblikin

Posted 2010-01-14T06:16:22.563

Reputation: 620

1

Even more compactly:

_expand() { :; }

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

Joe

Posted 2010-01-14T06:16:22.563

Reputation: 19