2

When i type something like this:

$ complete -p | grep -ie '\<man\>'

or:

$ find /etc/bash_completion.d/ -iname '*man*'

or:

$ grep -rie '\<man\>' /etc/bash_completion.d/

I don't find any trace of the complete function for the man command!

The goal is to add this completion for one of my own-made function:

complete -F <the_man_complete_function> <my_man_related_command>

Any idea?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
  • 1
    it is a bash builtin, so you can find it in `man bash`. More info : http://www.gnu.org/software/bash/manual/bashref.html#Programmable-Completion-Builtins – Aserre Aug 26 '14 at 13:55
  • 2
    FYI, there's no need to put `RESOLVED` in the title -- questions always show whether they have an accepted answer. If you want to answer your own question, do that by adding an answer other people can vote on, just like any other. There's a delay before you can accept your own answer, but it _is_ (eventually) allowed. – Charles Duffy Aug 26 '14 at 15:02

4 Answers4

3

I can't give you complete solution, but I found the following on my system

If I run a new shell and type:

> complete -p man
bash: complete: man: no completion specification

If I try to autocomplete a man command with:

> man [tab][tab]
Display all 16625 possibilities? (y or n)
CTRL-C

Now:

> complete -p man
complete -F _man man

Why now? Don't have an answer, but now I can get:

> complete -p | grep _man
complete -F _man man
complete -F _man apropos
complete -F _man whatis

I hope this helps a bit. It is quite a mystery why it works only after using it first. Someone else an idea?

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • Thank you, it's a very good clue! It looks like a kind of autoload, it's surely because it's builtin (as Ploutox wrote); i keep looking... – yolenoyer Aug 26 '14 at 14:11
  • 1
    Yep most of the completion functions are autoloaded when needed. You can force the loading of a completion function from a script using e.g. `_completion_loader man` (at least in the version of bash that I have). – dshepherd Aug 26 '14 at 16:02
1

Bash function documentation can also be found using, e.g., help complete

seanmcl
  • 9,740
  • 3
  • 39
  • 45
0

I think you are asking where the man page is for the command 'complete' can be found. If so, complete is part of bash (if you are using bash), so just type 'man bash'.

0

I found a solution : to load the not-yet-loaded completion func, I just have to launch this in .bashrc:

. /usr/share/bash-completion/completions/man

Usefull links that helped me:


EDIT

To avoid loading the man completion script on every bash session start, a better solution is to write a local completion script, to put in the directory $XDG_DATA_HOME/bash-completion/completions (typically ~/.local/share/bash-completion/completions), with the name of the desired command (as explained in the bash-completion FAQ).

Here's the code I put in the script (named myman):

#!/bin/bash

# Force the load of the man completion function if it is not loaded yet:
type -t _man >/dev/null || . /usr/share/bash-completion/completions/man

complete -F _man myman

With this method, the man completion function will be loaded only when required (by man, or myman).

yolenoyer
  • 8,797
  • 2
  • 27
  • 61
  • This isn't necessary. Bash-completion dynamically loads a completion for a command when it is first used. – Benjamin W. Sep 02 '18 at 21:16
  • @BenjaminW. Of course it does; but just try this in a fresh bash session: `myman() { echo "Custom"; man "$@"; }; complete -F _man myman`. Then if you try `$ myman `, bash will complain it doesn't find the `_man` function. That was the reason of my question, 4 years ago... – yolenoyer Sep 02 '18 at 21:43
  • Oh, your question doesn't mention that that's what you wanted to do. There is a way to have an existing completion dynamically loaded for your own function or alias, given your version of bash-completion is recent enough: drop a symlink to the existing completion in the right directory; on my system, it's `~/.local/share/bash-completion/completions`; the name of the symlink has to be the name of your command. For example, `ln -s /usr/share/bash-completion/completions/man ~/.local/share/bash-completion/completions/myman`. Now dynamic loading should work. – Benjamin W. Sep 03 '18 at 01:00
  • Thanks, but it was still not working just by linking; indeed `complete -F _man myman` still needs to be run. Please see my edit for the solution I found based on your advices. – yolenoyer Sep 03 '18 at 08:45
  • But your added solution isn't loading the completion dynamically, it'll always load it. I'll write up my complete suggestion somewhere and link it here. Since the question doesn't really ask about the loading behaviour, making it an answer here wouldn't be the best way, I think. – Benjamin W. Sep 03 '18 at 17:26
  • Yes it is loaded dynamically, I put the script in `~/.local/share/bash-completion/completions/myman`... When I run a bash session, the `_man` does not exist (tested by `type -t _man` in the command line) before I hit TAB with `myman` command... Maybe I don't understand what you mean. – yolenoyer Sep 03 '18 at 18:38