# put this in your .bash_profile | |
if [ $ITERM_SESSION_ID ]; then | |
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND"; | |
fi | |
# Piece-by-Piece Explanation: | |
# the if condition makes sure we only screw with $PROMPT_COMMAND if we're in an iTerm environment | |
# iTerm happens to give each session a unique $ITERM_SESSION_ID we can use, $ITERM_PROFILE is an option too | |
# the $PROMPT_COMMAND environment variable is executed every time a command is run | |
# see: ss64.com/bash/syntax-prompt.html | |
# we want to update the iTerm tab title to reflect the current directory (not full path, which is too long) | |
# echo -ne "\033;foo\007" sets the current tab title to "foo" | |
# see: stackoverflow.com/questions/8823103/how-does-this-script-for-naming-iterm-tabs-work | |
# the two flags, -n = no trailing newline & -e = interpret backslashed characters, e.g. \033 is ESC, \007 is BEL | |
# see: ss64.com/bash/echo.html for echo documentation | |
# we set the title to ${PWD##*/} which is just the current dir, not full path | |
# see: stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script | |
# then we append the rest of $PROMPT_COMMAND so as not to remove what was already there | |
# voilà! |
This comment has been minimized.
This comment has been minimized.
tonatiuh
commented
Mar 12, 2014
Thanks a lot. |
This comment has been minimized.
This comment has been minimized.
tjmcewan
commented
Jul 18, 2014
This gist is
I pieced it together from this gist & this superuser answer. Note: I removed the iTerm-specific guard because Terminal.app also supports this now. |
This comment has been minimized.
This comment has been minimized.
phaseOne
commented
Jul 25, 2014
When in
Any ideas? |
This comment has been minimized.
This comment has been minimized.
caseycs
commented
Sep 4, 2014
Thanks! Good job! |
This comment has been minimized.
This comment has been minimized.
nirnaeth
commented
Oct 8, 2014
thank you veeeeery much! |
This comment has been minimized.
This comment has been minimized.
jmsmrgn
commented
Nov 12, 2014
@phaseOne - this clears up iterm command not found
https://github.com/barryclark/bashstrap/blob/master/.bash_profile#L105 |
This comment has been minimized.
This comment has been minimized.
ericdorsey
commented
Mar 19, 2015
This is a fantastic addition to my |
This comment has been minimized.
This comment has been minimized.
countcain
commented
Jul 3, 2015
This is awesome. |
This comment has been minimized.
This comment has been minimized.
chris-schmitz
commented
Aug 25, 2015
Just what I was looking for. Thanks! |
This comment has been minimized.
This comment has been minimized.
vgaviria
commented
Nov 19, 2015
Appreciate the Gist. This is exactly what I was looking for! |
This comment has been minimized.
This comment has been minimized.
cybertk
commented
Dec 1, 2015
Awesome! +1 |
This comment has been minimized.
This comment has been minimized.
clarkenheim
commented
May 2, 2016
You are a gentleman sir. |
This comment has been minimized.
This comment has been minimized.
spindelmanne
commented
May 10, 2016
Great stuff! Thanks! |
This comment has been minimized.
This comment has been minimized.
mhdkassir
commented
Jun 8, 2016
Thanks! very useful |
This comment has been minimized.
This comment has been minimized.
alexey-danilov
commented
Dec 3, 2016
This does not seem to be working in iTerm version 3.0.12 |
This comment has been minimized.
This comment has been minimized.
bradj
commented
Dec 6, 2016
@alexey-danilov confirmed for me as well. |
This comment has been minimized.
This comment has been minimized.
markbuckalewcb
commented
Dec 8, 2016
^^ |
This comment has been minimized.
This comment has been minimized.
alexilyaev
commented
Dec 31, 2016
•
If you get this:
Or you want to be able to add several commands to PROMPT_COMMAND, this is what I use: # Add command to PROMPT_COMMAND (runs before each command)
# Makes sure ithe command is not already in PROMPT_COMMAND
addToPromptCommand() {
if [[ ":$PROMPT_COMMAND:" != *":$1:"* ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND:+"$PROMPT_COMMAND:"}$1"
fi
}
# Set iTerm title to show current directory
if [ $ITERM_SESSION_ID ]; then
addToPromptCommand 'echo -ne "\033];${PWD##*/}\007"'
fi Based on: |
This comment has been minimized.
This comment has been minimized.
twei55
commented
Jan 5, 2017
Great and thanks! |
This comment has been minimized.
This comment has been minimized.
buraksay
commented
Jan 22, 2017
What worked for me was this (on MacOS sierra):
|
This comment has been minimized.
This comment has been minimized.
kud
commented
Feb 1, 2017
This comment has been minimized.
This comment has been minimized.
jeff-kilbride
commented
Feb 13, 2017
The following two commands in
This also lets you set the title with |
This comment has been minimized.
This comment has been minimized.
Pierre-Thibault
commented
Mar 8, 2017
Thank you! |
This comment has been minimized.
This comment has been minimized.
kurko
commented
Mar 12, 2017
Only @alexilyaev worked for me on tmux. |
This comment has been minimized.
This comment has been minimized.
seantcanavan
commented
Mar 15, 2017
None of these worked for me in iterm2 Build 3.0.14 on macOS Sierra 10.12.3 (16D32). They either overwrote what I put in my |
This comment has been minimized.
This comment has been minimized.
jameswise-ft
commented
Sep 11, 2017
You're a lifesaver, this makes my life so much easier. |
This comment has been minimized.
This comment has been minimized.
jeromecovington
commented
Sep 22, 2017
@phette23, will the gist that you provided (thanks for taking the time to do so), show the current working directory and the current process? |
This comment has been minimized.
This comment has been minimized.
ozydingo
commented
Oct 11, 2017
@jeromecovington if you have the iterm settings checked to show the current job name, this will be appended. If you mean process id (not sure if that's really useful but hey why not try), you can use the bash variable
|
This comment has been minimized.
This comment has been minimized.
ozydingo
commented
Oct 11, 2017
@phette23 thanks for this, it's really helpful! I've added two lines of comment-docs to ensure that people have the correct setting in iTerm to allow this change to take effect -- without the "terminal may set tab/window title" setting checked this will not work. This may be why some people are having trouble. See https://gist.github.com/ozydingo/cc4d400fcf754ae0f1456ab7240be604 . Could you add these two comments, in some form, to this gist? |
This comment has been minimized.
This comment has been minimized.
danio
commented
Mar 8, 2018
Using the agnoster theme that also sets the prompt to show the current git branch, I found that it no longer worked correctly, even using @alexilyaev's suggestion. The only way I could get it to work for me is:
(thanks to https://stackoverflow.com/a/36669770/12663) |
This comment has been minimized.
This comment has been minimized.
CavalcanteLeo
commented
Apr 5, 2018
also use precmd function, to watch directories changes. |
This comment has been minimized.
This comment has been minimized.
wsshin
commented
Jul 18, 2018
•
To show not just the current directory, but also the immediate parent directory, use this: if [ $ITERM_SESSION_ID ]; then
export PROMPT_COMMAND='i="$IFS";IFS='/';set -f;p=($PWD);set +f;IFS="$i";if [ ${#p[@]} -le 3 ];then echo -ne "\033];$PWD\007";else echo -ne "\033];${p[-2]}/${p[-1]}\007";fi;':$PROMPT_COMMAND
fi Showing the immediate parent directory is useful when you work on several different projects with similar directory structures. For example, if you work in For tokenizing EDIT: the command is updated to handle |
This comment has been minimized.
This comment has been minimized.
scumbkt19
commented
Oct 2, 2018
I ran into this looking for a way to mark the current git repo i'm in. This version will display the git repository root (normally the repo name) if you're in a repository, or it will fall back to show the current working direcory if [ $ITERM_SESSION_ID ]; then
export PROMPT_COMMAND='git rev-parse --is-inside-work-tree > /dev/null 2>&1 && echo -ne "\033];$(basename $(git rev-parse --show-toplevel))\007" || echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi |
This comment has been minimized.
This comment has been minimized.
bertolin
commented
Nov 16, 2018
Many thanks for this helpful stuff! |
This comment has been minimized.
This comment has been minimized.
jhrr
commented
Nov 21, 2018
Here's a more legible version of @scumbkt19's snippet:
|
This comment has been minimized.
This comment has been minimized.
jwmann
commented
Jan 12, 2019
To expand on @jhrr I've added the current branch to my tab title.
|
This comment has been minimized.
phette23 commentedMar 29, 2013
It took me way too long to figure this out so I've put in all the documentation I could find.