问题
man bash
describes a very useful Event Designator
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to ''!!:s/string1/string2/''
Is there a way to execute !!:gs/string1/string2/
when typing in @string1@string2@
on the command line to replace all occurrences in the previous command? (@
or any other designated character/string)
回答1:
^string1^string2^:g&
See question Replace all occurrences of a word in the last command.
See Modifiers in History Expansion.
回答2:
Shortly: no!
In fact, there may exist a way, using `trap "..." debug'...
Something like:
trap 'if [[ $BASH_COMMAND =~ ^@(.*)@(.*)@$ ]] ;then
BASH_LAST=${BASH_LAST//${BASH_REMATCH[1]}/${BASH_REMATCH[2]}};
$BASH_LAST;
unset BASH_COMMAND;
else BASH_LAST=$BASH_COMMAND;
fi;
' debug
This is quick and dirty, there left an execution error, but I think: It may be a way to do it.
Edit 01
This is a little better, but history stay quite wrong:
shopt -s extdebug
trap '
if [[ $BASH_COMMAND =~ ^@(.*)@(.*)@$ ]] ;then
BASH_LAST="${BASH_LAST//${BASH_REMATCH[1]}/${BASH_REMATCH[2]}}"
$BASH_LAST
false
else
BASH_LAST="$BASH_COMMAND"
fi' debug
But warn: I do this for fun, for playing with bash and to understand how it work... This is not intended to be used in effective final solution!!
来源:https://stackoverflow.com/questions/16284042/shortcut-to-replace-all-strings-in-previous-bash-command