Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to execute a screen command when the user changes the window?

I have my current working directory in my hardstatus by using zsh's precmd() feature to change screen's working directory to zsh's working directory when a command runs. But since zsh doesn't run a command when the window changes, the working directory in my hardstatus doesn't update to the zsh instance in the new window (obviously).

My thinking is that if I can get screen to execute a :chdir when the window changes, I can have zsh storing its working directory somewhere or something along those lines and have screen read it and change to that when the window changes. (There's likely a more efficient solution, that's just what I came up with off the top of my head.)

share|improve this question
This sounds like a complicated thing. What are you trying to achieve? – tjameson Apr 5 '11 at 17:55
As stated, I want the working directory of the contained shell in my hardstatus. – Alex S Apr 5 '11 at 17:59
up vote 2 down vote accepted

I want the working directory of the contained shell in my hardstatus.

Possible solution: rewrite precmd() to instead set the Xterm title, using printf "\e]0;%s\007" "$PWD". (I like ${PWD/#$HOME/~}.)

When used inside screen, this will actually update hardstatus text.

share|improve this answer
It's working exactly as I expect currently. i.e. when I run a command my hardstatus is updated with zsh's working directory. The problem is when I change windows, the zsh instance in that window doesn't update until a command is run. – Alex S Apr 5 '11 at 20:20
@Shadow: Have you at least read the post? – grawity Apr 6 '11 at 15:02
Yes, but the problem is the fact that precmd is being used at all. precmd doesnt get run when screen switches windows. – Alex S Apr 25 '11 at 18:02
@Shadow: It isn't a problem. Screen remembers the 'titles' of every window (it's part of its job), so once the precmd sets a title, it will be kept even if you switch to another window and back. All you have to do is configure screen's hardstatus to display the title of currently selected window. – grawity Apr 25 '11 at 21:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.