How does this script for naming iTerm tabs work?

6.4k views Asked by At

I'm trying to name my iTerm tabs and found this link. Here is the pertinent part of the guy's post:

I wrote a simple script, which I call “nametab”, which allows you to name the tab you are in from the command line. You just type something like:

$ nametab New tab name

If you’d like to use this yourself, here is the code:

#!/bin/bash

# A simple script which will name a tab in iTerm
# usage:
# $ nametab New tab name

echo -ne "\033]0;"$@"\007"

I have created a directory $HOME/dev/bash_scripts and placed a file in that directory called nametab.sh. I then switched to that directory and ran the command

chmod u+x nametab.sh

But when I try to name my current tab in iTerm by typing nametab.sh New tab hellooooo, nothing happens. I also tried nametab.sh hellooooo, and nothing happens.

Can you help me understand what I don't understand?


Update:

echo TERM=$TERM

returns

TERM=xterm-256color

and

echo $PATH

returns

.:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:~/dev/bash_scripts

and

cat ~/dev/bash_scripts/nametab.sh

returns

#!/bin/bash

# A simple script which will name a tab in iTerm
# usage:
# $ nametab NewTabName

echo "trying to rename the current tab to $@"
echo -ne "\033]0;"$@"\007"
echo "finished"

and

nametab.sh hellooooo

returns

trying to rename the current tab to helloooo
finished

but the tab name always stays the same.

Incidentally, the tab name reads

MindRoot (bash)

I am running iTerm2. I try to do all my bash shell configuration in /etc/bashrc. This way I get the same bash terminal behavior regardless of which user account I am logged in on. The contents of /etc/bashrc is

# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
   return
fi

#PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize

# ALL OF THE BELOW ADDED BY DEONOMO ON 2011-04-25

# custom prompt
PROMPT_HOSTNAME='MindRoot' 
PROMPT_COLOR='0;35m'

# If I am root, set the prompt to bright red
if [ ${UID} -eq 0 ]; then
PROMPT_COLOR='1;31m'
fi

PS1='\[\e]1;${PROMPT_HOSTNAME}\a\e]2;${PROMPT_HOSTNAME}:${PWD}\a\
\e[${PROMPT_COLOR}\]\
[\u@${PROMPT_HOSTNAME} \w]\n \#\$ \
\[\e[m\]'

#PS1="\e[0;45m\w:$ "

# added by Deonomo on 2011/09/12 in order to have textmate work as default editor
export EDITOR='mate -w'

# added by Deonomo on 2012-01-11 in order to start a dev/bash_scripts directory
export PATH="$PATH:~/dev/bash_scripts"
3

There are 3 answers

0
csiu On
rename_tab () {
    TEXT=$1
    export PROMPT_COMMAND='echo -ne "\033]0;${TEXT}\007"'
}

This is a function. You can add it to your ~/.bashrc (or something similar, like a ~/.bash_profile). To rename the tabs, you can then do this:

$ rename_tab 'NEW NAME HERE'

1
Chad Armstrong On

I was having your same problem - but I saw the tab name briefly flash before going to back to what it was: the shell and the cwd. Turns out I had an environment variable changing the tab name on every shell command, so this fixed it for me:

export PROMPT_COMMAND=''

Now: echo -e "\033];MY_NEW_TITLE\007"

..works just fine and persists.

0
Jeppe Fihl-Pearson On

If you want to have an alias for changing the tab name, you can actually do it by defining a function in your .profile/.bashrc file like this:

function renametab () {
    echo -ne "\033]0;"$@"\007"
}