Answers:
确保您的历史记录已启用。您可以通过运行以下命令检查当前状态:
set -o
输出应包含(请注意以下history on
行):
histexpand on
history on
ignoreeof off
如果未启用此功能,则需要运行set -o history
。要使此更改持久化,您需要将其附加到~/.bashrc
:
set -o history
如果要运行上一个命令,则也可以运行下一个命令:
!!
从Bash手册页:
Event Designators
An event designator is a reference to a command line entry in the history list.
! Start a history substitution, except when followed by a blank, newline,
carriage return, = or ( (when the extglob shell option is
enabled using the shopt builtin).
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for `!-1'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ?
may be omitted if string is followed immediately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with
string2. Equivalent to ``!!:s/string1/string2/'' (see Modifiers below).
!# The entire command line typed so far.
如果您使用的是Bash,则还可以使用默认的快捷方式浏览历史记录:
Ctrl+ N:下一条命令
用于操作历史记录的命令历史记录(Cp)从历史记录列表中获取先前的命令,然后在列表中移回。next-history(Cn)从历史记录列表中获取下一个命令,在列表中向前移动。
/etc/inputrc
或~/.inputrc
?另外,您使用的终端中的向上和向下箭头会产生什么转义序列?要进行检查,请运行cat
,然后在空白行上,按上下箭头,然后按Ctrl + C退出cat。它在这里产生^[[A^[[B
。
在终端中输入:
gedit ~/.inputrc
然后复制粘贴并保存:
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
从现在开始,您可以在终端中进行增量搜索,找到上一个命令所需要做的就是输入前两个或三个字母,向上箭头会迅速带您到那里。