# 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.
Thanks a lot. |
This comment has been minimized.
This comment has been minimized.
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.
When in
Any ideas? |
This comment has been minimized.
This comment has been minimized.
Thanks! Good job! |
This comment has been minimized.
This comment has been minimized.
thank you veeeeery much! |
This comment has been minimized.
This comment has been minimized.
@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.
This is a fantastic addition to my |
This comment has been minimized.
This comment has been minimized.
This is awesome. |
This comment has been minimized.
This comment has been minimized.
Just what I was looking for. Thanks! |
This comment has been minimized.
This comment has been minimized.
Appreciate the Gist. This is exactly what I was looking for! |
This comment has been minimized.
This comment has been minimized.
Awesome! +1 |
This comment has been minimized.
This comment has been minimized.
You are a gentleman sir. |
This comment has been minimized.
This comment has been minimized.
Great stuff! Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks! very useful |
This comment has been minimized.
This comment has been minimized.
This does not seem to be working in iTerm version 3.0.12 |
This comment has been minimized.
This comment has been minimized.
@alexey-danilov confirmed for me as well. |
This comment has been minimized.
This comment has been minimized.
^^ |
This comment has been minimized.
This comment has been minimized.
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.
Great and thanks! |
This comment has been minimized.
This comment has been minimized.
What worked for me was this (on MacOS sierra):
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The following two commands in
This also lets you set the title with |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
Only @alexilyaev worked for me on tmux. |
This comment has been minimized.
This comment has been minimized.
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.
You're a lifesaver, this makes my life so much easier. |
This comment has been minimized.
This comment has been minimized.
@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.
@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.
@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.
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.
also use precmd function, to watch directories changes. |
This comment has been minimized.
This comment has been minimized.
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.
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.
Many thanks for this helpful stuff! |
This comment has been minimized.
This comment has been minimized.
Here's a more legible version of @scumbkt19's snippet:
|
This comment has been minimized.
This comment has been minimized.
To expand on @jhrr I've added the current branch to my tab title.
|
This comment has been minimized.
This comment has been minimized.
Thank you @jwmann. |
This comment has been minimized.
This comment has been minimized.
Simple and elegant. This is a gem. |
This comment has been minimized.
This comment has been minimized.
Did this suddenly stop working in iTerm2 v3.3.3 for anyone else than me? Does anyone know how to fix that? |
This comment has been minimized.
This comment has been minimized.
Stopped working for me this morning :( |
This comment has been minimized.
This comment has been minimized.
@garyking @runtimee did you try Profiles > (your profile) > Terminal > [x] "Terminal may report window title"? I'm not 100% certain that's the cause but I had the same issue, selected that, opened a new tab in the profile and it worked again. FWIW I use Fish shell now which just has a |
This comment has been minimized.
This comment has been minimized.
stopped working for me too |
This comment has been minimized.
This comment has been minimized.
stopped working for me too [2]. Checking the Profiles > (your profile) > Terminal > [x] "Terminal may report window title" didn't work for me. |
This comment has been minimized.
This comment has been minimized.
Mine also stopped working. |
This comment has been minimized.
This comment has been minimized.
The tcsh version I had to do this (which I copied from here long, long ago) also stopped working this evening after I logged in, for no obvious reason. iTerm2 v3.3.3 Kind of a disaster for me..... |
This comment has been minimized.
This comment has been minimized.
according to the changelog for 3.3.3 on https://iterm2.com/downloads.html says
it might have made changes to the default profile if that was the one you were using "/ |
This comment has been minimized.
This comment has been minimized.
Ah! That was really useful, many thanks. The solution appears to be fiddle with Preferences->General and change the "Title" box to something (like "Name" -- I also unchecked "Job Name") which allows the "Applications in terminal may change the title" box to be checked. After I did that, the directory names came back as the tab titles, so I guess the script I run is allowed through. |
This comment has been minimized.
This comment has been minimized.
Thanks @dvbowen, that worked perfectly. |
This comment has been minimized.
This comment has been minimized.
Thanks @dvbowen! And .zshrc:
|
This comment has been minimized.
This comment has been minimized.
The above solution worked for me. |
This comment has been minimized.
This comment has been minimized.
Thanks for that too @dcastro. |
This comment has been minimized.
This comment has been minimized.
I always get this --> |
This comment has been minimized.
This comment has been minimized.
Update: I always get this error everytime I open new terminal on This is my script: (working but always show bash error: To fix the bash error |
This comment has been minimized.
This comment has been minimized.
@kenjoegolo that makes it sound like you modified the prompt command multiple times. The |
This comment has been minimized.
This comment has been minimized.
For me, using zsh, adding DISABLE_AUTO_TITLE="true"
tab_title() {
# sets the tab title to current dir
echo -ne "\e]1;${PWD##*/}\a"
}
add-zsh-hook precmd tab_title |
This comment has been minimized.
This comment has been minimized.
Thanks Man. |
This comment has been minimized.
This comment has been minimized.
When I connect via |
This comment has been minimized.
This comment has been minimized.
This gist is pure gold. |
This comment has been minimized.
This comment has been minimized.
This kind of confuses if two panels is open in a single window |
This comment has been minimized.
This comment has been minimized.
The above solution worked for me. |
This comment has been minimized.
This comment has been minimized.
+1 |
This comment has been minimized.
This comment has been minimized.
Is there a way to show just ~ when I'm in my home directory, and the current directory otherwise? I tried all kinds of things to nmodify ${PWD##/} to do that but can't get it to work. I know that this works: ${PWD/#$HOME/~} |
This comment has been minimized.
This comment has been minimized.
You should just be able to add some logic to your prompt, psuedocode for it being "if PWD = $HOME then print '~' else print PWD". |
This comment has been minimized.
This comment has been minimized.
Tried that but didn't work. I found a different workaround though. Instead of setting PROMPT_COMMAND, you can add the code that changes the tab name to PS1, like this: |
This comment has been minimized.
It took me way too long to figure this out so I've put in all the documentation I could find.