【问题标题】:How do I change bash history completion to complete what's already on the line?如何更改 bash 历史完成以完成已经上线的内容?
【发布时间】:2010-11-05 00:52:16
【问题描述】:

几个月前我发现了一个命令,当按下向上箭头时,我的 bash 历史记录自动完成在线上的内容:

$ vim fi

$ vim file.py

我想在我的新计算机上进行设置,因为它在保存大量历史记录时可以节省大量时间。问题是我一生都记不起它在哪里被提及,不幸的是,阅读无休止的 bash 参考资料和教程也无济于事。

有人知道这个命令吗?

【问题讨论】:

    标签: linux bash shell


    【解决方案1】:

    使用 ohmyzsh,在您的 .zshrc 中使用它:

    bindkey '\e[A' history-search-backward
    bindkey '\e[B' history-search-forward
    

    要重新加载,source ~/.zshrc 或重新启动终端。

    来源:https://superuser.com/a/418299/71680

    【讨论】:

    • 在 zsh 上对我有用的是源链接中给出的选项: bindkey '\e[A' history-search-backward bindkey '\e[B' history-search-forward
    【解决方案2】:

    如果 set enable-keypad on 像某些 st (suckless simple terminal) 用户一样在您的 ~/.inputrc 中,请注意箭头键处于键盘模式。 Ubuntu 附带了这个有用的/usr/share/doc/bash/inputrc.arrows

    # This file controls the behaviour of line input editing for
    # programs that use the Gnu Readline library.
    #
    # Arrow keys in keypad mode
    #
    "\C-[OD"        backward-char
    "\C-[OC"        forward-char
    "\C-[OA"        previous-history
    "\C-[OB"        next-history
    #
    # Arrow keys in ANSI mode
    #
    "\C-[[D"        backward-char
    "\C-[[C"        forward-char
    "\C-[[A"        previous-history
    "\C-[[B"        next-history
    #
    # Arrow keys in 8 bit keypad mode
    #
    "\C-M-OD"       backward-char
    "\C-M-OC"       forward-char
    "\C-M-OA"       previous-history
    "\C-M-OB"       next-history
    #
    # Arrow keys in 8 bit ANSI mode
    #
    "\C-M-[D"       backward-char
    "\C-M-[C"       forward-char
    "\C-M-[A"       previous-history
    "\C-M-[B"       next-history
    

    所以我不确定你是否需要全部,但在你的~/.inputrc 中拥有可能不会有什么坏处:

    # Arrow keys in keypad mode
    "\C-[OA": history-search-backward
    "\C-[OB": history-search-forward
    "\C-[OC": forward-char
    "\C-[OD": backward-char
    
    # Arrow keys in ANSI mode
    "\C-[[A": history-search-backward
    "\C-[[B": history-search-forward
    "\C-[[C": forward-char
    "\C-[[D": backward-char
    

    这也是同一个主题:My cursor keys do not work 还有这个xterm: special keys

    【讨论】:

      【解决方案3】:

      大概是这样的

      # ~/.inputrc "\e[A": 历史搜索-向后 "\e[B": 历史搜索转发

      或等价的,

      # ~/.bashrc 如果 [[ $- == *i* ]] 然后 绑定'“\ e [A”:历史搜索向后' 绑定 '"\e[B": 历史搜索转发' 菲

      (if 语句检查交互模式)

      通常,Up 和 Down 分别绑定到 Readline 函数 previous-historynext-history。我更喜欢将 PgUp/PgDn 绑定到这些函数,而不是取代 Up/Down 的正常操作。

      # ~/.inputrc "\e[5~": 历史搜索-向后 "\e[6~": 历史搜索-转发

      修改~/.inputrc后,重启你的shell或者使用Ctrl+X,Ctrl+R告诉它重新阅读~/.inputrc


      顺便说一句,如果您正在寻找相关文档:

      Bash 使用 The GNU Readline Library 作为 shell 提示符和历史记录。

      【讨论】:

      • 感谢您的回答。这听起来很熟悉,但不幸的是对我不起作用。将它放在 .inputrc 中不会改变任何东西并将它放在 .bashrc 中会阻止箭头做任何事情。有什么想法吗?
      • 没关系。 history-complete-* 不存在;只有 history-search-* 函数存在。谢谢!
      • 啊,我显然打错了前两个例子……不过最后一个是正确的。我会解决的。
      • @user1037114 :这本身就是一个问题,您应该在自己的帖子中提问。
      • 这为我在 bash 中使用 vi 模式修复了向上/向下箭头。请注意,我必须点击“Esc”(命令模式)才能使其工作。
      【解决方案4】:

      使用以下内容更新 .inputrc:

      "\C-[OA": history-search-backward
      "\C-[[A": history-search-backward
      
      "\C-[OB": history-search-forward
      "\C-[[B": history-search-forward
      

      【讨论】:

      • 我不知道这些键具体是什么,但是当在 X11 上使用 tmux 和 st 终端时向上和向下键没有进行历史搜索时,这个答案为我修复了这个功能。跨度>
      • @razzintown for st 你可能有set enable-keypad ondel 键才能正常工作(faq)。当键盘位于箭头键上时,是"\C-[OA""\C-[OB"(以及"\C-[OC""\C-[OD")。我在下面发布了一个答案来澄清这一点。
      • @Chris 这些是键盘模式的向上和向下箭头键,以及 ANSI 模式的向上和向下箭头键。请参阅下面的答案。
      【解决方案5】:

      您可能需要启用 bash 补全。

      检查

      • /etc/profile
      • /etc/bash.bashrc
      • ~/.bashrc

      查看以上文件是否来自/etc/bash_completion。即

      . /etc/bash_completion

      如果/etc/bash___completion 不是上述任何文件的来源,您需要将其添加到其中一个文件中。

      如果您希望计算机上的所有 bash 用户都完成 bash,请从 /etc/bash.bashrc 获取 /etc/bash_completion

      如果只有您想要完成 bash,请从您的 ~/.bashrc 获取 /etc/bash_completion

      【讨论】:

      • 制表符补全很好,但与历史回忆不同,这是原始发布者所要求的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2018-10-17
      • 1970-01-01
      相关资源
      最近更新 更多