Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to allow spaces in bash bang pattern (repeating last command)

Tags:

bash

repeat

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.

like image 612
thor Avatar asked Jan 17 '14 20:01

thor


People also ask

How do you repeat the last command in bash?

Just press the Ctrl and P keys together to fill the prompt with the last executed command and you are ready to go. This method works in bash perfectly even after closing the terminal, but it might not work in zsh after closing the session.

Which command is used to repeat the last command?

To repeat something simple, such as a paste operation, press Ctrl+Y or F4 (If F4 doesn't seem to work, you may need to press the F-Lock key or Fn Key, then F4). If you prefer to use the mouse, click Repeat on the Quick Access Toolbar.

Which key is used to reuse the previous command?

For example, most of us know that we can press Ctrl-p (or the Up-Arrow key) to recall the last command so that we can modify it and press Enter to execute it.


2 Answers

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
like image 56
Digital Trauma Avatar answered Oct 13 '22 03:10

Digital Trauma


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

like image 34
glenn jackman Avatar answered Oct 13 '22 02:10

glenn jackman