About a year ago the Gnome developers took away the option to run Gnome terminal with the –title parameter. This allowed you to give the terminal window a custom name like ‘SSH@MYBOX’ or ‘myProject’ instead of just ‘Terminal’. Why did they do that? I don’t know. It seems to just be what Gnome developers do these days.
If you prefer separate terminal windows for separate tasks, naming those windows is a nice way to tell them apart. This applies to general orientation (i.e. looking at the windows in the Activities overview and deciding which one to click on) and for scripting purposes (i.e. writing a script targets windows by title).
For me personally this latter option is the more important. I have a local terminal and a remote terminal and I would like be able to access either quickly and easily with a simple keyboard shortcut rather than mucking about with tabs. Here I will detail a way to accomplish that without the missing –title parameter.
Here’s the short version: We create distinct Gnome Terminal profiles for our distinct terminals. For each profile we create and run a small shell script that writes the window’s ID to a /tmp file at the moment we start the profile. Then, whenever we need to find our window, we simply read that /tmp file to get the window id and use xdotool to fetch us that window. This implementation will not rename the windows though I am sure that it can be used for such a purpose in one way or the other.
I guess I have to preface this sort of hack with the notice: This only applies to windows under the X Window System, not Wayland or Mir. With Wayland rumoured to be the default setting in the next Fedora version, solving problems this way is somewhat shortsighted. I guess I’ll deal with that in time. For now, let’s hack.
Get the WindowID
In Gnome Terminal (or probably any xterm descendant) echo the environment variable $WINDOWID. You’ll get an eight digit number that uniquely identifies this window. Put it in a script that writes it to a /tmp file, something like
#! /bin/sh echo $WINDOWID > /tmp/localsession bash rm /tmp/localsession
The second line simply starts an interactive shell – otherwise the terminal would run the script and exit. If you are using a multiplexer like GNU screen og tmux you should start that instead of the shell directly. Or set it to ssh into your server.
#! /bin/sh echo $WINDOWID > /tmp/localsession tmux attach || tmux new rm /tmp/localsession
The final line will only run once the shell in the terminal exits. We remove the /tmp file so it’s clear that the window is no more. You’ll ned a distinct script and /tmp file for every distinct terminal window you wish to have.
Incidentally I found documentation on the environment variable somewhat hard to come by. This old Solaris man page was my only source.
Set up a Gnome Terminal profile
Next you create a distinct Gnome Terminal profile for every distinct terminal window. Profiles allow you to set custom colors and fonts and the like. They also let you run a profile unique script at the start of the session. What they will not do is let you write an alternate title for the profile’s window because that functionality was removed. Yes. Ok. Fine. Onward. Tie the profiles together with the scripts you created earlier. It’s probably a good idea to use the same name for the profile, the script and the tmp file. You tell Gnome Terminal to use a specific profile byr running it with the –window-with-profile=$SESSION_NAME parameter.
Now whenever you start a specific Gnome Terminal profile it will write the window’s id to a /tmp file associated with that profile. This way we have a link between a specific window and a name that we recognise. Whenever I run my localsession profile – i.e. a purely local terminal – a little file called localsession is created with a number that tells me what window my localsession is in. No matter how many Gnome Terminal windows using other profiles I spawn I can always find the localsession one now. If I close the profile’s terminal window down and start it again, the file will get overwritten with a new window id. Note that this hack does not support running multiple terminals using the same profile.
You can put this link to various uses. If you’re hellbent on naming the window, there are probably ways to do that. What I am going to do is find a way to switch to the window I am interested in: One keyboard shortcut for the local session and one for the remote.
Get the right window with xdotool
Which is fortunately quite easy:
xdotool windowactivate $(cat /tmp/localsession)
Xdotool is one my go-to window manipulation tools and it doesn’t fail us now. windowactivate is xdotool’s combined ‘give focus + switch to the window’s desktop’ command (unlike windowfocus which only does the first thing). The $(cat /tmp/localsession) supplies the windowactivate command with the WINDOWID value we stored in /tmp/localsession when we started the Gnome terminal profile.
To tie the whole thing together I use the technique I wrote about in part four of my OCDish Everything in it’s right place series which is simply trying to activate the window and when that fails, start the program from scratch (with the desired profile):
xdotool windowactivate $(cat /tmp/localsession) || gnome-terminal --window-with-profile=localsession &
This is incidentally also why we use xdotool rather than wmctrl here because wmctrl doesn’t fail explicitly when using window id’s. So wmctrl -ai “thereisnosuchid” will exit with a zero indicating success despite the obvious failure. We need proper failure for our purposes. For more on this sort of thing or setting it up with keyboard shortcuts, there is this.
So there it is. I am aware that the option to set a title is still present in XFCE4’s terminal and others which are perfectly usable in Gnome. This is a somewhat uncertain hack for those who would rather stick with Gnome’s terminal despite it’s limitations.