Archive for the ‘Productivity’ Category

Often I find myself working on multiple source code clones or views simultaneously. And I like to have a separate screen session for each view/clone. In this post, I attempt to achieve two things.

  • Make it easier to correct the DISPLAY variable after reconnecting to an existing screen session.
  • Display current clone name on screen status line.

Screen shot of screen status line

First we decide on a suitable environment variable name to hold the view/clone name. CLONE_NAME is good enough. Now let’s define a bash function that will set the view/clone for us. Give this command to change the view/clone.

setclone ()
{
  if [ "$1" ]; then
    export CLONE_NAME=$1;
    SERVER_CLONE=`hostname`.$CLONE_NAME;
    export DISPLAY_FILE="$HOME/displays/$SERVER_CLONE";
    fi;
    echo clone=$CLONE_NAME;
}

Setclone exports two variables. CLONE_NAME is the name of the view/clone that we are going to work on. DISPLAY_FILE is the name of an ASCII file where we will store the current $DISPLAY variable before starting screen. We will use this later, from within screen. Now we define another function – clview (short for clone view) which actually starts the screen session.

clview ()
{
  if [ "$1" ]; then
    setclone $1;
  fi;
  rm -f $DISPLAY_FILE;
  echo $DISPLAY > $DISPLAY_FILE;
  screen -xR -S $CLONE_NAME;
}

This function writes the current DISPLAY variable to $DISPLAY_FILE (which was set by setclone). If we had started a screen session before and are reconnecting to it now, the DISPLAY variable in the existing screen session may have changed. So we have to set it up again. Instead of detaching from screen, finding the DISPLAY variable, reconnecting to screen and manually updating the DISPLAY variable, the clview function will put the correct value for DISPLAY variable in the file $DISPLAY_FILE. Now we write another function that will correct the DISPLAY variable for the screen sessions.

display ()
{
  echo old DISPLAY=$DISPLAY;
  export DISPLAY=`cat $DISPLAY_FILE`;
  echo new DISPLAY=$DISPLAY;
}

The screen options used are

       -x   Attach to a not detached screen session. (Multi display mode).
       -R   attempts  to resume the first detached screen session it finds.  If successful,
            all other command-line options are ignored.  If no detached session exists,
            starts a new session using the specified options, just as if -R had not been
            specified. The option is set by default if screen is run as a login-shell
            (actually screen uses "-xRR" in that case).
       -S sessionname
            When creating a new session, this option can be used to specify a meaningful
            name for the session. This name identifies  the  session  for  "screen -list"
            and "screen -r" actions. It substitutes the default [tty.host] suffix.

One thing that can be useful is for screen to display the current view/clone name on the status bar. Put the following the the screenrc file for a nice status line. The backtick command can query the shell and display its output in the status line. The query interval is set to 3600 seconds.

backtick 1 3600 3600 /bin/echo $CLONE_NAME
hardstatus alwayslastline "%{ck}%H: %{gk}%1` %?%{wk}%-Lw%?%{Yk}[%n*%f %t]%?%{kk}(%u)%?%?%{wk}%+Lw%?%=%{gk}%C %A %{Bk}%D, %M %d, %Y"

One side effect of the above status line is that it display the current time. So if we use the mouse to scroll back in history and the minute changes, it needs to be redisplayed. As part of the redisplay, we end up back at the prompt. You can disable this from the status line by removing “%{gk}%C %A ” (set green foreground, black background, time in 12 HR format, AM or PM).

I used to use Xming for an X server for my Windows 7 PC. I started looking out for other X servers for Windows because of a particularly irritating bug of Xming. Sometime Xming interferes with the clipboard, so that cut/copy/paste no longer works. The bug blocks the clipboard not only for X applications but also for native Windows applications. Quitting Xming doesn’t get the clipboard functionality back. Even after quitting Xming, a residual process would still be running which continues to block the clipboard. It has to be manually killed from the task manager.

Finally, I found an alternative that is as capable as Xming but doesn’t block the clipboard from time to time. Cygwin comes with an X server among many other things. Grab your copy here. It is an installer stub and will download additional stuff from the internet based on the components selected for installation. Cygwin/X which has the X server is not selected for installation by default. You can do it manually by selecting the Xinit package from the X11 section as shown. Cygwin has so many packages, that it is easier to search for what you want.

Screenshot of Cygwin X Server Installation

