我怎样才能让bash标签补全像vim标签补全一样运行,并循环匹配匹配?

vjrehmav  于 2022-11-24  发布在  其他
关注(0)|答案(5)|浏览(129)

多年来,我一直想找到一个解决办法。
由于这个原因,我在Vim中操作文件的效率要比在bash中高得多。
如果我有

file_12390983421
file_12391983421
file_12340983421
file_12390986421

在bash中,键入file_1-〉tab,它显然列出了:

file_12390983421 file_12391983421 file_12340983421 file_12390986421

这是一个可怕的无聊和痛苦的工作。
vim中的相同序列将一次循环一个文件。
请有人告诉我如何在bash中做到这一点,或者如果有另一个shell可以做到这一点,我明天会切换。

watbbzwu

watbbzwu1#

默认情况下,TAB绑定到complete readline命令。您所需的行为将改为menu-complete。您可以通过编辑~/.inputrc来更改readlines设置。要重新绑定TAB,请添加以下行:

TAB: menu-complete

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

qxsslcnc

qxsslcnc2#

对于bash〉= 4,你可能会喜欢这些设置,你可以直接在命令行中尝试,如果你喜欢的话,可以把它们放到你的~/.bash_profile中。

# 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 (common) completion on the first Tab press, only start
# cycling full results on the second Tab press (from bash version 5)
bind "set menu-complete-display-prefix on"

此设置类似于Vim的set wildmode=longest:full:list,full
我从Unix & Linux站点上的this question中提取了这些设置。
顺便说一下,既然你在这里,这里有一些其他伟大的绑定:

# Cycle through history based on characters already typed on the line
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

# Keep Ctrl-Left and Ctrl-Right working when the above are used
bind '"\e[1;5C":forward-word'
bind '"\e[1;5D":backward-word'

这意味着,如果键入ssh<Up>,它将循环显示以前运行ssh的行
如果你不喜欢你得到的,你可以用Ctrl-KCtrl-U清除这行
我从AskUbuntu上的this question中提取了这些设置。

f3temu5u

f3temu5u3#

# cycle forward
Control-k: menu-complete
# cycle backward
Control-j: menu-complete-backward

您也可以考虑添加

# display one column with matches
set completion-display-width 1

这样,您将保留当前的Tab功能,并使bash在一列中显示可能的选项。

file_12340983421 file_12390983421 file_12390986421 file_12391983421

你会得到

file_12340983421
file_12390983421
file_12390986421
file_12391983421

你可以从这个The GNU Readline Library网站获得最新的readline库。

woobm2wo

woobm2wo4#

多亏了“某事”,我找到了最适合我的方法:
保持正常的bash选项卡完成,然后在需要时使用ctl-f循环使用menu-complete
将以下内容放入.inputrc文件中:

"\C-f": menu-complete
fwzugrvs

fwzugrvs5#

根据我的经验,在某事的答案中提供的解决方案从来没有对我完全有效过。DR**:将set -o vi添加到您的~/.bashrc
当结合vi键绑定使用menu-complete时,我必须确保我的~/.bashrc具有:

set -o vi

对于我的~/.inputrc来说,仅仅拥有以下内容是远远不够的:

TAB: menu-complete

set editing-mode vi
set keymap vi

我的猜测是,set editing-modeset keymap不知怎么地破坏了TAB: ...的设置,但我还没有彻底研究文档来弄清楚为什么会出现这种情况。

相关问题