0

Calling zle set-local-history 1 from .zshrc causes

widgets can only be called when ZLE is active

Is there a way to execute the widget on startup, or maybe a setting for this particular case?


upd: I checked zsh source code, and I believe that hist_skip_flags is set only by set-local-history widget, so there is no setting to change this behavior. However it is much harder to determinate if it's possible to run zle widget on startup.

magras
  • 101
  • "The zsh line editor is usually abbreviated to `zle'. Normally it fires itself up for any interative shell; you don't have to do anything special until you decide you need to change its behaviour. If everything looks OK and you're not interested in how zle is started up, skip to the next subsection. " please read A User's Guide to the Z-Shell for more info – DavidPostill Feb 15 '23 at 13:39
  • @DavidPostill, could you expand on your point? Are you implying that the the problem is that zle not loaded during execution of zshrc? I don't think so, because bindkey can be used in zshrc, which according to the guide should mean that zle is loaded. – magras Feb 15 '23 at 14:16
  • @DavidPostill, I've seen this and some other related answers, they don't answer my question about changing the initial state. It is easy to show that ZLE_STATE is persisting in between widget calls and is global (changes to it are visible to other widgets), so it is possible to switch between local and global history at runtime without wrapping and rebinding all functions that access history. – magras Feb 15 '23 at 15:54
  • Sorry, I'm no expert on this and have no more advice. Hopefully someone else will be able to help. – DavidPostill Feb 15 '23 at 16:29
  • What is your goal? Your question might be a XY problem?! – mpy Feb 15 '23 at 17:31
  • @mpy, I'm experimenting right now and want to switch between local and global history at will, but having global history by default is too disruptive for me. – magras Feb 15 '23 at 18:18
  • You can get a zle-command prompt with the default keybinding ALT+X, where you can execute set-local-history which will toggle between global and local history. If you want to have the local state by default, IMHO you have to redefine the widgets you are using to access the history, as it is explained in the Q&A which DavidPostill linked above. – mpy Feb 15 '23 at 18:28
  • 1
    Does setting it in a zle-line-init hook work for you? – okapi Mar 18 '23 at 01:03
  • @okapi, kind of. It is executed before each user input request. To achieve what I want I have to use an environment variable to set local history only on the first execution of the widget. But I like this solution more than rebinding every function that touches history. – magras Mar 18 '23 at 13:37

1 Answers1

0

Solution based on the comment by @okapi:

zle-line-init() {
  if [[ "$ZLE_LOCAL_HISTORY_SET" != "1" ]]; then
    ZLE_LOCAL_HISTORY_SET=1
    zle set-local-history 1
  fi
}
zle -N zle-line-init

zle-line-init executed on every user input request, so additional environment variable required to set local history only on the first run.

magras
  • 101