LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Tab completion with sudo (http://www.linuxquestions.org/questions/slackware-14/tab-completion-with-sudo-476309/)

extrasolar 08-22-2006 01:51 PM

Tab completion with sudo
 
How do I enable tab completion when using sudo like on Ubuntu? I have no .bashrc file or /etc/bash.bashrc

Cheers.

acid_kewpie 08-22-2006 01:57 PM

can you explain more clearly what you mean? tab completion is a standard default bash faeture so if that's your shell, you will have it enabled.

extrasolar 08-22-2006 02:16 PM

Sorry I meant for commands and applications. If I use sudo I have to type the full command and cannot use tab completion.

Quigi 08-22-2006 02:26 PM

When you hit tab in your shell, you get command completion and filename completion. For a contrived example, typing "lso<TAB>" at the beginning of the line will complete the command to "lsof", but "sudo lso<TAB>" tries to complete this argument as a file name, and often there is none starting with "lso".

My workaround is to complete first, then jump to the beginning (C-A) and add "sudo ". It helps to have the 3 directories {,/usr{,/local}}/sbin in your path as a normal user.

I know tcsh can be configured to very sophisticated context-sensitive completions; not sure about bash. That would be the fancy approach.

statguy 08-22-2006 02:28 PM

Do you mean that for example instead of typing

sudo /sbin/ifconfig

you would like to be able to type

sudo ifc[tab]

and have it expand to ifconfig?

I don't know the answer, just trying to help clarify the question. :)

Alien Bob 08-22-2006 02:37 PM

You need to install the bash-completion package which is not part of stock Slackware but it is available in the /extra directory. A link to the package for Slackware 10.2 is here: http://slackware.osuosl.org/slackwar...sh-completion/

Eric

osor 08-22-2006 02:44 PM

For so-called "smart" tab-completion, most distros already supply some good rules (usually something like /etc/bash_completion). To put the rules in your startup, you generally source the file in ~/.bashrc or ~/.bash_profile or ~/.profile.

If you don't have a file /etc/bash_completion, you can create your own rules with the compgen and complete commands.

For example, for sudo to complete with executables, you could do:
Code:

complete -c sudo
Or you could think of more elaborate methods.

acid_kewpie 08-22-2006 02:46 PM

Quote:

Originally Posted by osor
For so-called "smart" tab-completion, most distros already supply some good rules (usually something like /etc/bash_completion). To put the rules in your startup, you generally source the file in ~/.bashrc or ~/.bash_profile or ~/.profile.

If you don't have a file /etc/bash_completion, you can create your own rules with the compgen and complete commands.

For example, for sudo to complete with executables, you could do:
Code:

complete -c sudo
Or you could think of more elaborate methods.

/me continues to learn new commands.....

Quigi 08-22-2006 02:46 PM

I should have read "man bash" before (so should everyone).

Extrasolar:
Use complete-command (M-!), i.e.,

sudo /usr/sbin/lso[M-!]

to complete it to "/usr/sbin/lsof". (And if /usr/sbin is in your path, you don't have to type it.)

And yes, bash has Programmable Completion (see man), which sounds nifty.


All times are GMT -5. The time now is 07:23 PM.