HOWTO: populate your term's title automatically

g33kWhen you’re running a ton of termial windows or tabs, it helps to have the title of the box name, along with some environment values, easily available to keep you orientated. Here’s a quick script I created to do this automatically when called via your .profile file in your home directory.

#!/bin/bash
HOST_NAME=`hostname -f`
if [ `id -u` = 0 ]; then
OPT="`uname` (`uname -a | cut -f12 -d' ' -`) - ROOT USER"
else
OPT="`uname` (`uname -a | cut -f12 -d' ' -`)"
fi
REPLACE="${HOST_NAME} - ${OPT}"
echo -n -e "33]0; $REPLACE 07 "
echo "${REPLACE}"
exit 0

When I run this script in my term here at work, the title or tab becomes:

nldg-8 (Linux / x86_64)

Drop this into a bin directory your user can hit – I always put on in my home directory and append ~/bin to my PATH in my .profile. For Solaris fans/users, it needs to be done a bit differently:

#!/usr/local/bin/bash
HOST_NAME=`uname -a | cut -f2 -d' ' -`
OPT="(`uname -a | cut -f1 -d' ' -` / `uname -a | cut -f6 -d' '`)"
REPLACE="${HOST_NAME} - ${OPT}"
echo -n -e "33]0; $REPLACE 07 "
echo "${REPLACE}"
exit 0

foo

  • cas

    Seems to work.

    Have you considered putting this into a shell function in your .bashrc,
    titled something like, “set_title”.

    Then. If you were to create a “_cd” function that did “cd” $@ first thing, then executed your set_title with a parameter, you could even figure out how to get your current working directory in your title bar.

    If you wanted to, that is.

    I usually combine the title setting with the setting of my PS1 variable in my _cd().

    Hope this finds you well,
    cas

  • cas

    Seems to work.

    Have you considered putting this into a shell function in your .bashrc,
    titled something like, “set_title”.

    Then. If you were to create a “_cd” function that did “cd” $@ first thing, then executed your set_title with a parameter, you could even figure out how to get your current working directory in your title bar.

    If you wanted to, that is.

    I usually combine the title setting with the setting of my PS1 variable in my _cd().

    Hope this finds you well,
    cas

  • http://fak3r.com/ fak3r

    Nice, I never thought of that, the way I do it is have my PS1 contain this info:

    [11:04:49] [fak3r@chavez /home/faj3r/build/bin]$

    This has all the info I need, plus it truncates the path if it get’s too long, keeping my prompt at least a big consistant. Here’s my .bashrc:

    bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
    NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
    NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi
    }
    #
    bash_prompt() {
    local NONE='[33[0m]' # unsets color to term's fg color

    # regular colors
    local K='[33[0;30m]' # black
    local R='[33[0;31m]' # red
    local G='[33[0;32m]' # green
    local Y='[33[0;33m]' # yellow
    local B='[33[0;34m]' # blue
    local M='[33[0;35m]' # magenta
    local C='[33[0;36m]' # cyan
    local W='[33[0;37m]' # white

    # empahsized (bolded) colors
    local EMK='[33[1;30m]'
    local EMR='[33[1;31m]'
    local EMG='[33[1;32m]'
    local EMY='[33[1;33m]'
    local EMB='[33[1;34m]'
    local EMM='[33[1;35m]'
    local EMC='[33[1;36m]'
    local EMW='[33[1;37m]'

    # background colors
    local BGK='[33[40m]'
    local BGR='[33[41m]'
    local BGG='[33[42m]'
    local BGY='[33[43m]'
    local BGB='[33[44m]'
    local BGM='[33[45m]'
    local BGC='[33[46m]'
    local BGW='[33[47m]'

    local UC=$C # user's color
    [ $UID -eq "0" ] && UC=$R # root's color

    PS1="${W}[t${W}] ${Y}[${R}u${R}@${R}h ${EMB}${NEW_PWD}${Y}]${W}\$ ${NONE}"
    }
    #
    PROMPT_COMMAND=bash_prompt_command
    bash_prompt
    unset bash_prompt

    Appreciate the advice!

  • http://fak3r.com fak3r

    Nice, I never thought of that, the way I do it is have my PS1 contain this info:

    [11:04:49] [fak3r@chavez /home/faj3r/build/bin]$

    This has all the info I need, plus it truncates the path if it get’s too long, keeping my prompt at least a big consistant. Here’s my .bashrc:

    bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
    NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
    NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi
    }
    #
    bash_prompt() {
    local NONE='\[33[0m\]' # unsets color to term's fg color

    # regular colors
    local K='\[33[0;30m\]' # black
    local R='\[33[0;31m\]' # red
    local G='\[33[0;32m\]' # green
    local Y='\[33[0;33m\]' # yellow
    local B='\[33[0;34m\]' # blue
    local M='\[33[0;35m\]' # magenta
    local C='\[33[0;36m\]' # cyan
    local W='\[33[0;37m\]' # white

    # empahsized (bolded) colors
    local EMK='\[33[1;30m\]'
    local EMR='\[33[1;31m\]'
    local EMG='\[33[1;32m\]'
    local EMY='\[33[1;33m\]'
    local EMB='\[33[1;34m\]'
    local EMM='\[33[1;35m\]'
    local EMC='\[33[1;36m\]'
    local EMW='\[33[1;37m\]'

    # background colors
    local BGK='\[33[40m\]'
    local BGR='\[33[41m\]'
    local BGG='\[33[42m\]'
    local BGY='\[33[43m\]'
    local BGB='\[33[44m\]'
    local BGM='\[33[45m\]'
    local BGC='\[33[46m\]'
    local BGW='\[33[47m\]'

    local UC=$C # user's color
    [ $UID -eq "0" ] && UC=$R # root's color

    PS1="${W}[\t${W}] ${Y}[${R}\u${R}@${R}\h ${EMB}\${NEW_PWD}${Y}]${W}\\$ ${NONE}"
    }
    #
    PROMPT_COMMAND=bash_prompt_command
    bash_prompt
    unset bash_prompt

    Appreciate the advice!

  • http://www.tvloanmodificationleads.com Loan Modification leads

    nice blog.. it will a lot.. especially for me… thnx

  • http://www.tvloanmodificationleads.com Loan Modification leads

    nice blog.. it will a lot.. especially for me… thnx

  • HallBasham878

    Hello Mate, I would like to know more on this when is the next post commingrespectjenny omper______________________________________________passing hair drug test | pass drug test | pass marijuana drug test

  • http://zixmailencryption.com/ zixmail

    hello friend, How to automatically capture visitors name/ID and populate attendance list …. Enter your search terms!

  • http://zixmailencryption.com/ zixmail

    hello friend, How to automatically capture visitors name/ID and populate attendance list …. Enter your search terms!

  • http://zixmailencryption.com/ zixmail

    hello friend, How to automatically capture visitors name/ID and populate attendance list …. Enter your search terms!

Read previous post:
Tie goes to the runner

I've had a ton of jobs, some would say this is a liability, but if questioned I have perfectly good [...]

Close