4

Something I have found that helps me when I have multiple tabs open in my terminal is to set the title and badge text; I do this by pressing Cmd + i and manually setting it.

What I would find even more beneficial is a command line option to set these up.

I have found a function to set the title, but not the badge:

# Set Terminal Tab Title
function title {
    echo -ne "\033]0;"$*"\007"
}

Moving one step on from this, I would like to be able to call the function and have it set the title based upon the directory that I am in currently.

So I have two questions:

  1. How can I set the iTerm tab title to the directory that I am in currently?

  2. How can I do the same for the badge?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283

2 Answers2

6

When you have multiple tabs open, it is often difficult to know where you are and what you are working on currently. To help with this, you can set the title and badge text to make it more clear.

To set this up takes minutes; firstly enable Shell Integration and then place the following snippets into your .bashrc file:

# This will set your window title
export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
source ~/.iterm2_shell_integration.`basename $SHELL`

# This creates the var currentDir to use later on
function iterm2_print_user_vars() {
  iterm2_set_user_var currentDir "${PWD##*/}"
}

Next, go to your default profile in iTerm2 and place \(user.currentDir) in the badges field. When you restart your terminal, you will notice your title and badge automatically update with the directory that you are in currently.

(This was reposted from a blog article I wrote, which is no longer online).

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283
0

I found the node project iterm2-tab-set very useful. This makes all this very simple with lots of options, and auto configuring the colors based on the badge text.

Krishna Vedula
  • 1,643
  • 1
  • 27
  • 31