如何使 bash 选项卡完成的行为类似于 vim 选项卡完成并循环匹配匹配项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7179642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:40:00  来源:igfitidea点击:

How can I make bash tab completion behave like vim tab completion and cycle through matching matches?

bashshellvim

提问by pixelearth

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 sth

By default TABis bound to the completereadline command. Your desired behavior would be menu-completeinstead. You can change your readlines settings by editing ~/.inputrc. To rebind TAB, add this line:

默认TAB绑定到completereadline 命令。你想要的行为将是menu-complete。您可以通过编辑~/.inputrc. 要重新绑定TAB,请添加以下行:

TAB: menu-complete

For more details see the READLINEsection in man bash.

有关更多详细信息,请参阅 中的READLINE部分man bash

回答by joeytwiddle

For bash >= 4 you might like these settings. You can try them directly on the command-line, and put them in your .bashrcif you like them.

对于 bash >= 4,您可能会喜欢这些设置。您可以直接在命令行上尝试它们,.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 questionon 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-KCtrl-U

如果你不喜欢你得到的东西,你可以用 Ctrl-KCtrl-U

I pulled these settings from this questionon AskUbuntu.

我从AskUbuntu 上的这个问题中提取了这些设置。

回答by Johnny Baloney

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. So instead of

通过这种方式,您将保留当前的 ​​Tab 功能并使 bash 在一列中显示可能性。所以代替

file_12340983421 file_12390983421 file_12390986421 file_12391983421

you would get

你会得到

file_12340983421
file_12390983421
file_12390986421
file_12391983421

P.S. You can get up to date readlinelibrary from this The GNU Readline Librarywebsite.

PS 您可以readline从此The GNU Readline Library网站获取最新的库。

回答by pixelearth

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

回答by jez

In my experience, the solution provided in sth's answer has never completely worked for me. TL;DR: Add set -o vito your ~/.bashrc.

根据我的经验,某人的答案中提供的解决方案从未完全适合我。TL;DR:添加set -o vi到您的~/.bashrc.

When using menu-complete in conjunction with vi keybindings, I have to make sure that my ~/.bashrchas:

将 menu-complete 与 vi 键绑定结合使用时,我必须确保我的~/.bashrc

set -o vi

It's never been enough for my ~/.inputrcjust to have:

对我来说,~/.inputrc仅仅拥有:

TAB: menu-complete

set editing-mode vi
set keymap vi

My guess is that somehow set editing-modeand set keymapare 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: ...设置,但我还没有彻底查看文档以弄清楚为什么会这样。