1

With

!!:gs/foo/bar/

I can modify a previous bash command. Is there any way I can recall the history modification command to the prompt (rather than the result of the modification) in order to be able to modify the modification e.g. to perform a similar operation?

In other words, is there a meta-history with a list of commands such as !!:gs/foo/bar/ which I can access? I guess not, but it would be handy.

2
  • I would guess not. The command that was run is not !!:gs/foo/bar/, but rather the output of bash's substitution applied to that. Luckily the creator and developers of bash have kept globbing and variable substitutions after the addition of commands to the history, but it appears that !! does the opposite. My guess is that this is because, you are not guaranteed that your history will always contain both the original command and the unsubstituted edit. Losing the context of the original command likely makes the unsubstituted edit meaningless to anyone reviewing the history.
    – t0w0i7ne
    Aug 9 at 17:18
  • Maybe, but there is also no guarantee that !5000:gs/foo/bar/ will work either, since that event may not exist if HISTSIZE/HISTFILESIZE are too small, or if event 5000 has been explicitly deleted. The usefulness of a meta-history would not be to a reviewer, but just to someone who wants to, say, perform a number of similar substitutions.
    – loris
    Aug 10 at 6:25

1 Answer 1

2

Try the following steps. Steps 1 & 3 will not appear on command-line.

  1. CTL-x (
  2. !!:gs/foo/bar/
  3. CTL-x )

Clear from command-line.

CTL-u

Recall to command-line for execution/modification.

CTL-x e

Used

echo "My foo is bar in file bar dir foo!"

for testing.

3
  • That is interesting. But after doing, say !123:gs:/foo/bar/ I want to then run !123:gs/foo/baz/. So I would have to edit the macro. I am not aware that that is possible.
    – loris
    Aug 11 at 7:51
  • 1
    Correct @loris: CTL-x ( clears the macro. Since no "meta-history" is available, this alternative will print to the prompt as requested in the subject line. You could, in step 2, use "!123:gs:/foo/bar/ !123:gs/foo/baz/" & CTL-U or CTL-K the part to remove at the prompt. I use the method for history deletion: " history -d ; history -d ; history -d ;" (3x is personally sufficient).
    – ptrck4193
    Aug 11 at 15:47
  • Sorry, you are right. Just printing to the prompt is all I need. I can then edit that.
    – loris
    Aug 12 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.