[英]How can I make bash tab completion behave like vim tab completion and cycle through matching matches?
I've been meaning to find a solution for this for YEARS.多年来,我一直想为此找到解决方案。
I am sooo much more productive in vim when manipulating files than bash for this reason.由于这个原因,在操作文件时,我在 vim 中的工作效率比 bash 高得多。
If I have如果我有
file_12390983421
file_12391983421
file_12340983421
file_12390986421
In bash and type file_1->tab , it obviously lists:在 bash 中输入 file_1->tab ,它显然列出了:
file_12390983421 file_12391983421 file_12340983421 file_12390986421
And this is a horrible bore and painful to work with.这是一个可怕的无聊和痛苦的工作。
The same sequence in vim will loop through the files one at a time. vim 中的相同序列将一次一个地循环文件。
Please someone tell me how to do this in bash, or if there is another shell that can do this, I'll switch tomorrow.请有人告诉我如何在 bash 中执行此操作,或者如果有另一个可以执行此操作的 shell,我明天将切换。
By default TAB
is bound to the complete
readline command.默认情况下, TAB
绑定到complete
readline 命令。 Your desired behavior would be menu-complete
instead.您想要的行为将是menu-complete
。 You can change your readlines settings by editing ~/.inputrc
.您可以通过编辑~/.inputrc
来更改 readlines 设置。 To rebind TAB
, add this line:要重新绑定TAB
,请添加以下行:
TAB: menu-complete
For more details see the READLINE
section in man bash
.有关更多详细信息,请参阅man bash
的READLINE
部分。
For bash >= 4 you might like these settings.对于 bash >= 4,您可能会喜欢这些设置。 You can try them directly on the command-line, and put them in your .bashrc
if you like them.您可以直接在命令行上试用它们,如果您喜欢,可以将它们放在您的.bashrc
。
# If there are multiple matches for completion, Tab should cycle through them
bind 'TAB':menu-complete
# Display a list of the matching files
bind "set show-all-if-ambiguous on"
# Perform partial completion on the first Tab press,
# only start cycling full results on the second Tab press
bind "set menu-complete-display-prefix on"
This setup is similar to Vim's set wildmode=longest:full:list,full
这个设置类似于 Vim 的set wildmode=longest:full:list,full
I pulled these settings from this question on the Unix & Linux site.我从 Unix & Linux 站点上的这个问题中提取了这些设置。
By the way, since you are here, here is another nice pair of bindings:顺便说一句,既然你在这里,这里还有一对不错的绑定:
# Cycle through history based on characters already typed on the line
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
This means if you type ssh<Up>
it will cycle through previous lines where you ran ssh
这意味着如果您键入ssh<Up>
它将循环遍历您运行ssh
前几行
If you don't like what you got, you can clear the line with Ctrl-K Ctrl-U如果你不喜欢你得到的东西,你可以用Ctrl-K Ctrl-U清除该行
I pulled these settings from this question on AskUbuntu.我从 AskUbuntu 上的这个问题中提取了这些设置。
On top of在之上
# cycle forward
Control-k: menu-complete
# cycle backward
Control-j: menu-complete-backward
you may also consider adding你也可以考虑添加
# display one column with matches
set completion-display-width 1
This way you would preserve the current Tab functionality and make bash display the possibilities in one column.通过这种方式,您将保留当前的 Tab 功能并使 bash 在一列中显示可能性。 So instead of所以代替
file_12340983421 file_12390983421 file_12390986421 file_12391983421
you would get你会得到
file_12340983421
file_12390983421
file_12390986421
file_12391983421
PS You can get up to date readline
library from this The GNU Readline Library website. PS 您可以从这个The GNU Readline Library网站获取最新的readline
库。
Thanks to @sth I found what works best for me:感谢@sth,我找到了最适合我的方法:
To keep normal bash tab completion, and then use ctl-f to cycle through when needed using menu-complete保持正常的 bash 选项卡完成,然后使用 ctl-f 在需要时使用 menu-complete 循环
put this in your .inputrc file:把它放在你的 .inputrc 文件中:
"\C-f": menu-complete
In my experience, the solution provided in sth's answer has never completely worked for me.根据我的经验,某人的答案中提供的解决方案从未完全适合我。 TL;DR : Add set -o vi
to your ~/.bashrc
. TL;DR :将set -o vi
添加到您的~/.bashrc
。
When using menu-complete in conjunction with vi keybindings, I have to make sure that my ~/.bashrc
has:将 menu-complete 与 vi 键绑定结合使用时,我必须确保我的~/.bashrc
具有:
set -o vi
It's never been enough for my ~/.inputrc
just to have:我的~/.inputrc
仅仅拥有:
TAB: menu-complete
set editing-mode vi
set keymap vi
My guess is that somehow set editing-mode
and set keymap
are clobbering the TAB: ...
setting, but I haven't looked into the documentation thoroughly to figure out why this is the case.我的猜测是以某种方式set editing-mode
和set keymap
正在破坏TAB: ...
设置,但我还没有彻底查看文档以弄清楚为什么会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.