0

I run two different terminal emulators with different zsh prompts depending on whether I am in one or the other (with the second being the "default"):

TERM_EMU=$(ps --pid $(ps --pid $$ -o ppid=) -o comm=)
if [ $TERM_EMU = 'term1' ]; then
    PS1='term1> '
else
    PS1='term2> '
fi

(Where I have taken the terminal emulator name finding command from this question)

However, I also use nnn for file navigation and frequently spawn its subshells. Whenever I enter an nnn subshell, the process id of the terminal emulator found with the ps command becomes nnn, and the shell switches to the "default" prompt. I want to sync the subshell prompts with my main prompt setting.

My first idea was to check if I'm at zero subshell depth first; this would presumably set my shell prompt to a variable that would later be referenced by the subshells:

if [ -z $NNNLVL ]; then
    TERM_EMU=$(ps --pid $(ps --pid $$ -o ppid=) -o comm=)
    if [ $TERM_EMU = 'term1' ]; then
        PS1='term1> '
    else
        PS1='term2> '
    fi
else
    PS1="($NNNLVL) $PS1"
fi

This doesn't work; instead, the subshell prompt becomes

(<level>) <hostname>% 

which is not at all what I want; apparently the PS1 variable is not carried over to the subshells. How do I force nnn shells to "remember" their parent terminal emulator?

  • Can't you just use $TERM? Note that the parent process of the one that executed your shell is already in $PPID. It's more the parent process of your session leader that you'd want here $(ps -o ppid= -p $(ps -o sid= -p $$)) – Stéphane Chazelas Jun 21 '23 at 19:22
  • "Can't you just use $TERM?" - No, most terminal emulators share that variable. In fact this is just a working example, in my actual script I also customize TERM for a specific terminal emulator to achieve the effects I want. – Andrii Kozytskyi Jun 22 '23 at 20:06
  • Also, the command you provided just gives me an error: zsh: command not found: 330219 I don't really know how ps works and can't debug that... – Andrii Kozytskyi Jun 22 '23 at 20:14
  • Yes, that's the point terminal emulators set $TERM to a value that tells applications what the terminal is so they know how to interact with it. That $(ps -o pid=....) was meant to be the pid of the parent of the session leader, not a command to run. – Stéphane Chazelas Jun 23 '23 at 06:02

0 Answers0