Setting up Emacs with Compiz and wmctrl

I recently spent some time to set up GNU Emacs 24.3.1 properly in my Ubuntu 12.04/14.04 environments with the Compiz windows manager. I wanted to

  1. start Emacs in a specified position and size, and
  2. set focus (activate) Emacs in the current workspace

using the Compiz window manager. Note that if you're using another window manager (find out by typing wmctrl -m in a terminal), most of the tips in this post will be useless to you.

Start Emacs in a specified position and size

I strictly keep Emacs on the right side of my screen and try to adjust the width to 80 columns. Emacs size can be controlled on start-up by passing the right set of parameters. In my setup I use

emacs24 -fh

to start in full height; the windows is 80 column wide by default.

To start in a specified position, I used the CompizConfig Settings Manager. Easily installed via

sudo apt-get install compiz compizconfig-settings-manager

the manager allows to set start-up window positions for classes of applications. In Place Windows selected the tab Fixed Window Placement. When adding a new fixed position for a class, note the grab functionality to easily extract the window class name.

Activate Emacs in the current workspace

The command line tool gives you (almost) everything you need to set the focus on your Emacs window. If you only ever have a single Emacs window open, all you need is

wmctrl -x -a Emacs

However, I like to have multiple Emacs instances across my workspaces. The solution to activate the Emacs window in the current workspace was given on AskUbuntu by user55822. Here's the script adapted to activate Emacs:

#!/bin/bash
# Adapted from user55822 on AskUbuntu: 
# http://askubuntu.com/a/195201/72546

SCREEN_W=$(xwininfo -root | sed -n 's/^  Width: \(.*\)$/\1/p')
SCREEN_H=$(xwininfo -root | sed -n 's/^  Height: \(.*\)$/\1/p')

NAME='emacs24.Emacs'

wmctrl -xlG | awk -v W="$SCREEN_W" -v H="$SCREEN_H" \
       -v NAME="$NAME" \
       '$7==NAME && $3>=0 && $3<W && $4>=0 && $4<H {print $1}' \
       | while read WINS; do wmctrl -ia "$WINS"; done

exit 0

Finally, set keyboard shortcuts to your custom commands/script in the Keyboard settings: