简体   繁体   English

如何让我的程序使用制表符完成?

[英]How can I make my program utilize tab completion?

I've noticed that some programs (eg hg) allow the user to tab-complete specific parts of the command. 我注意到一些程序(例如hg)允许用户选项卡完成命令的特定部分。 For example, if, in an hg repository working directory, I type: 例如,如果在hg存储库工作目录中输入:

hg qpush --move b8<TAB>

It will try to complete the command with any mercurial patches in my patch queue that start with "b8". 它将尝试使用以“b8”开头的补丁队列中的任何mercurial补丁完成命令。

What I'd like to do is imitate this behavior in my program. 我想做的是在我的程序中模仿这种行为。 That is, I have a series of commands that depend on files within a certain directory, and I'd like to be able to provide tab completion in the shell. 也就是说,我有一系列依赖于某个目录中文件的命令,我希望能够在shell中提供制表符完成。 Is there an API for providing this on Ubuntu Linux (preferably using python, as that's what my script is written in)? 是否有一个API在Ubuntu Linux上提供此功能(最好使用python,因为这是我的脚本编写的)?

To do this, you need to write tab-completion modules for your shell. 为此,您需要为shell编写制表符完成模块。 The default shell in most Linux distributions is bash , so you should write a completion script (typically a shell script). 大多数Linux发行版中的默认shell都是bash ,因此您应该编写一个完成脚本(通常是一个shell脚本)。 Once you've written your script, add it to /etc/bash_completion.d/ . 编写脚本后,将其添加到/etc/bash_completion.d/ This should be distributed with your program (for Linux distributions, included in the package). 这应该与您的程序一起分发(对于Linux发行版,包含在程序包中)。

Debian Administration has a guide for writing your completion scripts . Debian Administration提供了编写完成脚本的指南 For using completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion . 要在Mac上使用完成,请参阅https://trac.macports.org/wiki/howto/bash-completion

For examples of completion files, take a look at the bash-completion project from Debian (also on Github ). 有关完成文件的示例,请查看Debian (也在Github上 )的bash-completion项目 See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial . 另见https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial

If you use zsh , hack.augusto linked to the documentation for writing completions . 如果您使用zsh ,则hack.augusto链接到用于编写完成的文档

This is what the readline module is for. 这就是readline模块的用途。

Actually, readline is a common C library so it has bindings in many languages. 实际上,readline是一个常见的C库,因此它具有多种语言的绑定。 And I can say, I've had tons of fun with it. 我可以说,我玩得很开心。

Enjoy B) 享受B)

You might want to try the zsh shell, it has a great completion system with support for tons of applications. 您可能想尝试zsh shell,它有一个很好的完成系统,支持大量的应用程序。

The completion system is written with the shell language, but if you really want to use python you can run the interpreter from inside your completion function. 完成系统是用shell语言编写的,但是如果你真的想使用python,你可以从完成函数内部运行解释器。 The down side it that if you want to write completion for your own software, you will need to do some reading ( user manual and the manpage for instance). 如果您想为自己的软件编写完成,则需要进行一些阅读(例如用户手册联机帮助页 )。

Take a look at the source of the 'cmd' module in the Python library. 看一下Python库中'cmd'模块的来源。 It supports command completion. 它支持命令完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM