Ville Skyttä c506b6
diff --git a/bash_completion b/bash_completion
Ville Skyttä c506b6
index 53eea33..e1e926b 100644
Ville Skyttä c506b6
--- a/bash_completion
Ville Skyttä c506b6
+++ b/bash_completion
Ville Skyttä c506b6
@@ -618,7 +618,7 @@ _filedir()
Ville Skyttä c506b6
 {
Ville Skyttä c506b6
     local i IFS=$'\t\n' xspec
Ville Skyttä c506b6
 
Ville Skyttä c506b6
-    __expand_tilde_by_ref cur
Ville Skyttä c506b6
+    _tilde "$cur" || return 0
Ville Skyttä c506b6
 
Ville Skyttä c506b6
     local -a toks
Ville Skyttä c506b6
     local quoted tmp
Ville Skyttä c506b6
@@ -803,7 +803,26 @@ _available_interfaces()
Ville Skyttä c506b6
 }
Ville Skyttä c506b6
 
Ville Skyttä c506b6
 
Ville Skyttä c506b6
+# Perform tilde (~) completion
Ville Skyttä c506b6
+# @return  True (0) if completion needs further processing, 
Ville Skyttä c506b6
+#          False (> 0) if tilde is followed by a valid username, completions
Ville Skyttä c506b6
+#          are put in COMPREPLY and no further processing is necessary.
Ville Skyttä c506b6
+_tilde() {
Ville Skyttä c506b6
+    local result=0
Ville Skyttä c506b6
+    # Does $1 start with tilde (~) and doesn't contain slash (/)?
Ville Skyttä c506b6
+    if [[ ${1:0:1} == "~" && $1 == ${1//\/} ]]; then
Ville Skyttä c506b6
+        # Try generate username completions
Ville Skyttä c506b6
+        COMPREPLY=( $( compgen -P '~' -u "${1#\~}" ) )
Ville Skyttä c506b6
+        result=${#COMPREPLY[@]}
Ville Skyttä c506b6
+    fi
Ville Skyttä c506b6
+    return $result
Ville Skyttä c506b6
+}
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
 # Expand variable starting with tilde (~)
Ville Skyttä c506b6
+# We want to expand ~foo/... to /home/foo/... to avoid problems when
Ville Skyttä c506b6
+# word-to-complete starting with a tilde is fed to commands and ending up
Ville Skyttä c506b6
+# quoted instead of expanded.
Ville Skyttä c506b6
 # Only the first portion of the variable from the tilde up to the first slash
Ville Skyttä c506b6
 # (~../) is expanded.  The remainder of the variable, containing for example
Ville Skyttä c506b6
 # a dollar sign variable ($) or asterisk (*) is not expanded.
Ville Skyttä c506b6
diff --git a/test/lib/completions/ls.exp b/test/lib/completions/ls.exp
Ville Skyttä c506b6
index 171f6e1..fa47f85 100644
Ville Skyttä c506b6
--- a/test/lib/completions/ls.exp
Ville Skyttä c506b6
+++ b/test/lib/completions/ls.exp
Ville Skyttä c506b6
@@ -19,4 +19,15 @@ if {[assert_exec {ls --help} "" "" "unsupported"]} {
Ville Skyttä c506b6
 sync_after_int
Ville Skyttä c506b6
 
Ville Skyttä c506b6
 
Ville Skyttä c506b6
+set test "~part should complete to ~full"
Ville Skyttä c506b6
+assert_bash_exec {compgen -u} {} /@ users
Ville Skyttä c506b6
+find_unique_completion_pair $users part full
Ville Skyttä c506b6
+# If home directory exists, append slash "/", else space " "
Ville Skyttä c506b6
+set trail [expr {[llength [glob -nocomplain ~$full]] ? "/" : " "}]
Ville Skyttä c506b6
+assert_complete "~$full$trail" "ls ~$part" $test
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+sync_after_int
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
 teardown
Ville Skyttä c506b6
diff --git a/test/unit/_tilde.exp b/test/unit/_tilde.exp
Ville Skyttä c506b6
new file mode 100644
Ville Skyttä c506b6
index 0000000..54394cb
Ville Skyttä c506b6
--- /dev/null
Ville Skyttä c506b6
+++ b/test/unit/_tilde.exp
Ville Skyttä c506b6
@@ -0,0 +1,51 @@
Ville Skyttä c506b6
+# @param string $part  Reference to variable to hold partial unique username
Ville Skyttä c506b6
+# @param string $full  Reference to variable to hold full unique username
Ville Skyttä c506b6
+proc setup {part full} {
Ville Skyttä c506b6
+    upvar $part _part
Ville Skyttä c506b6
+    upvar $full _full
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+    assert_bash_exec {compgen -u} {} /@ users
Ville Skyttä c506b6
+    find_unique_completion_pair $users _part _full
Ville Skyttä c506b6
+    save_env
Ville Skyttä c506b6
+}
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+proc teardown {} {
Ville Skyttä c506b6
+    assert_env_unmodified {
Ville Skyttä c506b6
+        /COMPREPLY=/d
Ville Skyttä c506b6
+    }
Ville Skyttä c506b6
+}
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+setup part full
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+set test "function should run without errors"
Ville Skyttä c506b6
+assert_bash_exec {_tilde > /dev/null} $test
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+sync_after_int
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+set test "function should not pollute environment"
Ville Skyttä c506b6
+# NOTE: A possible environment pollution is detected by assert_env_modified() in teardown()
Ville Skyttä c506b6
+assert_bash_exec {foo() { local aa="~"; _tilde "$aa"; }; foo; unset foo} $test
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+sync_after_int
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+set test "~full should complete to ~full unmodified"
Ville Skyttä c506b6
+set cmd [format {_tilde "~%s"; printf "%%s" "${COMPREPLY[@]}"} $full]
Ville Skyttä c506b6
+assert_bash_list "~$full" $cmd $test
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+sync_after_int
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+set test "~part should complete to ~full"
Ville Skyttä c506b6
+set cmd [format {_tilde "~%s"; printf "%%s" "${COMPREPLY[@]}"} $part]
Ville Skyttä c506b6
+assert_bash_list "~$full" $cmd $test
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+
Ville Skyttä c506b6
+teardown