Multiple Custom Commands in Shell Startup (for Terminator)

Multiplexing with a GUI

If you use the shell extensively, I hope you know terminator by now. But, you know, this whole “writing a blog” thing will be pretty pointless if you already knew everything I wanted to say, so… I should probably elaborate.

Whenever I use a shell, I probably need several side by side - In one, I compile; in a second, I check the system log; in a third, I have an SSH to a staging server; in a fourth, I have htop showing how awesome my machine is at compiling - etc. You know what they say about shell windows - “Bet you can’t have just one!”. Wait… Nevermind.

terminator supports split windows, tabs, profiles, saved layouts (which I’ll come to in a minute), etc. I use it every day.

If you look online for this issue, you’ll probably come across recommendations for screen or tmux. These tools are useful, but require lots of key combination memorization. I’m all in for using the keyboard (I’m writing this blog post using Vim, although mainly because I haven’t figured out how to quit), but I prefer to have a GUI option.

I recommend you head down to their website RIGHT NOW and install it on your system. If you’re on Ubuntu, the command is:

$ sudo apt-get install terminator

Custom Layout

One of terminators useful features is layouts. When you work on certain project, you usually want the same layout of windows - one in the root of your projects to run your build system and / or source control commands, one in the build directory, and the last one tail -fing the system log. terminator lets you save these structures as layouts.

This is how the terminator layout setting window looks like:

(The photo is courtesy of this Stack Overflow question)

See that little “Custom command” textbox on the upper right side? terminator will run that command in the selected window. Most probably, we’ll want to run some shell (zsh for me) with a couple of commands specific to the window, like setting a Python virtualenv, running a certain script, etc.

However, there are a several problems with this feature.

To solve all of these problems, I put the following script in my ~/.zshrc (or ~/.bashrc, if you’re so inclined):

echo $INIT_CMD
if [ ! -z "$INIT_CMD" ]; then
    OLD_IFS=$IFS
    setopt shwordsplit
    IFS=';'
    for cmd in $INIT_CMD; do
        print -s "$cmd"  # add to history
        eval $cmd
    done
    unset INIT_CMD
    IFS=$OLD_IFS
fi

And the following snippet goes in the ‘Custom command’ textbox:

env INIT_CMD="cd bla; export PYTHONPATH=/tmp; workon project" zsh

That’s it. You get multiple custom commands inside a shell, with easy history access. Enjoy!

Discuss this post at the comment section below.
Follow me on Twitter and Facebook

Similar Posts