look out honey 'cause I'm using technology

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 "\033]0; $REPLACE \007 "
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 "\033]0; $REPLACE \007 "
echo "${REPLACE}"
exit 0

foo



Related posts

    
  • nice blog.. it will a lot.. especially for me... thnx
  • 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='\[\033[0m\]' # unsets color to term's fg color

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

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

    # background colors
    local BGK='\[\033[40m\]'
    local BGR='\[\033[41m\]'
    local BGG='\[\033[42m\]'
    local BGY='\[\033[43m\]'
    local BGB='\[\033[44m\]'
    local BGM='\[\033[45m\]'
    local BGC='\[\033[46m\]'
    local BGW='\[\033[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!
  • 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
blog comments powered by Disqus

We like








We support


EFF - Electronic Frontier Foundation       TOR - The Onion Router       HRC - Human Rights Campaign







Private