Skip to content

Instantly share code, notes, and snippets.

@fl0at
Last active December 10, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fl0at/4504977 to your computer and use it in GitHub Desktop.
Save fl0at/4504977 to your computer and use it in GitHub Desktop.
Bash-completion has errors on Debian Squeeze, and while they've been fixed, it'll be forever until we get it officially backported.

Tilde expansion

Bash-completion has errors on Debian Squeeze, and while they've been fixed, it'll be forever until we get it officially backported. (See http://ask.debian.net/questions/why-did-bash-compleation-expand_tilde-stoped-working-in-squeeze)

I have taken the liberty of using the commit where it was fixed, and making a simple patchfile. (See http://anonscm.debian.org/gitweb/?p=bash-completion/bash-completion.git;a=commit;h=ccbf141e13eaa98d6a8721ae015a5504654424cb)

Save this gist to a file, and apply using the following:

cd /etc; patch < your_filename

You're done! You'll have to re-source /etc/bash_completion for the change to affect your shell. Or, just log off and back on.

--- bash_completion 2010-11-16 00:08:47.000000000 -0800
+++ bash_completion 2013-01-10 11:13:40.461543043 -0800
@@ -602,7 +602,7 @@
{
local i IFS=$'\t\n' xspec
- __expand_tilde_by_ref cur
+ _tilde "$cur" || return 0
local -a toks
local quoted tmp
@@ -805,7 +805,26 @@
}
+# Perform tilde (~) completion
+# @return True (0) if completion needs further processing,
+# False (> 0) if tilde is followed by a valid username, completions
+# are put in COMPREPLY and no further processing is necessary.
+_tilde() {
+ local result=0
+ # Does $1 start with tilde (~) and doesn't contain slash (/)?
+ if [[ ${1:0:1} == "~" && $1 == ${1//\/} ]]; then
+ # Try generate username completions
+ COMPREPLY=( $( compgen -P '~' -u "${1#\~}" ) )
+ result=${#COMPREPLY[@]}
+ fi
+ return $result
+}
+
+
# Expand variable starting with tilde (~)
+# We want to expand ~foo/... to /home/foo/... to avoid problems when
+# word-to-complete starting with a tilde is fed to commands and ending up
+# quoted instead of expanded.
# Only the first portion of the variable from the tilde up to the first slash
# (~../) is expanded. The remainder of the variable, containing for example
# a dollar sign variable ($) or asterisk (*) is not expanded.
@@ -834,7 +853,7 @@
# becomes "~a". Double quotes allow eval.
# 2: Remove * before the first slash (/), i.e. "~a/b"
# becomes "b". Single quotes prevent eval.
- # +-----1----+ +---2----+
+ # +-----1----+ +---2----+
eval $1="${!1/%\/*}"/'${!1#*/}'
else
# No, $1 doesn't contain slash
@fl0at
Copy link
Author

fl0at commented May 6, 2013

Wheezy fixes this issue. And there was much rejoicing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment