Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I can change the name of a window with Ctrl-a Shift-a. Instead of editing several window names by hand, is there a way to have them automatically named after the current directory?

share|improve this question
    
Which shell do you use? –  marco Jan 18 '11 at 20:01
    
I'm using bash. –  David McDavidson Jan 18 '11 at 20:12

2 Answers 2

up vote 17 down vote accepted

Make your shell change the window title every time it changes directory, or every time it displays a prompt.

For your ~/.bashrc:

if [[ "$TERM" == screen* ]]; then
  screen_set_window_title () {
    local HPWD="$PWD"
    case $HPWD in
      $HOME) HPWD="~";;
      $HOME/*) HPWD="~${HPWD#$HOME}";;
    esac
    printf '\ek%s\e\\' "$HPWD"
  }
  PROMPT_COMMAND="screen_set_window_title; $PROMPT_COMMAND"
fi

Or for your ~/.zshrc (for zsh users):

precmd () {
  local tmp='%~'
  local HPWD=${(%)tmp}
  if [[ $TERM == screen* ]]; then
    printf '\ek%s\e\\' $HPWD
  fi
}

For more information, look up under Dynamic titles in the Screen manual, or under “Titles (naming windows)” in the man page.

share|improve this answer
    
I copypasted the bash one on my .bashrc and it is giving off these errors: bash: PROMPT_COMMAND: line 0: syntax error near unexpected token ;' bash: PROMPT_COMMAND: line 0: ;screen_set_window_title' –  David McDavidson Jan 19 '11 at 5:40
    
@David: Sorry, the point was to accumulate commands in PROMPT_COMMAND if there was already something, but I had the components in the wrong order. –  Gilles Jan 19 '11 at 8:08
    
Awesome, its working perfectly now. Thanks! –  David McDavidson Jan 19 '11 at 13:49
2  
Great trick @Gilles. For those who like to keep window titles short, you can drop the full path and only keep the basename. Just replace $HOME/*) HPWD="~${HPWD#$HOME}";; with *) HPWD=`basename "$HPWD"`;; –  user6176 Mar 30 '11 at 21:32

You can put this into the .bashrc to change the terminal name (window name) to the last directory (or file name) that you are in (working on).

case "$TERM" in xterm*|rxvt*) PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"' ;; *) ;; esac

more described here: link

share|improve this answer

Your Answer

 
discard

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.