Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created October 11, 2012 18:59
Show Gist options
  • Save wilmoore/3874729 to your computer and use it in GitHub Desktop.
Save wilmoore/3874729 to your computer and use it in GitHub Desktop.
Terminal Tab reflecting your current git project name

First, setup a git alias:

% git config --global alias.project-name 'config --replace-all project.info.name'

Use it like this (your CWD should be a git project):

% git project-name "NodeJS Latest"

Review the setting with:

% git config --local --get project.info.name

In your bash profile:

export PROMPT_COMMAND="echo -ne \"\033]0;\$(git config --get project.info.name || $PROMPT_COMMAND)\007\"; $PROMPT_COMMAND"

Your terminal tab when you are within a configured project:

1

Your terminal tab when you are not in a configured project:

2

@wilmoore
Copy link
Author

Caveats

  • git(1) is a requirement; however, this is simple to extend for other VCSes.

Inspiration

Alternative Approaches

@philpennock
Copy link

Thanks for this; I adapted heavily for a zsh setup, but use this happily. In zsh, the chpwd_functions array variable can hold names of functions to be invoked whenever you change directory (unless it's a "quiet" change). This lets me use local prj="[$(git config --get project.info.name)]" and then, if that's [], replace with prj=${$(git rev-parse --show-toplevel):t} to get the project.

Then check [[ $(git rev-parse --is-inside-git-dir) = true ]] to either set here="[git-dir]" or here="$(git rev-parse --show-prefix)" and just print -Pn '\e]2;'"git:${prj}:${here}"'\a'

@wilmoore
Copy link
Author

I now use tmux heavily so this is even easier. I have the following in my ~/.tmux.conf:

set -g set-titles-string '#(tmux display-message -p "#S")'

This puts the name of the session into the terminal tab -- so much easier and does not rely on git (i.e. you may have not yet run git init or you may be using another VCS).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment