Human friendly bash auto-completion with sudo
Asked Answered
L

2

9

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...

Lengthways answered 6/8, 2017 at 13:3 Comment(1)
Are you sure you have removed complete -cf sudo in your .bashrc and your .bash_profile (this was my problem)? And are you sure you've sourced the completion file, i.e. . /usr/share/bash-completion/bash_completion in your .bashrc?Contaminate
P
12

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

Procurance answered 6/12, 2018 at 16:28 Comment(1)
I couldn't figure out what file bash was sourcing that was calling complete -cf sudo so I used bash -x to see what files get sourced. I have seen several forums/comments/threads that say that you want the above line, but if bash-completion is installed correctly, this is definitely not what you want. This should be marked as the correct answer.Honduras
M
3

use double tab:

sudo rfkill <TAB><TAB>

UPD

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

complete -cf sudo
Maulmain answered 6/8, 2017 at 18:40 Comment(2)
I mean I do use double tab, I'm going to edit my question, thanksLengthways
It doesn't work (actually I already had this line in .bashrc), I'll try to replace /usr/share/bash-completion/completions/sudo file tomorrow, have no time now.Lengthways

© 2022 - 2024 — McMap. All rights reserved.