2

Every now and then I want to recycle a command sequence I recently used, after I adapted it.

Let's imagine, yesterday I executed

foo 42
bar with some strange arguments
baz /my/most/beloved/folder

Now today I need to something similar like

foo -x 42
bar with some more strange arguments
baz /my/most/hated/folder

I can use ctrlR and foo to find yesterday's command (maybe multiple times, because I use foo in different contexts), change it and execute it. No problem so far. But now I'd love to quickly jump to the following line in history without repeating the lengthy search again (in reality often more than ten commands).

The fc command of ksh comes close to what I want, but it is not interactive: If I issue a dozen git commands I may need to react to one of them responding other than expected (or executing the following commands will cause a mess).

The cmd shell of MS Windows has a (for me usually annoying) behaviour: After executing a command from history, the up arrow doesn't take me to the last command in history, but to the last executed command, so after executing the foo line again, arrow up and then arrow down would take me to the bar line. This exact behaviour would not help me, because it only work when executing an unmodified line. If I modify it, the »history pointer« is set to the end again. But it would be the perfect solution to have such a bookmark in history, set it at the foo line and have a key combination to take me back there or even better to the following line, moving the bookmark.

I guess I'm not the only one with such a need, but I did not find any solution to it. Currently I solve it by printing the relevant part of history and use the mouse to paste the line I need next, but this is clumsy and I don't ilke to switch between keyboard and mouse. And please get away with history expansion; I typically need to interactively edit the line, including tab expansion.

Do you know any solution, preferably for zsh?

5
  • fc <histroy-number-1> <history-number-2> in zsh opens that range of commands in the history in an editor. You can then edit, save and quit, at which point it will execute the edited commands. Is that close to what you want?
    – muru
    Jul 13, 2023 at 6:43
  • @muru As written, fc gets close to it, but is not interactive, which is a problem for many cases. I can't use tab-completion and I can't stop following commands if something went wrong.
    – Philippos
    Jul 13, 2023 at 6:57
  • 1
    Add a ; read to the end of each line (easy to do in an editor) and then you'll have to press Enter to continue after each command, and pressing Ctrl-C instead would cancel and stop progressing to the next command
    – muru
    Jul 13, 2023 at 7:03
  • @muru Thank you, that solves the worst problem of the fc solution. Feel free to add this as an answer. I'll accept if no one can give something closer to my needs on the silver platter.
    – Philippos
    Jul 13, 2023 at 7:54
  • If this pattern happens to you often enough and the length of commands to repeat is long enough it sure sounds like you want to be scripting it. Start with `history > tmp.sh' and regex/sed/etc from there.
    – ckhan
    Jul 13, 2023 at 9:29

1 Answer 1

3

Foolish me! I got the answer elsewhere. What I described as perfect solution seems to exist since long ago, I simply always oversaw it and didn't find the right search terms:

ctrlO executes the current command and brings you to the following line from the history.

What a great feature. Gladly, of more than twenty people reading the request did not know it either.

You must log in to answer this question.

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