Bash – Screen status bar to display current directory for zsh/bash shell

bashgnu-screenzsh

I use GNU screen a lot with zsh as my shell. It would be a nice feature (I think) to be able to display the current directory name (not the full path) as the tab title. I can do that with CTRL+A SHIFT+A but that is manual. I would like it to change whenever I change directory.

Does anyone know of a way to do this?

Edit: The answer also feature the solution for bash.

Best Answer

For zsh:

Put this in your ~/.zsh

[centos@centos ~]$ cat .zsh
if [[ ${TERM} == "screen-bce" || ${TERM} == "screen" ]]; then
  precmd () { print -Pn "\033k\033\134\033k%m[%1d]\033\134" }
  preexec () { print -Pn "\033k\033\134\033k%m[$1]\033\134" }
else
  precmd () { print -Pn "\e]0;%n@%m: %~\a" }
  preexec () { print -Pn "\e]0;%n@%m: $1\a" }
fi
PS1=$'%{\e[0;32m%}%m%{\e[0m%}:%~> '
export PS1
[centos@centos ~]$ 

In your ~/.screenrc

hardstatus string "%h"
caption always "%{= kw} %-w%{= wk}%n*%t%{-}%+w%{= kw} %=%d %M %0c %{g}%H%{-}"
Related Question