5

During my day-to-day work, I keep many terminal tabs. So to identify those, I set titles

Terminal Tab > Set Title

e.g. IRC, API Codebase, API Logs, Server1, etc

What I want to do is do something in the tab based on the title of the tab. e.g.

  1. When I set "API Codebase", It should go to my codebase and activate appropriate python virtual environment
  2. When I set "IRC", It should run irssi
  3. When I set "Server1", It should run ssh command to connect to the server

and so on.

How can this be done?

9

I would do this slightly differently. Instead of manually setting the tab's title, create a function that both sets the title and runs the desired command. Add this function (adapted from here) to your ~/.bashrc (assuming you're using bash):

function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$@\a\]"

  ## Do different things depending on the string passed
  case "$@" in
        "API Codebase")
            echo cd ~/codebase
            echo python ...
            ;;
        "IRC")
            echo irssi
            ;;
        "Server1")
            echo ssh server1
            ;;
  esac
  PS1="${ORIG}${TITLE}"
}
  • So there is no way without running set-title command? – Hussain Tamboli Feb 25 '16 at 11:43
  • 3
    @HussainTamboli well, I guess you could run a loop in the background that tests for changes in the tab's title and then run the commands but why would you want to? With the solution I give, if you make it a script, instead of a function, you can even assign shortcuts for the different values. So, say, Alt+S would run set-title Server1 for example. – terdon Feb 25 '16 at 11:52
  • Hmm. That's one way to do it. – Hussain Tamboli Feb 25 '16 at 12:57
2

To my knowledge it is not possible to inspect the state of a terminal from a process in the shell.

But even if you could, changing behavior based on the terminal title would be very error-prone. There's no easy way to check for misspellings or anything like that. It also isn't very scaleable - you'd have one do_something_based_on_title command that would just keep growing as you add features.

Instead of one massive set-title script, make each command responsible for setting it's own terminal title when they run. You can do this easily with aliases, like so (put this in your .bashrc or similar).

set_title() {
  echo -e "\e]0;$*\a"
}

alias apic='set_title "API Codebase"; command_for_api_codebase'
alias irc='set_title "IRC"; irssi'
alias server1='set_title "Server1"; ssh server1'

Now you have tab-completion for these commands, no risk of the title-setting and program-starting code getting out of whack, and no more need to right click on the tab to set its title. Just run the commands you want, and wham the title gets set too.

  • You're missing a single quote on the first alias – cat Feb 25 '16 at 15:01
  • Fixed. You can also make such edits yourself. – dimo414 Feb 25 '16 at 15:04
  • Actually, I can't, because it wouldn't be six chars and I don't have 1k edit privileges yet :) – cat Feb 25 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.