7

Consider this one-liner to activate bash completion for foobar:

complete -F _known_hosts foobar

This shows a list of completion options for

> foobar <TAB> <TAB>

but not for

> $(foobar <TAB> <TAB>

or

> `foobar <TAB> <TAB>

I think it makes a lot of sense to have tab completion for expressions that are inside backticks or $(). How can I make this work?

dbc
  • 104,963
  • 20
  • 228
  • 340
mnieber
  • 992
  • 9
  • 23
  • 3
    I think the problem is that programmable completion occurs before the shell has done any other parsing; the command name to the completion system is `$(foobar`, not `foobar`. Putting a space before `foobar` doesn't help, because then the command name looks like `$(`. Something like `ls && foobar ` works, so there might be some minimal parsing or special casing; this might be a feature request for `bash` 4.5. – chepner Dec 07 '16 at 14:22

3 Answers3

1

Try these:

Esc+/ (to complete the filename i.e. you have entered some characters

Ctrl+x+/ (to list possible filenames - do note; there are 2 +)

(I was searching for an answer to this question for a long time; then got this from bash man page :-))

zmag
  • 7,825
  • 12
  • 32
  • 42
mnt
  • 11
  • 1
1

Bash completion inside back ticks or $() is called "command substitution".

So it looks similar to a few other questions:

https://unix.stackexchange.com/questions/152328/bash-tab-completion-fails-inside-of-command-substitution

https://askubuntu.com/questions/571544/bash-tab-completion-bash-unexpected-eof-while-looking-for-matching-bash/576983#576983

https://unix.stackexchange.com/questions/152328/bash-tab-completion-fails-inside-of-command-substitution

For Debian-based linux operating systems like Ubuntu, the problem is a known bug from the bash-completion application: /usr/share/bash-completion/completions

You can report an issue here:

https://github.com/scop/bash-completion/issues

rossetta
  • 61
  • 3
0

If you only want the command completion, then you can do the following:

1) Open the file /usr/share/bash-completion/bash_completion.

2) Go to the function _quote_readline_by_ref.

3) Replace the line [[ ${!2} == \$* ]] && eval $2=${!2} with [[ ${!2} == \$\'* ]] && eval $2=${!2}.

4) Save the changes and exit.

After this, If you open a new tab and start typing command inside quotes or $(), the command will get completed or shows the possibility as it happens normally. See the example below:

[22:57]✓ -> echo $(ls
ls           lsblk        lscpu        lshw         lsipc        lslogins
lsof         lspcmcia     lsusb        lsattr 

Let me know if you face any problems.

Samit
  • 595
  • 2
  • 12
  • I tried it, and unfortunately it made no difference (your example also works for me without the patch, and with the patch my use-case still doesn't work). – mnieber Jul 12 '18 at 06:12
  • I don't think there is any direct way. I guess you may need to write your own program to enable such feature and provide to the open source world :) – Samit Jul 13 '18 at 06:32