You are not logged in.

#1 2011-10-16 14:11:44

Nanthiel
Member
From: Slovenia
Registered: 2009-09-20
Posts: 148

[SOLVED] Bash completion with wildcards

Hello everyone,

after installing the bash-completion package, I can no longer do this:

rm *.JPG<tab><tab>

to display all the *.JPG files in the current directory. The same applies to "ls" and probably other commands.

However, using a command like "gpicview" and some other commands works. Also removing the package fixes this, but the package adds so much functionality I don't want to remove it now. smile

Is there a solution to this? I've searched the forums and google, and found people asking about this on the Ubuntu forums a year ago, but with no answers. I've also looked in to the /etc/bash_completion file and the /etc/bash_completion.d directory to find a solution, but I haven't been able to. The program "rm" is not mentioned anywhere exclusively, and I can't find a reference to "builtin" handling that would cause this. And "ls" isn't a builtin anyway.

Thanks!

Edit: marked as [SOLVED].

Last edited by Nanthiel (2011-10-16 15:51:13)

Offline

#2 2011-10-16 14:30:00

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,109
Website

Re: [SOLVED] Bash completion with wildcards

Expansion like that is controlled by readline, specifically by glob-expand-word, which defaults to the key-chain C-x *. I know that ZSH will expand that sort of thing inline, but i've never heard of bash performing that sort of expansion inline via the tab key.

edit: aha, misunderstood the question.

Last edited by falconindy (2011-10-16 18:09:10)

Offline

#3 2011-10-16 15:32:15

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,716

Re: [SOLVED] Bash completion with wildcards

Check how the commands are completed:

complete -p rm gpicview

Probably, with the bash-completion package, you have some completion specifications for `rm' , but not for `gpicview'. As a result, `gpicview' uses bash's default completion rules. Use `-o bashdefault' may give you what you want:

compopt -o bashdefault rm

This silver ladybug at line 28...

Offline

#4 2011-10-16 15:49:53

Nanthiel
Member
From: Slovenia
Registered: 2009-09-20
Posts: 148

Re: [SOLVED] Bash completion with wildcards

@lolilolicon: Ah, the command does show a definition, which isn't there for gpicview. After seeing this, I also found the definition in /etc/bash_completion. It looks like this:

for i in a2ps awk bash bc bison cat colordiff cp csplit \
    curl cut date df diff dir du enscript env expand fmt fold gperf gprof \
    grep grub head indent irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
    mv netstat nl nm objcopy objdump od paste patch pr ptx readelf rm rmdir \
    sed seq sha{,1,224,256,384,512}sum shar sort split strip tac tail tee \
    texindex touch tr uname unexpand uniq units vdir wc wget who; do
    have $i && complete -F _longopt -o default $i
done
unset i

"-o default rm" should make it work — the manpage says it means that the default readline action gets invoked if nothing else worked.

Now I changed the "-o default" line into "-o bashdefault", and it works! But am I safe changing this file? It will get overridden once the package gets updated, right?

Thanks for the help, I'll mark the thread as solved.

Offline

#5 2011-10-16 16:06:27

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,716

Re: [SOLVED] Bash completion with wildcards

Nanthiel wrote:

@lolilolicon: Ah, the command does show a definition, which isn't there for gpicview. After seeing this, I also found the definition in /etc/bash_completion. It looks like this:

for i in a2ps awk bash bc bison cat colordiff cp csplit \
    curl cut date df diff dir du enscript env expand fmt fold gperf gprof \
    grep grub head indent irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
    mv netstat nl nm objcopy objdump od paste patch pr ptx readelf rm rmdir \
    sed seq sha{,1,224,256,384,512}sum shar sort split strip tac tail tee \
    texindex touch tr uname unexpand uniq units vdir wc wget who; do
    have $i && complete -F _longopt -o default $i
done
unset i

"-o default rm" should make it work — the manpage says it means that the default readline action gets invoked if nothing else worked.

No, the default readline command bound to <TAB> is `complete', as per bash(1):

complete (TAB)
    Attempt to perform completion on the text before point.  Bash attempts completion treating the text as a variable (if the text begins with $), username (if the text begins with ~), hostname (if the text begins with @), or command (including aliases and functions) in turn.  If none of these produces a match, filename completion is attempted.

But what you want is, as falconindy already said, the glob-expand-word command.

Now I changed the "-o default" line into "-o bashdefault", and it works! But am I safe changing this file? It will get overridden once the package gets updated, right?

Yeah, it will be overwritten. Instead, add `compopt -o bashdefault <commands>' to ~/.bash_completion, which is source'd by /etc/bash_completion.

(Unrelated, but I strongly discourage using bash_completion. It's so Debian and Ubuntu!!! tongue)


This silver ladybug at line 28...

Offline

#6 2011-10-16 19:09:40

Nanthiel
Member
From: Slovenia
Registered: 2009-09-20
Posts: 148

Re: [SOLVED] Bash completion with wildcards

lolilolicon wrote:

(Unrelated, but I strongly discourage using bash_completion. It's so Debian and Ubuntu!!! tongue)

How do you cope? Do you simply remember all the stuff? Or have manpages open constantly? smile
I struggle with programs like "ip" that have a unique syntax, and I need them rarely. So tabbing to get the proper completion is very useful.

I'll see what I'll end up using. The long shell startup times for loading all the completion stuff are annoying.

Offline

#7 2011-10-17 01:20:01

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,716

Re: [SOLVED] Bash completion with wildcards

Nanthiel wrote:
lolilolicon wrote:

(Unrelated, but I strongly discourage using bash_completion. It's so Debian and Ubuntu!!! tongue)

How do you cope? Do you simply remember all the stuff? Or have manpages open constantly? smile
I struggle with programs like "ip" that have a unique syntax, and I need them rarely. So tabbing to get the proper completion is very useful.

I'll see what I'll end up using. The long shell startup times for loading all the completion stuff are annoying.

I rarely use command option completion. Usually, I either know the syntax of the command I'm going to run or know the man page well enough to find the answer. Relying on such completions would mean I didn't understand what I were typing. Plus, if the options (I use) were so plenty that I couldn't remember, the list TAB-completion would be even longer. Programs which have long long option names and require such to be fully specified may need it, but so far I don't find many such programs.
What I mostly need is filename completion, filtered by filename pattern / type.  But currently bash is not perfect in some edge cases, and a simple `complete -f -X' is not working as expected.  But it will improve in the next version, hopefully.

The long startup time is due to disk IO (see how many files there are in /etc/bash_completion.d/). They probably will at some point implement dynamic loading of completion functions (via `complete -D`), so all the files don't have to be loaded at startup.

Last edited by lolilolicon (2011-10-17 01:24:45)


This silver ladybug at line 28...

Offline

#8 2011-10-18 17:45:55

rwd
Member
Registered: 2009-02-08
Posts: 646

Re: [SOLVED] Bash completion with wildcards

If the goal is to look up quickly commmands you issued earlier you can just search through ~/.bash_history with the 'history' command. For convenience I have this in my  /etc/profile.bash

# increase the number of items in ~/.bash_history
export HISTSIZE=1000
export HISTFILESIZE=1000

# alias to search bash history
function hist {
    history | grep -i "${1}"
}

and then use the numbers to re-issue a command
!<some number in history>

Offline

#9 2011-10-18 17:49:31

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,109
Website

Re: [SOLVED] Bash completion with wildcards

The 'fc' builtin in bash exists for this purpose.

Offline

Board footer

Powered by FluxBB