Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human friendly bash auto-completion with sudo

My system is Manjaro Linux based on Arch Linux, I use bash and bash-completion.

It works perfectly when I type something as regular user (no sudo)

$ rfkill <TAB><TAB>
block    event    help     list     unblock 

but when I type it with sudo

$ sudo rfkill <TAB><TAB>
Display all 3811 possibilities? (y or n)

Obviously, it tries to complete sudo command but I want it to complete rfkill.

I know I can change this behavior by editing /usr/share/bash-completion/completions/sudo file, but I have no idea how to say if second word is not a flag for sudo then use completion for next word.

Do you have?

UPD: I'm testing Ubuntu 16.04 in virtual machine and I see it works as expected. I'll check the difference between ubuntu's /usr/share/bash-completion/completions/sudo file and mine, if any.

UPD2: There is some mirror (meaningless) difference between these files, anyway that didn't help. I have more ideas to test...

like image 586
Yet Another John Smith Avatar asked Aug 06 '17 13:08

Yet Another John Smith


People also ask

How do I autocomplete in Linux terminal?

When at the MS-DOS, Windows command line or a Linux or Unix shell, typing in long file names or directories can become a burden. Use the Tab to autocomplete the names of directories and files while in the command line.

How do you make an autocomplete case insensitive in bash?

Making bash autocomplete case insensitive is a small and easy way to make your Linux terminal experience much smoother. For reverting the changes, you can simply edit the inputrc file and delete the set completion-ignore-case On line.

How do I know if bash completion is installed?

If the autocomplete results contain directories only (no files), then Bash Completion is installed. If the autocomplete results include files, then Bash Completion is not installed.


2 Answers

I had exactly the same problem (running Manjaro) and found a solution in the Manjaro Forum (Source):

  1. Make sure bash-completion is actually installed by checking whether /usr/share/bash-completion/bash_completion exists. If not install it with pacman -S bash-completion
  2. In your ~/.bashrc file make sure that complete -cf sudo is commented out. Otherwise, this will make sudo only auto-complete filenames and commands but not use bash-completion.

I hope this helps you solving the problem

like image 119
bentocin Avatar answered Oct 13 '22 22:10

bentocin


use double tab:

sudo rfkill <TAB><TAB>

UPD

if there is not that line, add this to your .bashrc

complete -cf sudo
like image 31
tso Avatar answered Oct 14 '22 00:10

tso