Friday, October 3, 2008

Setting Up a Multi-Tabbed GNOME-Terminal Development Environment

For many developers (such as myself), the first thing we tend to do when we login to our desktops, is to launch gnome-terminal (or similar) with a number of tabs and then will typically have to setup a bunch of environment variables in each tab.

Most of us have probably simplified this at least somewhat by creating a file that sets our environment variables for us when we do

. ~/project.env
or something to that effect.

The problem is that if we have multiple tabs that we've got to source our project.env files in, this gets tedious.

This morning, Rolf shared with me a way to spawn gnome-terminal and have it automatically cd into a list of specified working-directories... but I wanted to take it a step further and have gnome-terminal auto-magically source my project environment variables.

Here's how I solved this problem:

* Step 1: Create a ~/bin/devterms shell script:

#!/bin/bash -e

MONO_ENV="/bin/bash --rcfile ~/.devtermsrc -i"

gnome-terminal \
  --tab --working-directory=/cvs/moon --command="$MONO_ENV" \
  --tab --working-directory=/cvs/moon/test --command="$MONO_ENV" \
  --tab --working-directory=/cvs/mono --command="$MONO_ENV" \
  --tab --working-directory=/cvs/monodevelop --command="$MONO_ENV" \

* Step 2: Create ~/.devtermsrc rc file for bash:

# Load ~/.bashrc to setup all of my standard environment variables and aliases
test -s ~/.bashrc && . ~/.bashrc || true

# Load Mono-specific development environment variables
test -s ~/mono.env && . ~/mono.env || true

* Step 3: Create a launcher (in my case, I made ~/Desktop/Development Environment.desktop):

[Desktop Entry]
Name[en_US]=Development Environment
Name=Development Environment

Hopefully this will help other people make their own lives a little simpler.


C.J. said...

17:40 <@cj> jeff: very nice. when I start running linux on my dev machine again (oh, please, soon!) I'll use that :)

Marco Barisione said...

Interesting, I never thought about that and --tab is not documented in the man page (at least on Debian).

Only one problem, I didn't find a way to move the focus back on the first tab. Do you know a solution for this?

Jeffrey Stedfast said...

haven't figured that one out either :(

alwayslurking said...

You can do similar things using screen and custom screenrc files, with all the added persistence and functionality that screen supplies. My screenrc;

termcapinfo xterm* G0:is=\E[?4l\E>:ti@:te@
termcapinfo linux me=\E[m:AX
hardstatus lastline
hardstatus string '%{= kG} [%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'
screen -t local -T xterm -a
screen -t root -T xterm -a su -
screen -t piccolo -T xterm -a
stuff "source p_alias_bsh ^M "
stuff "cd /qa1/piccolo ^M "
screen -t ingres -T xterm -a su - ingres
screen -t temp -T xterm ~/linux_screen
select 0

And the linux_screen script, with passwords redacted;

sleep 3
screen -p 1 -X stuff 'XXXXXXX^M'
screen -p 3 -X stuff 'XXXXXXX^M'
sleep 1
screen -p 3 -X stuff '. .loadIIbsh^M'

Extending that idea with environment variables allows me to generate multi-tabbed testing environments on remote servers with very little typing.

Very useful if you work from more than one location, as you can just pick up the session from another system, or if you have connectivity issues, as the screen persists even if you disconnect.

Aaron L. said...

Only one problem, I didn't find a way to move the focus back on the first tab. Do you know a solution for this?

gnome-terminal has the --active switch you can add after a --tab option to control that.

gnome-terminal \
--tab --active --working-directory=/cvs/moon --command="$MONO_ENV" \
--tab --working-directory=/cvs/moon/test --command="$MONO_ENV"

Jeffrey Stedfast said...

Aaron: Thank you!

This is awesome!

KaZJote said...

Thanks for the tip - it was something I was looking for :)

Code Snippet Licensing

All code posted to this blog is licensed under the MIT/X11 license unless otherwise stated in the post itself.