LinuxCommandLibrary

histexpand

Reuse and expand the shell history in `sh`, `bash`, `zsh`, `rbash` and `ksh`.

TLDR

Run the previous command as root (!! is replaced by the previous command)

$ sudo !!
copy


Run a command with the last argument of the previous command
$ [command] !$
copy


Run a command with the first argument of the previous command
$ [command] !^
copy


Run the Nth command of the history
$ ![n]
copy


Run the command n lines back in the history
$ !-[n]
copy


Run the most recent command containing string
$ !?[string]?
copy


Run the previous command, replacing string1 with string2
$ ^[string1]^[string2]^
copy


Perform a history expansion, but print the command that would be run instead of actually running it
$ [!-n]:p
copy

Copied to clipboard