5

I am using CetnOS 6 and want to have autocompletion for parameters while using sudo.

I found this question https://superuser.com/questions/804128/bash-hints-does-not-work-after-sudo-or-man and now the following works:

  • yu[tab] completes to yum and with another tab press it suggests the different yum commands
  • yum upg[tab] autocompletes to yum upgrade as desired
  • sudo yu[tab] autocompletes to sudo yum as expected
  • sudo yum upg[tab] does not autocomplete to anything, also no suggestions.

How can I achieve parameter completion even when using sudo?

(I used yum just as an example because it is available on all CentOS; the result is the same for other commands.)


Some informations about my system:

$ uname -a
Linux server 2.6.32-642.6.1.el6.x86_64 #1 SMP Wed Oct 5 00:36:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

$ yum list installed | grep completion
bash-completion.noarch  1:1.3-7.el6        @epel

$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
complete -cf sudo
4
  • Bash completion does not have sudo available. Look at /etc/bash_completion.d.
    – Sokel
    Commented Nov 8, 2016 at 21:18
  • I don't understand. There is no file called sudo in /etc/bash_completion.d - yes. But on my ubuntu/debian systems there is also no such file in there but there parameter completion works. sudo apt-get di[tab] completes to sudo apt-get dist-upgrade as desired
    – masgo
    Commented Nov 9, 2016 at 9:45
  • 1
    Ubuntu provides its own bash completion scripts/rules than other distributions. You will need to look at the files the bash-completion package in ubuntu provides, find the one for sudo and go from there. it may not be named as such, but you can use grep to find it.
    – Sokel
    Commented Nov 11, 2016 at 17:40
  • 1
    but where should I search? I did a grep for sudo on everything in /etc/bash_completion.d and found nothing. It must be somewhere else
    – masgo
    Commented Nov 20, 2016 at 20:15

1 Answer 1

1

Not the perfect workaround, but you can add complete -cf sudo to your .bashrc which will autocomplete sudo commands thereon.

However, in doing so, it will not act exactly like normal tab completion. for example, if you were to use this, and then sudo cd [tab] you will get a lot of possibilities that won't be useful - i.e a file names will show... who wants to cd to a file?

For the most part though, it will complete commands like sudo yum upd[tab]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .