Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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
$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top