使用向上箭头运行上一个命令

使用向上箭头运行上一个命令需要什么设置? 使用Mac,我可以使用向上箭头重新运行我刚刚运行的命令,但它似乎不适用于我的bash shell。

我使用8.04(由于一些编译器版本问题,我不能使用最新的发行版)。

添加

我没有改变任何东西,因为它是Mac上VMWare Fusion的全新安装。

确保您的历史记录已启用。 您可以通过运行来检查当前状态:

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 + P :上一个命令
  • Ctrl + N :下一个命令

    用于操作历史记录的命令previous-history(Cp)从历史记录列表中获取上一个命令,然后在列表中返回。 next-history(Cn)从历史列表中获取下一个命令,在列表中向前移动。

确保你实际上正在使用bash。 常见问题是使用useradd而不是adduser或用户和组(GUI)应用程序创建新用户。 对于前者,默认的shell集是/bin/sh 。 运行chshch ange sh ell)以确保它设置为/bin/bash

在终端输入:

 gedit ~/.inputrc 

然后复制粘贴并保存:

 "\e[A": history-search-backward "\e[B": history-search-forward "\e[C": forward-char "\e[D": backward-char 

从现在开始你可以在终端中进行增量搜索,只需要输入前两个或三个字母,然后向上箭头就可以快速进入。