Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Tab path completion does not work (at least on Ubuntu and AFAIK Arch) with

sudo mount <whatever>

The iso file I am trying to mount is not in /etc/fstab. If I just type

mount <whatever>

the completion works (but of course the command fails as I am not root). Apparently it is sudo that breaks it.

How can make completion work with sudo?

It is surprising that with

sudo umount <whatever>

completion works. How is it achieved? Does it look into /etc/fstab?

Solution: I just put a shell script into /usr/local/bin that calls sudo mount ... with the arguments passed to it. Completion works when calling this script since there is no sudo in the way.

share|improve this question
add comment

4 Answers

up vote 8 down vote accepted

This has not at all to do with bash, but it depends on the completions programmed in the package bash-completion.

From some comments in the file /etc/bash_completion.d/mount:

# mount(8) completion. This will pull a list of possible mounts out of
# /etc/{,v}fstab, unless the word being completed contains a ':', which
# would indicate the specification of an NFS server. In that case, we
# query the server for a list of all available exports and complete on
# that instead.
#

# umount(8) completion. This relies on the mount point being the third
# space-delimited field in the output of mount(8)
#

Also, you find in the main file /etc/bash_completion the following comment, that explicitly talk about mount and umount commands:

# A meta-command completion function for commands like sudo(8), which need to
# first complete on a command, then complete according to that command's own
# completion definition - currently not quite foolproof (e.g. mount and umount
# don't work properly), but still quite useful.
#
share|improve this answer
add comment

No it doesn't look in fstab. Bash completion looks for commands in path, aliases in bashrc and/or bash_profile and paths. So if you type something like sudo mount /mnt/some_folder it should auto-complete. Maybe when you did the umount you were in a relative path for your destination and it did an auto-complete.

share|improve this answer
    
Thanks. I am sure auto-complete does not work for sudo mount /mnt/some_folder, 100% sure. AFAIK it does not work on Arch Linux either. When I did the umount I was not in the relative path for the destination. –  Ali Feb 4 '12 at 11:27
add comment

This can easily be solved by looking into the Arch Wiki:

It is useful to have the auto-complete feature (pressing Tab key twice on the keyboard) after you type some command like sudo.

To do this add a line in this format to your ~/.bashrc file:

complete -cf your_command

For example, to enable auto-complete after sudo and man:

complete -cf sudo complete -cf man

share|improve this answer
    
Thanks. Please note that auto-complete works surprisingly well in case of sudo umount. As enzotib points out, this feature is broken with mount and last time I checked, it was broken on Arch too. –  Ali Feb 4 '12 at 15:51
    
I just tried this on Arch and sudo mount /me<TAB> completes to the only path I have configured in /etc/fstab that starts with /media. –  Wieland Feb 4 '12 at 15:55
    
In my case, the file I am trying to mount is not in /etc/fstab. Could you please check this? –  Ali Feb 4 '12 at 15:59
add comment

I guess bash doesn't want to be too complex and stay portable, so they won't look in /dev to propose completion for a mount operation.

However when calling umount, it can easily look the output of df/mount/etc to know what devices are mounted and where.

Maybe zsh will provide completion for mount, heck zsh even autocomplete when doing an scp command...

share|improve this answer
    
The file I am trying to mount is an iso file in my home directory. The completion does not work. Oh, I have forgotten, completion with mount work if I do not use the sudo. It is the sudo that breaks it. Any thoughts? –  Ali Feb 4 '12 at 11:29
    
Even using absolute paths? /home/user/myiso<TAB> -> myisofile.iso. debug with sudo echo $SHELL ; sudo pwd, hopefully it'll give you some insights. –  Aki Feb 4 '12 at 11:48
    
Yes, with absolute paths. I will try the debugging you suggest. The debugging you suggest gives /bin/bash /home/ali as expected. –  Ali Feb 4 '12 at 11:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.