0

I have noticed that using zsh the history command is not available when executed from Emacs via shell-command.

history | grep runserver results in zsh:fc:1: no such event: 1.

I also use the histdb command from https://github.com/larkery/zsh-histdb and although the command is available from zsh it can't be called from the Emacs.

histdb --limit 2000 | grep runserver outputs zsh:1: command not found: histdb

Obviously there is core aspect of shells I don't understand.

What is needed to fix these?

This phenomenon does not exist solely in Emacs. When I run a remote ssh command eg ssh admin@example.com 'histdb --limit 2000' the same error message appears..

vfclists
  • 7,531
  • 14
  • 53
  • 79
  • 1
    I don't have a solid answer, but I suspect it will be something to do with emacs preferring some other shell, or possibly even having it's own built-in interpreter. I'm not familiar with emacs, but I guess you could try things like checking the shell's environment (run env) and look for the SHELL variable in there. Another possibility (I'm not familiar with zsh either) is that history is only enabled for interactive sessions (otherwise every time you run a script, all the commands in the script would be registered in the history.) – cryptarch Jan 14 '23 at 02:27
  • 1
    I suspect it's because in both cases, zsh is run as a non-interactive shell – steeldriver Jan 14 '23 at 12:14
  • @steeldriver How then do you get the invocation of zsh to be an interactive shell? – vfclists Jan 14 '23 at 13:06
  • 2
    @vfclists I think a better solution would be to put the function where it gets loaded for both interactive and non-interactive invocations (~/.zshenv I thnk?) - see for example What should/shouldn't go in .zshenv, .zshrc, .zlogin, .zprofile, .zlogout? – steeldriver Jan 14 '23 at 13:13

1 Answers1

2

My zsh doesn't have a histdb command, so I have to assume it really is a function rather than a builtin.

Shell functions are loaded by sourcing scripts, typically either your personal init scripts or system level init scripts. There are multiple such scripts, and they don't all run in all situations, so if the function is defined in the wrong script, it might not be available in certain conditions.

Additionally, the init scripts can detect the environment the shell is running in and conditionally load parts. Maybe for some reason, when you run zsh in emacs it isn't loading all the functions you get when running outside of emacs.

To be sure, we would have to know what file the function is being loaded from, and if there is any conditional code previous to the definition in that file.

user10489
  • 6,740
  • histdb is not builtin is from the https://github.com/larkery/zsh-histdb repo as mentioned in the question. – vfclists Jan 14 '23 at 13:07