Sometimes on the server, if the disk IO load is heavy, the terminal is stuck by the tab completion. Ctrl-C|D|Z all don't quit.

Each time I have to login from another session, find the stuck pty and kill it.

Is there any way to prevent the tab completion hang?


First idea - disable file name completions, while the disk is high loaded. It is affect only filenames, other completions stay working. Because, I think they doesn't cause hang.

Create file .bash_completion in the home directory and put this code to there.


### We are needed redefine original _filedir function
### and add new functionality to it
# for this, output the original function code and add the word 'original'
# in the beginning of it - now we are have _filedir renamed to
# 'original_filedir'
eval "original$(declare -f _filedir)"

# Define our own _filedir function, which will check disk load
# and: 
# if load are low - call original function.
# if load are high - stop further execution. 
_filedir() {
    io_load=$(awk '/sda /{print $12}' /proc/diskstats)
    if ((io_load > io_load_limit)); then
        echo -n "completion disabled - a lot i/o"


Also, the same trick can be done with _completion-loader function. It sets up dynamic completion loading.

In the Ubuntu, the main completion code resides in the /usr/share/bash-completion/bash_completion file and others, customized for specific program, reside in the /usr/share/bash-completion/completions/ directory.

When bash starts, it reads /usr/share/bash-completion/bash_completion file, and then, if you are typing apt-get, for example, _completion-loader runs and loads apt-get completion rules from the /usr/share/bash-completion/completions/apt-get. It is also affect to I/O and can be cause of hanging.

I decided use /proc/diskstats for evaluating disk activity. You can use another way. Limit value was chosen randomly.

Second idea - setup limit for i/o usage, like this.

  • Thx! 1. Does the random-chosen io-load-limit mean its value is not important? 2. In my case, the hang only occurs in home directory, not in the subfolders. Is it possible to disable tab completion in home directory only? – Lee Jul 19 '17 at 13:08
  • 1
    @Lee 1. No, it is the limit for number of I/Os currently in progress. You are needed change this value accordingly your situation. If tab completion hang, when io_load is 11, then you assign the 10 (or less) to the io-load-limit. That is, it need be regulated depending on the tab completion behavior. I just picked 10, for testing. 2. Sure, change the if statement to something like this: if [[ "$io_load" > "$io_load_limit" && "$PWD" == "$HOME" ]]; then. It will be checking the I/O load and the current working directory. – MiniMax Jul 19 '17 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.