简体   繁体   English

键入“第一个命令”时 Bash 可编程完成

[英]Bash programmable completion when typing “first command”

Is there a way how to control completion for first command being typed on bash4 command prompt?有没有办法控制在 bash4 命令提示符下键入的第一个命令的完成?

I can control completions for "empty line" with complete -E, but as soon as some characters are typed, bash (or possibly readline) gives me all filenames from PATH, builtin commands (if, then, else...) and functions on completion attempt (TAB).我可以使用完整的 -E 来控制“空行”的补全,但是一旦输入了一些字符,bash(或可能是 readline)就会给我来自 PATH、内置命令(if、then、else...)和函数的所有文件名完成尝试 (TAB)。

I was able to avoid PATH entries by unsetting PATH in PROMPT_COMMAND and restoring it by binding function to trap DEBUG, but no luck with getting rid of bash bultin commands completions.我能够通过在 PROMPT_COMMAND 中取消设置 PATH 并通过将函数绑定到陷阱 DEBUG 来恢复它来避免 PATH 条目,但是没有摆脱 bash bultin 命令完成的运气。

Any ideas?有任何想法吗?

This is a veritable weak point in the design of bash's programmable completion facilities.这是 bash 可编程完成工具设计中的一个名副其实的弱点。 Even the “default” command completion -D takes effect for commands on a blank line only (provided that no complete -E is specified).甚至“默认”命令补全-D仅对空行上的命令有效(前提是未指定complete -E )。 Something like complete -I (for incompletely typed-in commands) is missing.缺少像complete -I (用于不完整输入的命令)之类的东西。

As of Bash 5.0, the bash built-in complete supports the -I option:Bash 5.0 开始,bash 内置complete支持-I选项:

The -I option indicates that other supplied options and actions should apply to completion on the initial non-assignment word on the line, or after a command delimiter such as ';' -I 选项指示其他提供的选项和操作应应用于行上初始非赋值字的补全,或命令定界符(如“;”)之后的补全。 or '|', which is usually command name completion.或 '|',通常是命令名补全。

I was able to download and install bash 5.1 on an ubuntu docker image with no problem and used this to emulate a tool environment shell.我能够毫无问题地在 ubuntu docker 映像上下载并安装 bash 5.1 ,并使用它来模拟工具环境 shell。 Bash 5 should mostly be a drop-in replacement . Bash 5 应该主要是一个简单的替代品

bash completions are stored in /etc/bash_completion.d/ If you have root access, then you could edit/delete files there. bash 完成存储在 /etc/bash_completion.d/ 如果您有 root 访问权限,那么您可以在那里编辑/删除文件。

If you want to control things at the user level, then you could create and edit ~/.inputrc.如果您想在用户级别控制事物,那么您可以创建和编辑 ~/.inputrc。 Have a look at /etc/inputrc and it will give you an idea of where to start.看看 /etc/inputrc ,它会让你知道从哪里开始。

~/.inputrc overrides anything in /etc/inputrc, so you could change the places where bash_completion looks for completion files. ~/.inputrc 覆盖 /etc/inputrc 中的任何内容,因此您可以更改 bash_completion 查找完成文件的位置。 You could make a local completion directory and have only the items you want completed in there.您可以创建一个本地完成目录,并在其中仅包含您想要完成的项目。 I believe you can also eliminate bash built-ins also.我相信您也可以消除 bash 内置插件。

There are also a couple of very good tutorials on bash completion online.还有一些关于 bash 完成在线的非常好的教程。 Here Here这里这里

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

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