Preserving your working directory in gnome-terminal

I use gnome-terminal for most of my hacking. In fact, I use it so much, that I’ll often have multiple tabs open for a particular project. Here’s my workflow:

  1. Control+Alt+t (My shortcut to open a new gnome-terminal window.)
  2. cd ~/code/some_cool_hack/ # directory of some cool hack
  3. Control-Shift-t (Shortcut to open a new gnome-terminal tab.)
  4. Hack, hack, hack…

The problem is that the new tab that I’ve created will have a $PWD of ~, instead of keeping the $PWD of ~/code/some_cool_hack/, which is the project I’m working on!

The solution is to add:

# including this ensures that new gnome-terminal tabs keep the parent `pwd` !
if [ -e /etc/profile.d/vte.sh ]; then
    . /etc/profile.d/vte.sh
fi

to your ~/.bashrc. Now everything works perfectly!

Many thanks to Matthias Clasen and Ray Strode for figuring this one out!

One side note: this used to be the default, but for some reason it broke around Fedora 19 or 20. Maybe it had to do with my fancy prompt, but everything is working great now.

Happy Hacking,

James

 

Desktop Notifications for Irssi in Screen through SSH in Gnome Terminal

I’m usually on IRC, but I don’t often notice incoming pings until after the fact. I had to both write, and modify various scripts to get what I wanted, but now it’s all done, and you can benefit from my hacking by following along…

The Setup

Laptop -> Gnome-Terminal -> SSH -> Screen -> Irssi

This way, I’m connected to IRC, even when my laptop isn’t. I run irssi in a screen session on an SSH server that I manage, and I use gnome-terminal on my laptop. If you don’t understand this setup, then you’ll need to get more comfortable with these tools first.

Fnotify

The first trick is getting irssi to store notifications in a uniform way. To do this, I modified an irssi script called fnotify. My changed version is available here. Installation is easy:

# on your ssh server:
cd /tmp; wget https://dl.dropboxusercontent.com/u/48553683/irssi/fnotify.pl
cp /tmp/fnotify.pl ~/.irssi/scripts/
# in irssi:
irssi> /load perl
irssi> /script load fnotify

When someone sends you a direct message, or highlights your nick on IRC, this script will append a line to the ~/.irssi/fnotify file on the SSH server.

Watching fnotify

On your local machine, we need a script to tail the fnotify file. This was surprisingly hard to get right. The fruit of my labour is available here. You’ll want to copy this script to your local ~/bin/ directory. I’ve named this script irssi-fnotify.sh. This script watches the remote fnotify file, and runs notify-send and paplay locally to notify you of any incoming messages, each time one comes in.

SSH Activation

We want the irssi-fnotify.sh script to run automatically when we connect to our SSH server. To do this, add the following lines to your ~/.ssh/config file:

# home
Host home
    HostName home.example.com
    PermitLocalCommand yes
    LocalCommand ~/bin/irssi-fnotify.sh --start %r@%h

You might also want to have other directives listed here as well, but that is outside the scope of this article. Now each time you run:

ssh home

The irssi-fnotify.sh command will automatically run.

Magic

I’ve left out some important details:

  • The LocalCommand that you use, must return before ssh will continue. As a result, it daemonizes itself into the background when you invoke it with –start.
  • My irssi-fnotify.sh program watches the parent ssh $PID. When it exits, it will run a cleanup routine to purge old notifications from the fnotify file. This requires a brief SSH connection back to the server. This is a useful feature!
  • You may wish to modify irssi-fnotify.sh to paplay a different alert sound, or to avoid making noise entirely. The choice is yours.
  • When irssi-fnotify.sh runs, it will tail the fnotify file over ssh. If there are “unread” messages, tail will try to “download” up to ten. You can edit this behaviour in irssi-fnotify.sh if you want a larger initial backlog.
  • The irssi-notify.sh script doesn’t attempt to prevent flooding, nor does it filter weird characters from incoming messages. You may want to add this yourself, and or /kb users who cause you to need these features.

Here’s a little screenshot (with shameless plug) of the result in action:

irssi-fnotify.sh notification screenshot

Here’s an example of how this helps me to be more responsive in channel:

helping out in #gluster

helping out in #gluster

I hope you found this useful.

Happy Hacking,

James