有没有办法在bash bang模式中允许空格(重复最后一个命令)

时间:2014-01-17 20:02:14

标签: bash repeat

我想知道是否有办法重复使用以空格分隔的子命令的bash命令。例如,如果我输入几个命令,

git add a.txt
git status
... other commands starting with git
git commit -m ""

并且做:

!GIT中

我将再次运行最后一个git commit命令。我的问题是,有没有办法重复包含空格的最后一个命令,例如重复上一个git status命令?

我试过了,

!git\ s
!"git s"

,但没有效果。

4 个答案:

答案 0 :(得分:6)

尝试:

!?git s

“!? string ”事件指示符搜索most recent command preceding the current position in the history list containing string

你也可以使用它来引用命令n行:

!-n

答案 1 :(得分:2)

在这种情况下,你可以点击 Ctrl R ,输入“git s”然后点击 Enter

答案 2 :(得分:2)

虽然相关,但没有完全回答你的问题......

尝试将以下两行添加到~/.bashrc

bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

功能性(在source ~/.bashrc之后或打开新终端):

  • 当您开始输入“git”并点击向上或向下箭头时,它将搜索您的历史记录完成情况以完成已在线上的内容。
  • Reference

答案 3 :(得分:0)

您还可以使用fc bash builtin command

$ git status
fatal: Not a git repository (or any of the parent directories): .git
$ git diff
usage: git diff [--no-index] <path> <path>
$ fc -s 'git s'
git status
fatal: Not a git repository (or any of the parent directories): .git
$