I am having to to rewrite history expansion commands, instead of calling it from history.

For Example, I have to change 35 to 36, 37, 38.... in the following command.

$ print -P '\033[35mThis is the same red as in your solarized palette\033[0m'
$ !!:gs/35/36

Now I need to make it !!:gs/36/37

However, when I use the Up key. It does not show $ !!:gs/35/36. It shows print -P '\033[35mThis is the same red as in your solarized palette\033[0m'

What can be done here?

  • 2
    This is the "expansion" part of history expansion: the !! invocation is expanded before it gets added to history. I don't think there's anything you can do, but I might be wrong. Commented Jun 25, 2020 at 16:09
  • 1
    print -P is zsh-specific but you're tagging with both bash and zsh, and because bash is a more popular tag than zsh, the title of the page ends up being bash - View History.... Do you want a solution for zsh, bash or both? Commented Jun 25, 2020 at 18:37
  • @StéphaneChazelas I actually need a zsh solution. However, if I get a bash solution then In-Shah-Allah I will be able to make it work in zsh. Commented Jun 26, 2020 at 5:10

2 Answers 2


I have two suggestions how you can approach what you want (referring to bash only):

add it to the history

Before typing the first history expansion command line you can disable history expansion (set +H) and "execute" the history expansion command (and then reenable with set -H). It then is part of the shell history and you can easily get back to it and modify it.

A more direct approach for getting the history expansion command line in the shell history would be history -s. The earlier suggestion may be easier to remember (and may be easier in case of complicated quoting), though (depending on how familiar someone is with shell options).

readline yank

This is most useful when you do not need yanking during the whole operation.

Type the history expansion command line but do not press Enter. Instead go to the beginning / end of the line and delete the whole line with Ctrl-K / Ctrl-U. This puts the whole line on the kill ring. You can restore the line with Ctrl-Y. Even after executing the command you can get it back this way as long as you do not put anything else in the kill ring. And even if: You can go back to older kill ring entries with Ctrl-Y Alt-Y.


it will show that output because the terminal think that you type the whole command example : if i type pacman -S vim i will see that error: you cannot perform this operation unless you are root.

so i will type sudo !! then before i type the password i will see a line with sudo pacman -S vim

just a screenshot

  • 1
    The OP already knows about history expansion (see !!:gs/36/37) but is instead asking if or how that command can be stored as a literal command in the history instead of the expanded version.
    – Jeff Schaller
    Commented Jun 25, 2020 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .