Linux 是否可以选择向上箭头+回车来运行上一个命令?

Linux 是否可以选择向上箭头+回车来运行上一个命令?,linux,unix,shell,Linux,Unix,Shell,有时,我必须连续多次运行命令,例如,查看服务是否已启动,将手从正常的打字位置移开,反复按向上箭头和输入键会变得很乏味。有没有一种方法可以在不使用向上箭头和Enter键的情况下运行上一个命令,或者使用复杂的shell脚本 我尝试了以下方法,但并不令人满意,因为它无法执行别名,而且速度有点慢 history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt sleep .01 chmod 777 prev_command

有时,我必须连续多次运行命令,例如,查看服务是否已启动,将手从正常的打字位置移开,反复按向上箭头和输入键会变得很乏味。有没有一种方法可以在不使用向上箭头和Enter键的情况下运行上一个命令,或者使用复杂的shell脚本

我尝试了以下方法,但并不令人满意,因为它无法执行别名,而且速度有点慢

history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt
理想情况下,我应该有一个这个脚本的别名,这样我就可以在命令行中键入类似prev的内容,然后按Enter键再次运行上一个命令。

在bash中,您可以按ctrlp键转到上一个命令-这比必须移动到箭头键要好得多


另请参见:

根据您使用的终端,我知道很多情况下使用F3作为重复选项,但这仍然超出了正常的键入范围,除非您有一个带有更易访问的功能键的特殊键盘

我的键盘使功能键很容易访问,但我在unix中不再做很多命令行工作,因此我无法确定这是否仍然可行。

使用

运行上一个命令

sudo !!

对于记录,它也可以工作。

与其连续多次运行同一个命令,为什么不改为运行它呢?watch将重复运行指定的命令,并在stdout中显示输出,以便您可以看到它随时间的变化


watchcommand

您是emacs用户还是vi用户?你可以用

set -o vi 
set -o emacs
设置emacs或vi键绑定。然后可以在bash中使用emacs或vi键绑定。我不知道这是否适用于其他外壳。我相信vi模式是在插入模式下启动的,所以你需要按esc键才能进入命令模式。在默认的emacs模式下,可以使用ctrl+p,然后使用ctrl+j移动到上一行并执行回车


否则,你可以使用!!正如其他人所建议的。

我经常使用bash中的历史扩展功能,该功能通常由cntlR激活-它通过交互方式在历史中搜索上一个最接近的匹配项

请参阅bash手册部分,以及bash中的。

$ help fc fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command] Display or execute commands from the history list. fc is used to list or edit and re-execute commands from the history list. FIRST and LAST can be numbers specifying the range, or FIRST can be a string, which means the most recent command beginning with that string. Options: -e ENAME select which editor to use. Default is FCEDIT, then EDITOR, then vi -l list lines instead of editing -n omit line numbers when listing -r reverse the order of the lines (newest listed first) With the `fc -s [pat=rep ...] [command]' format, COMMAND is re-executed after the substitution OLD=NEW is performed. A useful alias to use with this is r='fc -s', so that typing `r cc' runs the last command beginning with `cc' and typing `r' re-executes the last command. Exit Status: Returns success or status of executed command; non-zero if an error occurs.
注意别名r的建议;我经常使用它。

所以键入prev比up+enter快?应该移到superuser.com有办法放!!还是将下面答案中的ctrl-p转换为别名?我试过别名l=“!!”,但这让我大吃一惊:!!:我运行命令时未找到错误。如果您碰巧做得太过分,请按ctrl+n循环返回到下一个命令。