How to debug and fix slow autocomplete in bash?
I don't know about fixing — there are all kinds of things that could go cause delays. But I can offer a few tips to investigate.
Just as a guess, maybe there's a directory somewhere in a search path ($PATH
, or some place where bash looks for completion data) that's on a filesystem which is slow to respond. Usually it's remote filesystems that are slow, but it could also be a failing hard disk, a hung FUSE driver, etc.
The first step to investigate is to run set -x
to get a trace of the commands that the shell executes to generate the completions. Watch where it pauses.
If that doesn't give enough information, bring in the big guns. Note the shell's process ID (echo $$
). In another terminal, run strace -f -s9999 -p$$
(or the equivalent of strace if running on another unix flavor). Strace lists the system calls performed by the process. See if it seems to be accessing files that it shouldn't, or if access to some files is slow. Adding the option -T
to the strace
command line makes it show the time spent in each system call.
If your *nix box is setup as an LDAP client you may have this problem, even logged in as a local user.
Boring debug info: Debugging with set-x
, I found the completion that was hanging at:
> set -x
> ls foo<tab>
... <--- lots of output removed
...
+ _quote_readline_by_ref foo quoted
+ '[' -z foo ']'
+ [[ foo == \'* ]] <--- froze here
+ [[ foo == ~* ]] <--- actually causing the trouble
Confirm: I confirmed this with ls ~*
which also hung. It turns out my ldap server was sluggish, but this shouldn't affect things like bash completion and ls!
Solution: Aha, there is a bug filed against bash-completion + ldap, it will be fixed in a newer version, and a simple patch if you don't want to wait. Tab completion is fast again, hooray!
Here's the patchfile in case the link goes away. It's merely escaping the ~ on lines 545 and 547:
--- /usr/share/bash-completion/bash_completion.orig 2014-11-06 10:36:14.981888369 +0100
+++ /usr/share/bash-completion/bash_completion 2014-11-06 10:36:25.142070963 +0100
@@ -542,9 +542,9 @@
elif [[ $1 == \'* ]]; then
# Leave out first character
printf -v $2 %s "${1:1}"
- elif [[ $1 == ~* ]]; then
+ elif [[ $1 == \~* ]]; then
# avoid escaping first ~
- printf -v $2 ~%q "${1:1}"
+ printf -v $2 \~%q "${1:1}"
else
printf -v $2 %q "$1"
fi
You need to exit current ssh session and re-login in order for this patch to take effect.
I recommend checking your system audio is properly working. The terminal bell sound not being able to play correctly might be the cause for the temporary freeze. For example: PulseAudio Daemon crashed / works incorrectly.
In my terminal when I first press the tab key, it became stuck for a seconds. After that it was fine. It also occurred if all text in terminal erased and the backspace key was pressed.
I tried all the above methods but they didn't work out.
Turn off Terminal Bell
Ctrl+Alt+T --> Edit --> Preference --> {User profile, usually unnamed} --> Text --> Sound --> Uncheck Terminal bell