Modify or react on "cd" command

Hi folks,

I found a pretty nice thread in this forum showing how to change the window title of a PuTTY window from within the shell. (Which I can't reference due having less than 5 postings?!?)

I modified the solution a bit to have a function in my zshrc "wt()" changing the window title each time I call it to "$(pwd) @ $(hostname)".

Can I somehow connect this wt-call to my cd-command? Defining an alias doesn't work due to "cd" existing as a certain command, I reckon?
Is it possible to modify or overwrite the standard-cd-definition?

I'm sure it is, I probably just didn't find the proper search-keywords?! :wall:

Kind regards and many thanks in advance,
Andreas

You don't need this :slight_smile:
I guess Your goal is to have refreshed values of $(pwd) and $(hostname) in putty's title?
You can set variable PS1 to execute command every time prompt is being written.
In Your zshrc add

PS1=$PS1"\$(/path/to/script)"

or if You have defined function wt() before that it could look:

PS1=$PS1"\$(wt)"

So - every time You change path, or execute command, function wt() will be run.

This surely works for bash, I think it will work for zsh also...

Yes, you got my goal right, thanks.

But unfortunately it doesn't work in zsh as expected.
I just get

prompted in each line.

Ok, in zsh it should look like:

PS1=$PS1"%$(wt)"

Checked - worked. :slight_smile:

Unfortunately I have to disagree again :slight_smile:

I get this one prompted in each line now:

Where "/root @ test_mt1" is the "output" of my wt-function. But I don't wann see this in my command line but in my PuTTY window title :slight_smile:

What exactly wt function looks like? I thought it has no output... :confused:

Yep, sorry, that's why I put "output" in quotations. It's no real output.

I fiddled around with that zsh-syntax by myself, but didn't manage to get it working.

function wt()
{
export WTITLE="$(pwd) @ $(hostname)"
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND="\033]0;${WTITLE}\007"
    echo $PROMPT_COMMAND
    ;;
*)
    ;;
esac
}

With

PS1=$PS1"%$(wt)"

I get this command-line-prompt:

Many thanks for you patience!

Ok, I see what's happening. But unfortunately it seems that zsh can't execute commands in PS1 variable. At least I can't find how to do this...
With bash life is a little simplier :wink:
And returning to Your first idea, maybe an alias called "ccd" or sth like that, which would look like:

ccd () {
	cd $@
	wl
}

added to .zshrc file... Works, but requires using "ccd" instead of "cd" :slight_smile:

Rubbish zsh :wink: Unfortunately, I'm not (yet) to decide which shell to use on our systems.

But I found another way of doing it. I must confess, I don't understand that whole zsh-syntax, but I was able to modify the solution presented in the zsh-FAQ section 3.6.

Putting the following into you zshrc results in displaying "WorkingDir @ Host Username" in the PuTTY-window-title after each cd:

 chpwd() {
    [[ -t 1 ]] || return
    case $TERM in
      sun-cmd) print -Pn "\e]l$(pwd) @ $(hostname) $(whoami)\e\\"
        ;;
      *xterm*|rxvt|(dt|k|E)term) print -Pn "\e]2;$(pwd) @ $(hostname) $(whoami)\a"
        ;;
    esac
  }

(This rule of not being allowed to post URLs in the beginning is just pathetic. What is the use of that? I know cannot reference the source of my solution...)

Anyway, many thanks sulti!

---------- Post updated at 07:42 AM ---------- Previous update was at 07:41 AM ----------

Yay, I now reached my first 5 postings and so I now CAN post that URL of source of mine o_O

Z-Shell Frequently-Asked Questions