Linux/Unix Tab Autocomplete with Sudo Command
Previously I was too lazy to look up why is that when invoke “sudo” command in unix/linux server, auto complete in Linux/Unix terminal with sudo command is not working. To explain further, the auto complete command such as when type;
apac + tab will return apachectl
launchc + tab will return launchctl
ipta + tab will return iptables
so on and so forth. So, when I invoke sudo before the command, the auto complete would not work, i.e sudo apac + tab will not give me an auto complete of sudo apachectl.
I thought, it _should be_ not working, or it meant not to work. Then, I also wonder, why when i type command below it works;
sudo gi + tab return sudo git
sudo yu +tab return sudo yum
Then it got me to search and find out further. I found something called bash_completion. In Linux Centos there is bash_completion config file for yum and git in /etc/bash_completion.d/
The config files for git, subversion and yum magically made auto complete work with precede sudo command. So, how to make it work which command inside /bin/ /sbin/ /usr/bin/ and etc? There is a package name bash-completion will do the magic for you.
What’s next? Obviously it’s yum install bash-completion 😉 After the installation, there is a file in /etc/bash_completion which will do more “tab auto-complete” include when precede sudo command.
At the same time, if installing a bash-completion is not your kind of thingy, can put these lines into your bashrc/bash_profile/profile
if [ "$PS1" ]; then complete -cf sudo fi
I got lines above work on my Mac. Have fun.