complete命令有什么用?


17

complete在我的gnome终端上运行 命令时,它显示了一些命令,它们是什么?complete命令的用途是什么?

$ complete
complete -F _minimal 
complete -F _filedir_xspec oodraw
complete -F _filedir_xspec elinks
complete -F _filedir_xspec freeamp
complete -F _longopt split
complete -F _longopt sed
complete -F _longopt ld
complete -F _longopt grep
complete -F _service /etc/init.d/vboxweb-service
complete -F _service /etc/init.d/vboxballoonctrl-service
complete -F _service /etc/init.d/rc
complete -F _service /etc/init.d/nmbd
complete -F _service /etc/init.d/halt
complete -j -P '"%' -S '"' jobs
complete -d pushd

清单很长,所以我张贴了其中的一些。


1
我尝试了“ man complete”命令,但没有显示任何内容。
Avinash Raj 2014年

2
试试help complete
Sylvain Pineau'4

Answers:


16

complete是bash内置函数。因此,系统上没有二进制文件。它处理按时如何完成命令tab

示例:如果键入:

user@host:~$ pidof <tab><tab>

...列表中出现了该命令的所有可能值。在这种情况下,这意味着所有正在运行的进程。查看complete函数的输出:

user@host:~$ complete | grep pidof
complete -F _pgrep pidof

这意味着_pgrep当按Tab键时将执行功能(-F)pidof。该函数的定义在中/etc/bash_completion.d/procps

另一个示例:如果键入:

user@host:~$ cd /usr/<tab><tab>
bin/     games/   include/ lib/     lib32/   local/   sbin/    share/   src/

......你看到的文件夹,你可以在列表cd/usr/。执行哪个功能?greping的complete功能(如上)告诉我们它是funtction _cd/etc/bash_completion

自己动手:您有一个调用的程序/脚本/bin/myprog,如果您按以下方式执行它,则需要该程序/脚本

user@host:~$ myprog /home/user/<tab><tab>

...它应该只列出文件夹,而不是文件。因此,使用以下命令扩展您的bash完成:

user@host:~$ complete -F _cd myprog

而已。您还可以编写自己的函数来完成自定义操作,例如仅完成特定的文件或数字或静态值列表...


因此,这就是git的命令行完成似乎比大多数程序更聪明的原因
AShelly 2014年

8

complete是一个bash命令,用于在用户按下TAB终端中的键时执行自动完成操作。

调用just complete将列出所有为命令或服务选项的自动完成而注册的功能。

从bash手册页:

complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat]
          [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix]
          [-S suffix] [name ...]
    Specify how arguments are to be completed by Readline.

    For each NAME, specify how arguments are to be completed.  If no options
    are supplied, existing completion specifications are printed in a way that
    allows them to be reused as input.

    Options:
      -p    print existing completion specifications in a reusable format
      -r    remove a completion specification for each NAME, or, if no
        NAMEs are supplied, all completion specifications
      -D    apply the completions and actions as the default for commands
        without any specific completion defined
      -E    apply the completions and actions to "empty" commands --
        completion attempted on a blank line

    When completion is attempted, the actions are applied in the order the
    uppercase-letter options are listed above.  The -D option takes
    precedence over -E.

    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

检查/usr/share/bash-completion/bash_completion以查看bash随附的默认补全。

访问http://www.linuxjournal.com/content/more-using-bash-complete-command以获得有关此命令的完整教程。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.