28

I have just came across something that is really bothering me. Autocompletion on ubuntu works like a charm but when I login as root, e.g. su I get tab autocompletion only sometimes.

Why does it work like that? Can one change that?

Braiam
  • 66,947
  • 30
  • 177
  • 264
Patryk
  • 9,026
  • 27
  • 70
  • 108
  • Are you actually logging in as root, or using sudo su? – Marty Fried Feb 12 '12 at 19:08
  • @MartyFried Yes I am diong `sudo su`. What is the difference? – Patryk Feb 12 '12 at 19:19
  • It seems to work for me, can you provide us with a specific instance of when it does not work, something that we can replicate. – ste_kwr Feb 12 '12 at 20:20
  • 1
    @lordmonkey: Maybe none, I'm not really sure. I think su or sudo su by itself is a little different in some way that I don't remember, so I wanted to make sure that I could check this under the same conditions. It seems like it works, but you said "sometimes", so I can only suggest you double-check and try to make it reproducible, or just make sure there's not something else happening that is fooling you - ie, user error - hey, it happens to everyone! :) – Marty Fried Feb 12 '12 at 20:48

5 Answers5

51

Check your /root/.bashrc file for these lines

if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi

If they do not exist add them to the end of the file using your favorite text editor.

Please note, that the autocompletion will start working just after logging out and back again!

Frantique
  • 8,435
  • 35
  • 51
Bruno Pereira
  • 72,895
  • 33
  • 199
  • 223
  • Mine .bashrc contained the mentioned lines but they were commented out; uncommented the lines and sourced the new .bashrc ; things look pretty good now . – Ankit Jul 10 '12 at 02:30
  • The `bash_completion` line, at least in my case, were commented out, because it claims the `bash_completion` is already enabled in `/etc/bash.bashrc`, but that file has also that lines commented out. Instead of uncommenting the `etc` file, it's better to uncomment the line in `/root/.bashrc`. – ABu Feb 03 '17 at 20:30
8

You haven't mentioned which Ubuntu version you use.

  • Check if you have the bash-completion package installed:

    sudo apt-get install bash-completion
    
  • Mine works if I login using: sudo -i

Savvas Radevic
  • 7,693
  • 2
  • 36
  • 47
  • What's the point of removing root's .bashrc? – geirha Jun 27 '12 at 11:09
  • I think it will regenerate the default .bashrc upon next logout/login. Or perhaps it could contain some kind of "deactivation" for autocompletion (don't know if it's possible). I can't remember the actual reason I wrote it down though. – Savvas Radevic Jun 27 '12 at 13:23
  • 2
    No, a new `.bashrc` will not be created, and `/etc/bash_completion` will not be sourced, only bash's default completion will be available. – geirha Jun 27 '12 at 13:35
  • OK, edited and removed. Thank you for your input! Though I wonder if ~/.bashrc can "unsource" a file or if `disable-completion` is still available: http://superuser.com/questions/37148/how-to-disable-double-tab-to-show-available-commands-in-linux-console – Savvas Radevic Jun 28 '12 at 04:34
3

For Ubuntu 13.10, check /etc/bash.bashrc for

# enable bash completion in interactive shells
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

They might be commented. Un-commenting them works.

kiri
  • 27,676
  • 16
  • 81
  • 117
dbtek
  • 131
  • 2
0

Found that this worked (similar to the answers above but with a twist)

The file that needs to be edited (at least in my case with 12.04) was /root/.bashrc. This makes sense in my case, given that my regular user was autocompleting just fine but my root wasn't.

Anyway in said file I found all the necessary code but it was commented out:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    #. /etc/bash_completion
#fi

Just un-commented it:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

Saved, and it worked just fine.

Hope this helps someone.

neanderslob
  • 746
  • 2
  • 8
  • 24
0

In Linux Mint 18 (based on Xenial), you have to edit your bashrc :

sudo nano /etc/bash.bashrc

and comment out (remove the # in front of each line) this section :

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
     . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
     . /etc/bash_completion
  fi
fi

That’s all.