Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set the iTerm tab title to the current directory, not full path.
# 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à!
@tsiq-pete
Copy link

tsiq-pete commented Jun 4, 2019

Thank you @jwmann.

@dhruv-m-patel
Copy link

dhruv-m-patel commented Aug 21, 2019

Simple and elegant. This is a gem. 👍 (iTerm v3.3.2)

@garyking
Copy link

garyking commented Sep 5, 2019

Did this suddenly stop working in iTerm2 v3.3.3 for anyone else than me? Does anyone know how to fix that?

@runtimee
Copy link

runtimee commented Sep 5, 2019

Stopped working for me this morning :(

@phette23
Copy link
Author

phette23 commented Sep 5, 2019

@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 fish_title function you can override so I don't really check in on this snippet.

@andreasmihm
Copy link

andreasmihm commented Sep 6, 2019

stopped working for me too

@fmmatheus
Copy link

fmmatheus commented Sep 6, 2019

stopped working for me too [2]. Checking the Profiles > (your profile) > Terminal > [x] "Terminal may report window title" didn't work for me.

@jjpmann
Copy link

jjpmann commented Sep 6, 2019

Mine also stopped working.
iTerm2 v3.3.3
OSX 10.14.6
Rolled back to iTerm2 v3.3.2 did not fix it.

@dvbowen
Copy link

dvbowen commented Sep 7, 2019

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
macOS 10.14.6

Kind of a disaster for me.....

@nerdondon
Copy link

nerdondon commented Sep 8, 2019

according to the changelog for 3.3.3 on https://iterm2.com/downloads.html says

Improvements
- Change the default tab/window title to show the
  job name.

it might have made changes to the default profile if that was the one you were using "/

@dvbowen
Copy link

dvbowen commented Sep 8, 2019

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.

@seanhamlin
Copy link

seanhamlin commented Sep 8, 2019

Thanks @dvbowen, that worked perfectly.

@dcastro
Copy link

dcastro commented Sep 9, 2019

Thanks @dvbowen!
For clarity, here's what Preferences > Profile should look like:

Screenshot 2019-09-09 at 14 06 38
Screenshot 2019-09-09 at 14 06 48

And .zshrc:

DISABLE_AUTO_TITLE="true"

precmd() {
  # sets the tab title to current dir
  echo -ne "\e]1;${PWD##*/}\a"
}

@fmmatheus
Copy link

fmmatheus commented Sep 9, 2019

The above solution worked for me. 👍

@nunogoncalves
Copy link

nunogoncalves commented Sep 12, 2019

Thanks for that too @dcastro.
I added an answer here: https://superuser.com/a/1481956/767288 with your suggestion.

@kenjoegolo
Copy link

kenjoegolo commented Oct 7, 2019

I always get this --> bash: ::echo: command not found when I open my vscode terminal

@kenjoegolo
Copy link

kenjoegolo commented Oct 7, 2019

Update: I always get this error everytime I open new terminal on vscode or every command on iterm
bash: ::echo: command not found

This is my script: (working but always show bash error: :echo: command not found)
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007";':"$PROMPT_COMMAND";

To fix the bash error :echo: command not found, remove this one --> :"$PROMPT_COMMAND";
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"';

@phette23
Copy link
Author

phette23 commented Oct 7, 2019

@kenjoegolo that makes it sound like you modified the prompt command multiple times. The ':"$PROMPT_COMMAND"; postfix is meant to preserve any existing prompt command but it seems like your existing prompt command must start with "echo", thus why ":echo" is not found. You could add a space after the colon to fix this. Obviously, if the other piece is working, then the actual echo program is available. With the old string including postfix, what is the output of echo $PROMPT_COMMAND?

@benwoodward
Copy link

benwoodward commented Nov 12, 2019

For me, using zsh, adding add-zsh-hook precmd tab_title was necessary.

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

@nblahmad
Copy link

nblahmad commented Feb 6, 2020

Thanks Man.

@avatar-lavventura
Copy link

avatar-lavventura commented May 10, 2020

When I connect via ssh path shown as ...<path>

@srikar-git
Copy link

srikar-git commented Jul 11, 2020

This gist is pure gold.

@avatar-lavventura
Copy link

avatar-lavventura commented Jul 11, 2020

This kind of confuses if two panels is open in a single window

@Kcium
Copy link

Kcium commented Nov 17, 2020

Thanks @dvbowen!
For clarity, here's what Preferences > Profile should look like:

Screenshot 2019-09-09 at 14 06 38
Screenshot 2019-09-09 at 14 06 48

And .zshrc:

DISABLE_AUTO_TITLE="true"

precmd() {
  # sets the tab title to current dir
  echo -ne "\e]1;${PWD##*/}\a"
}

The above solution worked for me. 👍

@jjercx
Copy link

jjercx commented Jan 14, 2021

+1

@bkedryna
Copy link

bkedryna commented Jan 28, 2021

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/~}
But it shows the whole path when not in home directory. I tried this:
${PWD##
/#$USER/~}
But this didn't work.

@phette23
Copy link
Author

phette23 commented Jan 28, 2021

fish shell has a nice prompt_pwd function for this that makes sure the PWD string is compact & uses ~ for the home directory, but that's not helpful if you're using another shell.

You should just be able to add some logic to your prompt, psuedocode for it being "if PWD = $HOME then print '~' else print PWD".

@bkedryna
Copy link

bkedryna commented Jan 28, 2021

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:
PS1="[\033]0;\W\007]\w $ "
This shows me the full path at the command prompt but only the last directory in the tab name, or ~ if I'm in the home dir. This is perfect for me. Thanks

@tianqing617
Copy link

tianqing617 commented May 13, 2021

Thanks @dvbowen!
For clarity, here's what Preferences > Profile should look like:

Screenshot 2019-09-09 at 14 06 38
Screenshot 2019-09-09 at 14 06 48

And .zshrc:

DISABLE_AUTO_TITLE="true"

precmd() {
  # sets the tab title to current dir
  echo -ne "\e]1;${PWD##*/}\a"
}

Success! Thank your

@NoSkillGuy
Copy link

NoSkillGuy commented May 30, 2021

This is just perfect! Thanks

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