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.
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.
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.
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.
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
In this case, you could hit CtrlR , type "git s" then hit Enter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With