Cygwin installs a whole bunch of stuff (standard Unixy stuff). It is after all meant to provide a Unix style native environment in Windows. Even if you don’t use any of the other Cygwin stuff, it is worth it just for the X server. I am using Cygwin X Server for over a year now and it has never failed me. Also I put a shortcut to Cygwin X Server in my Startup folder so that it is automatically started whenever my PC boots.

I do most of my coding at work on a Linux server. My laptop at work runs Windows 7 and I can’t do much about it. So enter PuTTY – the ubiquitous terminal emulator for Windows. But I miss the eye candy of gnome terminal. PuTTY intentionally doesn’t support transparency or background images. So enter KiTTY – the slightly unstable and bloated PuTTY derivative that supports transparency, background images, automatic passwords, executing commands on remote machine among other things.

Get your copy of KiTTY here. Now let’s enable transparency and other eye candy. By default KiTTY doesn’t display options for setting background images. We can enable it by setting it kitty.ini file. That file is located on my Windows 7 PC at

 C:\Users\user_name\AppData\Roaming\KiTTY\kitty.ini.

Set this value to yes for KiTTY to enable setting background images.

backgroundimage=yes

The option will be available under the category “Window” as shown. Select your favorite image as the background and set a comfortable transparency level. Different profiles can have different images and other settings.

Screenshot of setting background image and transparency in KiTTY

If you want to dynamically control transparency of KiTTY, set this variable in kitty.ini

transparency=yes
transparencyvalue=0

If you are setting the background image and transparency, this option is not needed. This option is needed only if you want to change transparency dynamically. Ctrl + Up/Down increases/decreases transparency of the current frame.

Something that bothered me now was that KiTTY was capturing some of my key chords (Ctrl + arrow keys) instead of passing it on to the application on the remote server (screen in my case). Disabling shortcuts in kitty.ini solves this problem (you do lose all KiTTY shortcuts, which I don’t miss).

shortcuts=no

You can set post login commands which will be executed on remote machine after login. This is useful for stuff that cannot be done in .bashrc like becoming root. A command like this

"\psu\n\proot_pass\n"

would do the trick. The command is to be entered in the “Data” section of the config as shown. For details of what “\p” and other escape sequence stands for, check here.

Screenshot of KiTTY setting post login auto-commands

Even though KiTTY can save your login and password, I prefer using username@host.com and a putty private key file. Use PuTTY KeyGen to generate a private key/public key pair. Put the public key in ~/.ssh/authorized_keys file on a separate line on the remote machine. Give the path of the private key to KiTTY under “SSH->Auth” as shown. More details can be found here. It is worth the effort of creating a private key/public key pair for two reasons – 1) It is more secure 2) Even if the server takes time to respond, authentication will happen with the ppk file. KiTTY waits a certain amount of time and just pushes the password. If the server is taking time to respond, the password sent is lost and will have to be manually entered. Having a private key (ppk file) doesn’t have this problem, since it’s triggered by the server requests for authentication and not a predetermined timer.

Screenshot of KiTTY setting private key for authentication

Also if you plan to view X applications over the session, enable X forwarding (reverse ssh tunneling) under “SSH->X11” as shown.Screenshot of KiTTY enabling X forwarding (reverse SSH tunneling)

Most of the time I use Linux on my desktop and use the default X server for remote X clients. On the rare occasion of having to use Windows to ssh to a Linux server, my preferred X server is Xming. Now by default the fonts displayed by default X server on Linux is much more readable than the tiny size displayed by Xming. My initial solution was to increase the font sizes of the few applications (gvim, gnome-terminal) I typically used. But once I switched back to Linux desktop, the fonts were too big.

Xming properties screenshot

There is a better solution. By default Xming uses a DPI of 96. We can increase this value to have bigger font sizes (not only fonts, everything scales proportionately). I found anything in the range of 108-112 DPI to be appropriate. Xming takes the DPI config as part of its command line parameters.

Right click on the Xming icon and select properties. You should see a similar dialog box. You can edit the “Target” field (see screenshot) and add -dpi 108 to set the DPI config to be 108.

The final command should look something similar to

"C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow -dpi 108

Xming has a whole bunch of other options too. You can see a full list by giving

"C:\Program Files (x86)\Xming\Xming.exe" -h

A notepad with all options should popup.

EDIT: If you are having trouble with Xming hijacking the clipboard, you can give Cygwin X Server a try.