Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Retrieving the terminal title / setting it using the name of the foreground process



Hi,

i'm fairly new to zsh (it's been 4 days now), and i've been spending some time customizing it. one thing i haven't been able to do yet is change a terminal's title so that it contains the name of the currently active process, i.e. "zsh" if i'm simply using the shell, "perl args" if i'm running a perl program, "emacs file.txt" if i'm running emacs and "zsh" again if i hit ^Z and send emacs to the background. 

i haven't found as simple a way to do it as with the name of the current directory. using precmd or that kind of stuff probably wouldn't work, be it only because the name of the foreground app might change without my actually executing a command -- again, ^Z should also trigger a title change. i've tried using preexec, but then it gets mixed up when using e.g. "fg" to come back to a bg'd process. i also wanted to define TRAPTSTP to handle bg'ing, but i get the following error upon starting the shell: "can't trap SIGTSTP in interactive shells" (perhaps it's the wrong signal?)

anyway, another way i've thought of is to have a script (in my case it's in perl) run in the background, and every second call ps, capture the output and recognize the foreground processes in all tty's (most often i have a lot of them, that's why i need a way of differentiating them) and set the titles accordingly. some might argue that it's not very efficient, which might be right, and i welcome all suggestions for alternatives, then again i haven't found any other way. 

anyway, i still have some problems which i'm sure are solvable, but i haven't figured the solutions yet, perhaps someone can give me a hand? 

1) my title already contains information, so i need to *update* it, not simply overwrite it, i.e. i want to append the name of the current foreground process at the end...
e.g. my basic title as set by the precmd function is like this:
username@host:tty pwd ()
and i want the space between the brackets to be filled with the name of the fg process. 

i have thought of two possible ways to do that, both of which i dunno how to implement: 
a) upon creating the title in the precmd function, store it in a cache or something, which my perl script has access to. however, i dunno how to expand the cryptic expression "%n@%m:%l %~ ()" into a nice title from within the function... as i don't believe it's possible to access the environment of any session from a given terminal to retrieve the environment variables like $PWD, i have to use the value generated within the precmd function and extend it. anyone knows how to do that? 
b) retrieve the title of a given tty. the good part is then i wouldn't have to use a cache to store the titles of several tty's. the bad part is i dunno if it's possible. 

2) i can't remember how it's possible to "remotely" (i.e. from another terminal) change a terminal's title. i'm pretty sure i've read somewhere that this can be done by doing something like sending the "usual string" "\e]0;nice title\a" into /dev/ttyN, but that doesn't seem to work -- if i try to do that on the console, i get an error:
> echo "\e]0;abc\a" > /dev/tty5
zsh: keine Berechtigung: /dev/tty5
>
(yeah, sorry, it's german, it means "not authorized" or something like that)
anyone knows how to do that? 
if it's not possible to do that remotely, then there's always the possibility of having one perl script run in each terminal -- not nice, but that would work...

it'd be real cool if someone could give me some hints as to how i can get this on track. thx a lot in advance for your help! 

cheers

Enny



      



Messages sorted by: Reverse Date, Date, Thread, Author