12

Since tab completion became a standard feature in shells I have been a very happy user of it. I even like Bash's intelligent completion (where it only searches for PDF files, if I type evince foo<TAB>) - as long as it guesses correctly.

It ruins an otherwise happy day when Bash uses its intelligent completion to refuse completing.

Today I wanted to:

tar xvf centos5.8_x86_1.ova

So I entered:

tar xvf cent<TAB>

and Bash refuses to complete the path because .ova-files is not what you usually use with tar.

I know I can uninstall the bash-completion package to remove the intelligent completion. But can I have my cake and eat it too?

Can I ask bash to use intelligent completion first, and if that fails, then use non-intelligent completion?

1

3 Answers 3

10

The following should turn on normal bash completion after all 'intelligent' completions have failed for the programs tar, vim and emacs:

compopt -o bashdefault tar vim emacs

see also the relevant page in the online bash manual.

Caveat: you get the normal behavior for the type of completion (commands/filenames/usernames etc.) defined by the intelligent completion for the command at hand, for example, the completion for service will not complete filenames even if you call complete -o bashdefault service.

In this and other cases where the intelligent autocompletion fails, you can always press Alt-/ (or M-/ in the bash manual's notation) to complete filenames almost in the same way that TAB works if intelligent completion is disabled (almost the same way in the sense that M-/ does not complete patterns like *.txt). See here for key sequences for other types of completions such as command and usernames.

1
  • 2
    Thanks for the Alt-/ mention, I had no idea that was in there Apr 17, 2015 at 19:08
4

Hej Ole :-),

Having your cake and eating it too:

<tab>will give you bash's best guess at completion. As you know, it is sometimes wrong.

However, M-/ ( ALT+SHIFT+7 (and that is left ALT, not the right AltGr) on our danish keyboards) will force completion as a file for when bash's completion is not perfect. So I use M-/ when I know I want to force completion as a file.

For me, that is the best of both worlds.

0

bash completetion is an extension of bash's standard completion and is itself a bash program. So if you want to change how the program acts, just change it. This is free software, you are free to do it. For the specific case of the tar command, the file is /usr/share/bash-completion/completions/tar. For tar to autocomplete a file with extension ova, just add it to the line

local tars='@(@(tar|gem|spkg)?(.@(Z|[gx]z|bz?(2)|lzma))|t@([glx]z|bz?(2)))'

so it says

local tars='@(@(tar|gem|spkg|ova)?(.@(Z|[gx]z|bz?(2)|lzma))|t@([glx]z|bz?(2)))'

Note that this is the path and the code existing for Debian Wheezy (current stable version). A different distro or version may have a different code but the idea is the same. Open the code and adjust it to do what you want it to do.

Edit: I just noticed that the question was about every time that it doesn't find a match and not only the specific case of adding ova to tar. Maybe it can still be useful to someone, to know where they can look for the source.

2
  • 4
    Modifying files in /usr that are owned by the package manager is a good way to induce a nasty shock on upgrade. Better: override the packaged configuration.
    – Chris Down
    Mar 31, 2014 at 3:10
  • 1
    @ChrisDown and how would you do that? I'm still looking for the clean .bashrc solution. Mar 31, 2014 at 5:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .