1

I usually have several applications running on my workstation. Usually I need VIM, Google Chrome and Terminal and some other applications that I don't always need, e.g. mail client, IM, VLC, etc.

It would be very handy if I could switch to applications I need always like VIM with just one shortcut (let's say, Ctrl+Shift+V).

How it can be achieved?

2
  • 1
    What desktop environment (if any) are you using, and what version? eg Gnome 2.x, Gnome 3.x, KDE 3.x, KDE 4.x, XFCE etc – Paul May 10 '12 at 5:53
  • Gnome 2.30.0 .. – Nemoden May 11 '12 at 1:12
2

This is a script I use to do this with irssi, with extra logic since I use it within a tmux session, but some parts are applicable. It actually also contains code to make it work remotely over SSH and control GUI notification of new irssi messages, but I've tried to strip that out (including usage instructions and argument handling) for clarity. I leave some debugging printf statements.

#!/bin/sh
# Locally raise/attach/start irssi session.
#
# Designed for use with tmux and irssi.

TMUX="/usr/bin/tmux"
TERMINAL="/usr/bin/urxvtc"
IRSSI="/usr/bin/irssi"

WINDOWNAME="irssi"
SESSIONNAME=${WINDOWNAME}

tty -s && TTY=true || TTY=false
ttyprint()
{
    ${TTY} && printf -- '%s: %s\n' "${0##*/}" "${1}"
}

start_irssi()
{
    ttyprint 'Starting irssi...'
    if ( ${TMUX} new-session -d -s "${SESSIONNAME}" "${IRSSI}" && ${TMUX} rename-window -t "${SESSIONNAME}:0" "${SESSIONNAME}" ); then
        ttyprint ' done.'
        return 0
    else
        ttyprint ' failed.'
        return 1
    fi
}

attach_irssi()
{
    if ${TTY}; then
        ttyprint 'Attaching irssi...'
        if printf -- '\033]0;%s\007' ${WINDOWNAME} && ${TMUX} attach-session -t "${SESSIONNAME}"; then
            ttyprint ' done.'
            return 0
        else
            ttyprint ' failed.'
            return 1
        fi
    elif ${TERMINAL} -title "${WINDOWNAME}" -e ${TMUX} attach-session -t "${SESSIONNAME}"; then
        return 0
    else
        return 1
    fi
}

raise_irssi()
{
    ttyprint 'Searching for available session...'
    if WID=$(xdotool search --limit 1 --name "^${WINDOWNAME}$"); then
        ttyprint ' found, activating.'
        xdotool windowactivate "${WID}"
        return 0
    else
        ttyprint ' not found.'
        return 1
    fi
}

! raise_irssi && ! attach_irssi && start_irssi && attach_irssi

In short it uses xdotool (package: xdotool in Debian) to search for a window with title "irssi" and then activates that window. There are other properties than window name one can search for if those better fit your needs.

I'm using a small "trick" to name the terminal "irssi" when it is first started (see the printf statement in attach_irssi()).

The last line tries to

  1. Raise an existing terminal session, and if not found
  2. attach an existing tmux session, and if not found
  3. start a tmux session and then
  4. attach it.

I bind this script to Mod4+I (exactly how this is done is up to your window manager).

With some effort this should be possible to convert to your case.


EDIT: Specifically you can strip out the complete attach_irssi function if you don't use tmux or GNU Screen. If you don't run it over SSH or as an irssi plugin, you can easily throw out the TTY logic. Very condensed, this would work for irssi without tmux (but it is untested):

#!/bin/sh
TERMINAL="/usr/bin/urxvtc"
IRSSI="/usr/bin/irssi"
WINDOWNAME="irssi"
SESSIONNAME=${WINDOWNAME}

start_irssi()
{
    ${TERMINAL} -title "${WINDOWNAME}" -e "${IRSSI}"
}

raise_irssi()
{
    if WID=$(xdotool search --limit 1 --name "^${WINDOWNAME}$"); then
        xdotool windowactivate "${WID}"
        return 0
    else
        return 1
    fi
}

! raise_irssi && start_irssi
0

If you don't already have gconf-editor installed:

sudo apt-get install gconf-editor

Then launch it as a normal user (shortcuts are a per-user setting):

gconf-editor

We are going to use one of the placeholder commands to do what you want, so navigate to:

apps > metacity > keybinding_commands

Choose command_1 and edit it to launch your chosen command:

/usr/bin/vim

Then go to

apps > metacity > global_keybindings

Find command_1 and change the value to the key you want to use:

<Control><Alt>v

(I changed this to Control-alt-v as control-shift-v is often used for paste where control-v is used for other things).

1
  • 1
    He wants to "switch to" the open program session, not start a new one. – Daniel Andersson May 11 '12 at 10:19
0

Based on @Daniel_Andersson's answer, this is the script I have bound to my keyboard shortcuts to raise Firefox window (and bring the mouse over it, because I'll easily forget on which screen it was before switching).

#!/bin/bash
if WIDS=$(xdotool search --name 'Firefox$'); then
  for WID in $WIDS
  do
    if $(xwininfo -id $WID | grep -q IsViewable) 
    then
        xdotool windowactivate "${WID}" mousemove --window "${WID}" 50 50 
        break
    fi
  done
else
    notify-send "Firefox not running" 
fi

Because Firefox always return a number of results in xdotools, one has to find one which is an actual window. I've found that xwininfo (package x11-utils) returned a useful IsViewable flag, which I use to find the first viewable window. The notify-send (package libnotify-bin) is of course optional, one could also start a firefox session if inactive (see @Daniel_Andersson's script).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.