Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary command completion - possible?

Tags:

bash

I'm looking for a way to hook in a custom bash completion function. Problem is, I want this completion function not just for a specific command, but for all commands.

Is this even possible? Having looked around for a while, I couldn't find any resources online.

To reduce the problem to the most trivial case: would it be possible to always have tab-completion for the string 'foo'?

Meaning echo f<tab> would expand into echo foo, and ls fo<tab> would expand into ls foo

For context: I'm trying to implement something similar to http://blog.plenz.com/2012-01/zsh-complete-words-from-tmux-pane.html in bash, but I'm starting to fear it's not possible.

like image 516
jbnicolai Avatar asked Sep 28 '22 00:09

jbnicolai


1 Answers

You can do that with the -D option of the complete command:

suggest_hello()
{   
    COMPREPLY=( hello )
    return 0
}

complete -D -F suggest_hello

Now whenever I type echo h<Tab>, I get echo hello.

$ help complete
complete: ...
...
      -D  apply the completions and actions as the default for commands
          without any specific completion defined
...
like image 138
Andrea Corbellini Avatar answered Oct 05 '22 08:10

Andrea Corbellini