$ !! # repeat previous command
Adding a colon (:) and then a letter or two will modify the command before running it. A useful modifier is "p" for printing. That is:
$ !!:p # repeat previous command, but just :p-print it
This is useful because you can use up-arrow to now go to the previous command and edit it interactively.
For non-interactive editing, you can do global search and replace! Example: use the "repeat previous command" command, bangbang (!!). Then modify it (:), then say "global search" (gs). To do this to find "one" and replace it with "two", use this command:
$ !!:gs/one/two
In my real-world case, I'd already run a command to deploy my Development server with Terraform. The specific command is:
AWS_PROFILE=development terraform plan -var-file=../config/development.tfvars
This command was in my shell history. I want to recall this command (bangbang, aka !!), then search and replace "development" with "staging" to use Terraform to deploy to my staging environment.
Command:
$ !!:gs/development/staging
Got me:
AWS_PROFILE=staging terraform plan -var-file=../config/staging.tfvars
Resources:
- Bash scripting cheatsheet