如何更改 bash 历史完成以完成已经上线的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1030182/
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
How do I change bash history completion to complete what's already on the line?
提问by blokkie
I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow:
几个月前我发现了一个命令,当按下向上箭头时,它使我的 bash 历史记录自动完成已在线的内容:
$ vim fi
Press ↑
按 ↑
$ vim file.py
I'd like to set this up on my new computer, because it saves a lot of time when keeping a big history. The problem is that I can't for the life of me remember where it was mentioned and reading through endless bash references and tutorials unfortunately didn't help either.
我想在我的新电脑上设置这个,因为它在保存大历史时节省了很多时间。问题是我终生无法记住它被提及的地方,不幸的是,阅读无休止的 bash 参考资料和教程也无济于事。
Does anybody know the command?
有人知道命令吗?
回答by ephemient
Probably something like
大概是这样的
# ~/.inputrc "\e[A": history-search-backward "\e[B": history-search-forward
or equivalently,
或等效地,
# ~/.bashrc if [[ $- == *i* ]] then bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward' fi
(the if statement checks for interactive mode)
(if 语句检查交互模式)
Normally, Up and Down are bound to the Readline functions previous-history
and next-history
respectively. I prefer to bind PgUp/PgDn to these functions, instead of displacing the normal operation of Up/Down.
通常情况下,上下绑定到的ReadLine功能previous-history
和next-history
分别。我更喜欢将 PgUp/PgDn 绑定到这些函数,而不是取代 Up/Down 的正常操作。
# ~/.inputrc "\e[5~": history-search-backward "\e[6~": history-search-forward
After you modify ~/.inputrc
, restart your shell or use Ctrl+X, Ctrl+Rto tell it to re-read ~/.inputrc
.
修改后~/.inputrc
,重新启动 shell 或使用Ctrl+ X, Ctrl+R告诉它重新读取~/.inputrc
.
By the way, if you're looking for relevant documentation:
顺便说一句,如果您正在寻找相关文档:
Bash uses The GNU Readline Libraryfor the shell prompt and history.
Bash 使用GNU Readline 库作为 shell 提示和历史记录。
回答by nate_weldon
Update .inputrc with the following:
使用以下内容更新 .inputrc:
"\C-[OA": history-search-backward
"\C-[[A": history-search-backward
"\C-[OB": history-search-forward
"\C-[[B": history-search-forward
回答by MauricioRobayo
If set enable-keypad on
is in your ~/.inputrc
as some st (suckless simple terminal) users might, be aware that the arrows keys are in keypad mode. Ubuntu ships with this useful /usr/share/doc/bash/inputrc.arrows
:
如果set enable-keypad on
您~/.inputrc
像某些 st(简单的终端)用户一样,请注意箭头键处于键盘模式。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
So I'm not sure if you'll need all, but it might not hurt to have in your ~/.inputrc
:
所以我不确定你是否需要全部,但在你的~/.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
This is also on the same topic: My cursor keys do not workand also this xterm: special keys
回答by Benjamin Crouzier
With ohmyzsh, use this in your .zshrc:
随着ohmyzsh,在您使用本.zshrc:
bindkey '^[OA' history-search-backward
bindkey '^[OB' history-search-forward
To reload, source ~/.zshrc
or relaunch terminal.
重新加载source ~/.zshrc
或重新启动终端。
回答by Convict
You may need to enabled bash completion.
您可能需要启用 bash 完成。
Check
查看
/etc/profile
/etc/bash.bashrc
~/.bashrc
/etc/profile
/etc/bash.bashrc
~/.bashrc
to see if any of the above files source /etc/bash_completion
. i.e.
看看是否有上述文件来源/etc/bash_completion
。IE
. /etc/bash_completion
If /etc/bash___completion
is not sourced by any of the above files you will need to add it to one of them.
如果/etc/bash___completion
不是上述任何文件的来源,则需要将其添加到其中一个文件中。
If you want all bash users on your machine to have bash completion, source /etc/bash_completion
from /etc/bash.bashrc
.
如果您希望计算机上的所有 bash 用户都具有 bash 完成功能,请/etc/bash_completion
从/etc/bash.bashrc
.
If it's just you who wants bash completion, source /etc/bash_completion
from your ~/.bashrc
.
如果只是您想要 bash 完成,请/etc/bash_completion
从您的~/.bashrc
.