bash 让“完成”和“菜单完成”协同工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12044574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "complete" and "menu-complete" to work together
提问by kYuZz
I found out that the Bash shell supports a type of autocompletion that is different from the "traditional" autocompletion, where all possibilities get listed on the following line.
我发现 Bash shell 支持一种不同于“传统”自动完成的自动完成类型,所有可能性都列在下一行。
With the "traditional" autocompletion, if I type chand then press the Tabkey, I get something like:
使用“传统”自动完成功能,如果我输入ch然后按下Tab键,我会得到类似的信息:
$ ch
chacl chgrp chmod chown chvt
But if I add the following line to my /etc/inputrc(which remaps the Tab key to the built-in menu-completefunction):
但是,如果我/etc/inputrc将以下行添加到我的(将 Tab 键重新映射到内置menu-complete函数):
Tab: menu-complete
then the behavior of the shell changes: the word to be completed is replaced "inline" with a single match from the list of possible completions, and if I press the Tab key again, the word gets replaced with the next match.
然后 shell 的行为发生变化:要完成的单词被替换为“内联”与可能完成列表中的单个匹配项,如果我再次按下 Tab 键,该单词将被替换为下一个匹配项。
I found this useful, but I still wanted to keep the traditional autocompletion and have it bound to the key combination Ctrl + Tab. So I added the following line to my /etc/inputrcfile, according to what the readlinelibrary documentation suggests:
我发现这很有用,但我仍然想保留传统的自动完成功能并将其绑定到键组合Ctrl + Tab。因此/etc/inputrc,根据readline库文档的建议,我将以下行添加到我的文件中:
Ctrl-Tab: complete
However, adding this line only seems to make both Taband Ctrl-Tabcall the traditional completefunction.
然而,加入这一行似乎只能使双方Tab并Ctrl-Tab调用传统complete功能。
Does anyone know what I am doing wrong?
有谁知道我做错了什么?
Thanks in advance!
提前致谢!
回答by John Wordsworth
To start with, I'm not a massive expert in this area, but I think I can answer your question. First of all, while you are using Bash, Bash is a shell which interprets keyboard commands that it receives from a terminal / console. While you are informing Bash how to react to specific key combinations in the inputrc file, your Terminal determines precisely which character is 'sent' to the Shell before the inputrc file even enters the equation.
首先,我不是这方面的专家,但我想我可以回答你的问题。首先,当您使用 Bash 时,Bash 是一个 shell,它解释从终端/控制台接收到的键盘命令。当您通知 Bash 如何对 inputrc 文件中的特定组合键做出反应时,您的终端会在 inputrc 文件甚至进入等式之前准确地确定哪个字符被“发送”到 Shell。
Unfortunately, on my system (granted, it's OSX - but I don't think this is strange behaviour when compared to Linux), both Taband Ctrl-Tabsend the same keyboard input to the shell. Infact, both Tab and Ctrl-Tabsend a Ctrl-Icommand to the shell, and indeed, if I enter Ctrl-Iwhen using the terminal, it performs the completion as if I hit Tab.
不幸的是,我的系统上(当然,它的OSX -但我不认为这是相较于Linux的奇怪的行为),都Tab和Ctrl-Tab发送相同的键盘输入对外壳。事实上,Tab 和Ctrl-Tab向Ctrl-Ishell发送命令,事实上,如果我Ctrl-I在使用终端时输入,它会执行完成,就像我点击 Tab 一样。
The software (installed on most Linux systems by default), showkey will tell you what keys the shell is receiving when you press specific keyboard inputs as you push them.
该软件(默认安装在大多数 Linux 系统上),当您按下特定键盘输入时,showkey 会告诉您外壳正在接收哪些键。
Anyway, my suggestion to you is to use Shift-Tab, which does appear to send it's own key-code to the shell. Shift-Tabon my computer shows up (using showkey) as '<ESC>[Z', which I think is pretty standard across the board. As such, your inputrc file with the following bindings should allow you to use shift-tabinstead of ctrl-tabto achieve what you desire:
无论如何,我对您的建议是使用Shift-Tab,它似乎将自己的密钥代码发送到外壳程序。Shift-Tab在我的电脑上显示(使用 showkey)为 '<ESC>[Z',我认为这是非常标准的。因此,具有以下绑定的 inputrc 文件应该允许您使用shift-tab而不是ctrl-tab实现您想要的:
Tab: menu-complete
"\e[Z": complete
The \e in the second binding represents the escape character, and the [Z are simply the characters as shown using showkey. You can get a similar effect on OSX by simply using cat, running cat from within a terminal and pressing Shift-Tabwill show you "^[[Z", where ^[ represents the escape character and the other characters are as before.
第二个绑定中的 \e 表示转义字符,而 [Z 只是使用 showkey 显示的字符。您可以通过简单地使用 cat 在 OSX 上获得类似的效果,在终端内运行 cat 并按下Shift-Tab将显示“^[[Z”,其中 ^[ 代表转义字符,其他字符与以前一样。
I know this doesn't resolve your question precisely, but as I don't think you are able to use Ctrl-Tabas a key combination, without re-mapping Ctrl-Tabto another keybinding within your terminal (more likely to be easier if you are using a GUI terminal), this is likely as close as you can get without significant effort!
我知道这并不能准确地解决您的问题,但因为我认为您不能Ctrl-Tab用作组合键,而无需重新映射Ctrl-Tab到终端中的另一个键绑定(如果您使用 GUI,则可能更容易)终端),这可能是您无需付出太多努力就能获得的最接近的结果!
回答by Arch Stanton
I have ShiftTabbound to menu-complete-backward, so it goes back one step if I skipped the right completion, and I've mapped Ctrlqto complete, so if there are several possible completions I hit Ctrlqto list them without having to cycle through them.
我已经ShiftTab绑定到menu-complete-backward,所以如果我跳过正确的完成,它会返回一步,并且我已经映射Ctrlq到complete,所以如果有几个可能的完成,我会点击Ctrlq列出它们而不必循环它们。
# Make Tab cycle between possible completions
# Cycle forward: Tab
# Cycle backward: Shift-Tab
TAB: menu-complete
"\e[Z": menu-complete-backward
# Make C-q display the list of possible completions
Control-q: complete
# Display the list of matches when no further completion is possible
set show-all-if-unmodified on
Edit:Ctrlqis bound to quoted-insertby default, that is, it tells the shell to take the next key literally. quoted-insertis also bound to Ctrlv, so you don't lose that functionality if you rebind Ctrlq. Anyway, I've found that AltESCalso works, by default, for showing the possible completions (as far as I can tell it is equivalent to TAB); note that it may be seized by Gnome, then either double press ESCor rebind "Switch windows directly" in Settings → Devices → Keyboard → Navigation.
编辑:默认Ctrlq绑定到quoted-insert,也就是说,它告诉外壳从字面上获取下一个键。quoted-insert也绑定到Ctrlv,因此如果您重新绑定 ,则不会丢失该功能Ctrlq。无论如何,我发现AltESC默认情况下,它也可以用于显示可能的完成(据我所知,它相当于TAB);请注意,它可能会被 Gnome 占用,然后ESC在设置 → 设备 → 键盘 → 导航中双击或重新绑定“直接切换窗口”。
回答by Tate Thurston
The following should achieve what you're looking for (if I understand correctly!)
以下应该实现你正在寻找的东西(如果我理解正确的话!)
In your .inputrc
在你的 .inputrc
# display all possible matches for an ambiguous pattern at first tab
set show-all-if-ambiguous on
# next tab(s) will cycle through matches
TAB: menu-complete
# shift tab cycles backward
"\e[Z": menu-complete-backward
回答by chepner
I'm not sure Ctrl-Tab is a real character; my terminal, for instance, ignores the combination. I think the only way to use Ctrl-Tab is to use your terminal emulator to map it to some otherwise unused escape sequence, then bind that sequence to complete.
我不确定 Ctrl-Tab 是一个真正的角色;例如,我的终端会忽略该组合。我认为使用 Ctrl-Tab 的唯一方法是使用终端模拟器将其映射到一些其他未使用的转义序列,然后将该序列绑定到complete.

