Skip to content

Instantly share code, notes, and snippets.

@phette23
Last active August 3, 2024 16:51
Show Gist options
  • Save phette23/5270658 to your computer and use it in GitHub Desktop.
Save phette23/5270658 to your computer and use it in GitHub Desktop.
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à!
@srikar-git
Copy link

This gist is pure gold.

@avatar-lavventura
Copy link

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

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

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

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

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

This is just perfect! Thanks

@ziishaned
Copy link

Below is what I am using and it is much simple and memory efficient

DISABLE_AUTO_TITLE="true"
echo -ne "\e]1;\a"

Then open the settings for iTerm2 and select PWD from the title dropdown.

Screen Shot 2022-09-06 at 11 24 22 AM

Now the tab title for iTerm2 will look something like below:

Screen Shot 2022-09-06 at 11 26 38 AM

@Offirmo
Copy link

Offirmo commented Sep 7, 2023

Hi, the correct line (for bash) should be:

export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }echo -ne \"\033];\${PWD##*/}\007\""

in order to properly append to an existing PROMPT_COMMAND

Source: /etc/bashrc_Apple_Terminal on macOS Ventura

@bmelton-mdsol
Copy link

bmelton-mdsol commented Dec 28, 2023

Hi, the correct line (for bash) should be:

export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }echo -ne \"\033];\${PWD##*/}\007\""

in order to properly append to an existing PROMPT_COMMAND

Source: /etc/bashrc_Apple_Terminal on macOS Ventura

This fixed the problem I was having, similar to @kenjoegolo above: any time I ran source ~/.bash_profile I would get bash: :echo: command not found, which would persist. Each successive time I ran source ~/.bash_profile, the number of times bash: :echo: command not found displayed would increment (and persist).
Once I replaced the export with @Offirmo 's version, the problem stopped.

image

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