3

The r built-in utility in the zsh shell, if used with no options, re-executes the most recent command in the command history.

The issue with this is that if you have a number of shell sessions sharing history (the SHARE_HISTORY shell option is set), it's fairly easy to make the mistake of executing commands from a "non-local" shell session without intending to do so. This makes using r potentially dangerous.

Question: How may one either change the behavior of r to only consider history events local to the current shell session, or write a separate alias or function, rl, that only considers local events?

Since r is supposed to be the same as fc -e - (recall the most recent command, don't edit it, then execute it), I thought maybe using r -L or fc -e - -L would do it, but for whatever reason, it only seems to work if the most recent global history event is from the current shell session. If this is not the case, the shell complains with an fc: no matching events found error.

zsh version 5.8 (zsh-5.8-0-g77d203f) is in use here, from the zsh-5.8p0 package on OpenBSD 6.8.

Tangentially related question:

2 Answers 2

1

You will need to use the set-local-history widget for this:

bindkey '^[r' set-local-history

With this, you can press AltR to toggle between using local or global history for commands like r and fc. If you don't recall whether you are in local or global history mode, press Alt0 + AltR to force global history mode or Alt1 + AltR to force local history mode (presuming that you're using the default emacs keymap).

Why rc -L or fc -e - -L don't work for your case, I don't know. It sounds like a bug to me. You can try asking about it on the Zsh mailing list to see if the devs also think it's a bug or whether this is somehow intentional.

0

The fact that r -L was not working as expected was recognized as a bug when I asked about it on the zsh-users mailing list.

The bug was corrected on the 18th of February 2021, on the shell's Git development branch:

commit 6bef719302d6db33c63fb6f2636986dff1941ac2
Author: Peter Stephenson <retracted>
Date:   Thu Feb 18 21:37:08 2021 +0000

    users/26509: fix for r -L

    fc with the -L option should ignore remote entires, rather than
    reading them and treating them as an error.

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.