This is a quick tutorial on how to launch ZSH with a command already running, and drop to shell once it exits.
It can be any command, but I'm gonna use yazi - a terminal file manager - as an example.
The classic way would be zsh -c "yazi". This works, but the problem is that when you quit yazi, ZSH terminates. I want to drop to ZSH after I quit.
Another issue is that the command cannot be an alias. I have an yy alias for yazi. When I run yy, navigate to some directory, and then quit, ZSH changes directory to that one. This can't be done without an alias.
More promising method is this one:
zsh -i --nozle <<< "yazi; setopt zle"
It should drop to shell, and you could even use the yy alias.
This solution doesn't work for me though, giving me stty errors.
After some debugging, I figured out it's caused by my prompt - OhMyPosh.
I would like a solution independent on whatever is in my .zshrc - be it prompt or whatever else.
Solution #
To the final solution then...
Add this to the end of your .zshrc:
unset ZSH_RUN_TMP
export ZSH_RUN_TMP="$ZSH_RUN"
unset ZSH_RUN
eval "$ZSH_RUN_TMP"
You can then launch ZSH with your new ZSH_RUN env:
ZSH_RUN="yy" zsh
It works great. yy is an alias, and changing directory as described above works as well.
The second ZSH_RUN_TMP variable fuckery is there so new shells launched from this one don't inherit your ZSH_RUN. Otherwise if you ran
ZSH_RUN="nvim" zsh
and then opened a new terminal from inside neovim, it would open a nested neovim session. Yikes.
Real use case #
I use foot as my terminal emulator. Foot can launch any shell or command like this:
foot bash
# or
foot zsh
# or
foot yazi
Well, now I have a keybind to execute this command:
ZSH_RUN="yy" foot zsh
It opens foot with yazi running, but I can exit yazi in any directory and drop to ZSH in it.
Source #
Namely this comment, but improved.
No comments yet...