3

I want to ask bash how it would complete a string as if I had typed it in a shell and hit tab. For example, if I type

ls /[TAB][TAB]

I see the list of files and dirs in / that could possibly complete the ls command. How do I ask bash how it would complete 'ls /' without typing it and hitting tab? I want something like:

query_complete 'partial command line string'

I read the man page for complete and compgen, but couldn't figure out how to do it with them.

Note: 'ls /' is not the actual command I'm interested in, just an example. I am looking for a general solution for any arbitrary string representing a partial command line.

4 Answers 4

3

You could try:

compgen -o default /
2
  • thanks, maybe I didn't make it clear enough; the 'ls /' was just an example. Other commands have much more complex autocomplete scripts defined. I really want something that can take any arbitrary string and output the completion hints as if you had typed that string.
    – Ryan McKay
    Feb 4, 2011 at 23:09
  • 1
    @Ryan: Bash doesn't have that kind of reflexive functionality. I'm assuming you want all the programmable completions performed by the bash-completion package, too. Have you looked at the contents of that package? What you want to do falls somewhere between extremely complex and not possible. Feb 5, 2011 at 1:01
1

Apparently the Awesome WM has a neat little trick getting hold of this.

https://stackoverflow.com/questions/3520936/accesssing-bash-completions-for-specific-commands-programmatically

But I can't seem to get it working properly. Let me know if you have better luck.

0

You could use a function that uses bash's Parameter Expansion, like this:

expand(){ echo ${a:+$1}; }; expand "ls /*"

But you'll need to replace your [TAB][TAB] with "*".

-1

I think a good option for you is to look into bash functions you can write for your .bashrc file. My guess is that you end up typing commands over and over again, and you want to speed things up. Here's an example from my .bashrc file:

newmod() {
mkdir -p ${1}/{manifests,files,templates} && \
echo "class $1" >> ${1}/manifests/init.pp && \
echo "class ${1}::conf" >> ${1}/manifests/conf.pp && \
echo "class ${1}::packages" >> ${1}/manifests/packages.pp
} 

I create lots of puppet modules. This function, when invoked from the shell, does the following work for me:

[configmgr@mback-dev ~]$ newmod testmodule
[configmgr@mback-dev ~]$ tree testmodule/
testmodule/
|-- files
|-- manifests
|   |-- conf.pp
|   |-- init.pp
|   `-- packages.pp
`-- templates

3 directories, 3 files
[configmgr@mback-dev ~]$ 

The files get populated with scaffolding for the modules class, packages, and configuration. This saves me tons of typing!

HTH, ztron

1
  • How does this relate to getting bash string completion results programmatically?
    – Bill Weiss
    Jun 8, 2011 at 19:49

You must log in to answer this question.

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