7

I was wondering if there is a way to repeat a bash command with a subcommand separated by space. For example, if I enter several commands,

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

and do:

!git

I will run the last git commit command again. My questions, is there a way to repeat the last command that contains a space, e.g. to repeat the last git status command?

I tried,

!git\ s
!"git s"

, but none works.

thor
  • 21,418
  • 31
  • 87
  • 173

4 Answers4

9

Try:

!?git s

The "!?string" event designator searches for the most recent command preceding the current position in the history list containing string.

Also you could use this to refer to the command n lines back:

!-n
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
  • Thanks. I tried this on mingw/msys. It did not work. But it works on Ubuntu Linux 12.04 LTS. – thor Jan 17 '14 at 20:27
2

In this case, you could hit CtrlR , type "git s" then hit Enter

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

Though related, but not exactly answering your question ...

Try adding the following two lines to your ~/.bashrc

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

FUNCTIONALITY (after you source ~/.bashrc or open a new terminal):

  • when you start typing i.e. "git" and hit the up or down arrow, it will search through your history completion to complete what's already on the line.
  • Reference
Community
  • 1
  • 1
csiu
  • 3,159
  • 2
  • 24
  • 26
0

You can also use the 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
$
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83