1

I'm wondering if it's possible to make autocompletion span an = sign. So, for example, I want to type foo BAR=[TAB][TAB], and have it fill in the possible values for BAR. I've tried the following: I have a file called 'bar', as follows:

#!/bin/bash
echo -e "BAR=100\nBAR=110\nBAR=200" | grep "^$2"

And then I do:

~> complete -C bar foo

If I type foo [TAB][TAB], it gives me some possible values for BAR. If, I type foo BAR=[TAB][TAB], it fails (it appends BAR=BAR= to the end of the command). (note, if I type bar 1 BAR=, it gives me a proper list of completions, so this is not an issue with the bar script).

This would be very useful for some scripts I have.

tshepang
  • 12,111
  • 21
  • 91
  • 136
John
  • 3,400
  • 3
  • 31
  • 47
  • I'm not sure exactly what you mean but as far as I can tell, the answer to your question is probably "no". It would be useful if you made it clear in the question what exactly you're trying to do and why this behaviour would be useful, as there might be a better way to go about it anyway. – Tom Fenech Sep 16 '14 at 19:23
  • I think this is what `COMP_WORDBREAKS` is for though you can't really just go changing that. So the question is are there ways to work around that behaviour like in [this question](http://stackoverflow.com/questions/10528695/how-to-reset-comp-wordbreaks-without-effecting-other-completion-script) about `:`. – Etan Reisner Sep 16 '14 at 19:31
  • Tom: I have a somewhat complicated makefile, which takes in one of several configuration files, which are located in another directory. The file list is somewhat dynamic, so I cannot make rules for each target. Instead, the target is specified on the make line: `make target=....` It would be nice to have this autocomplete. There are also a few other macros that can be set, and it would be nice to have autocompletion on these as well. – John Sep 16 '14 at 20:19
  • ALMOST there... if the = is treated as a word break, I thought I could check the previous word, and see if it's BAR, and autocomplete based on that... only after the first character after the `=` sign, `$3` becomes `=` instead of `BAR`... blah! – John Sep 16 '14 at 21:00
  • Possible duplicate of [Bash completions with equals sign and enumerable flag values](http://stackoverflow.com/questions/5040883/bash-completions-with-equals-sign-and-enumerable-flag-values) – miken32 Mar 14 '16 at 21:20

1 Answers1

1

Create a function (in your .bashrc e.g.):

bar()
{
  local POS=${COMP_WORDS[COMP_CWORD]}
  if [ "${COMP_WORDS[1]}" = "BAR" ] && [ $COMP_CWORD -eq 3 ]; then
    COMPREPLY=($(echo -e "100\n110\n200" | grep ^$POS ))
  fi
}

and link function to command foo:

complete -F bar foo
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • This works, only it seems to modify the global COMP_WORDBREAKS, which I'm worried would adversly effect other applications. – John Sep 16 '14 at 20:46
  • I've updated my answer. Disadvantage: You have to type at least one vaild character after `=`. – Cyrus Sep 16 '14 at 22:02