10

I'm using Ubuntu 10.10 and I always have opened windows:

Browser, console, vim and Skype. I want to set specific shortcut-keys for those windows. Is there a way to use for example:

Win-C = Google Chrome
Win-T = terminal
Win-S = Skype

I want use hotkeys to switch between these windows.

1
  • not that in unity you will not be able to use most Win+[anything] combinations as it steals several of those (e.g. Win+t, opens trash) and there's no way to override or change that. reported on this bug that has no love bugs.launchpad.net/unity/+bug/823142
    – gcb
    Feb 25, 2013 at 21:26

5 Answers 5

8

If you prefer an existing package there is wmctrl which supports many different window managers:

The tool provides command line access to almost all the features defined in the EWMH specification. It can be used, for example, to get information about the window manager, to get a detailed list of desktops and managed windows, to switch and resize desktops, to make windows full-screen, always-above or sticky, and to activate, close, move, resize, maximize and minimize them.

Install it:

sudo apt-get install wmctrl

List existing windows:

wmctrl -l

And map:

wmctrl -a <WIN>

to a keyboard shortcut.

7

It is possible to achieve this with a python script. The script requires python-wnck and python-gtk to be installed in order to work, although I think these are installed by default anyway.

Copy and paste this into a text editor and save in a sensible place (eg. switch.py in your home folder):

#!/usr/bin/env python2
import wnck
import gtk
import sys
import time

screen = wnck.screen_get_default()

while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()

for w in windows:
    if len(sys.argv) > 1:
        if w.get_application().get_name() == sys.argv[1]:
            w.activate(int(time.time()+1))
            break
    else:
        print("Application name of window with title " + repr(w.get_name()) + " is " + repr(w.get_application().get_name()))

You can then set up the keyboard shortcut by opening Keyboard Shortcuts (System->Preferences->Keyboard Shortcuts).

Click add to create a new shortcut.

enter image description here

Use the command bash -c 'python ~/switch.py Terminal' (this is assuming you saved it as switch.py in your home folder). Replace 'Terminal' with the application name of the window you want to switch to. To find out the application names of the currently opened windows, run python ~/switch.py in a terminal.

enter image description here

You can then assign your preferred keyboard combination to this action.

6
  • how can i determine a window name for GVIM ? i setted up shortcuts with your script for my chrome and terminal, but i cant setup it for GVIM.
    – RusAlex
    Jun 20, 2011 at 14:26
  • I get it as 'Vim'. (Running python ~/switch.py should give you a list including something like Application name of window with title '[No Name] - GVIM' is 'Vim')
    – dv3500ea
    Jun 20, 2011 at 14:32
  • doesn't work for me. maybe do you have a script to determine a window names ? =)
    – RusAlex
    Jun 20, 2011 at 14:34
  • 1
    If you run python ~/switch.py with no arguments it will list all of the windows and tell you the title and application name.
    – dv3500ea
    Jun 20, 2011 at 14:40
  • wow, thats good. but it seems script just show a window, and didn't focus it. maybe some script changes can help ?
    – RusAlex
    Jun 20, 2011 at 14:44
3

This is possible in Kubuntu (kwin)... click on the titlebar of an application choose "advanced->Window Shortcut". I know that doesn't exactly answer your question - I am curious to know if Gnome/Unity can do this as well.

1
  • yes, it seems like it's just in kde
    – RusAlex
    Jun 20, 2011 at 14:28
3

On recent Ubuntu versions, you can use Super + 1 to focus (or open) the first app in the sidebar, Super + 2 for the second, and so on. So you might not need any other tool. You just have to have the icons in the order you want, for example, in the following image:

  • Chrome is Super + 1
  • Alacritty is Super + 2
  • VS Code is Super + 3
  • Slack is Super + 4
  • etc

Screenshot

0

The following adaption of the code from @dv3500ea worked for me, including setting the focus:

#!/usr/bin/env python2
import wnck
import gtk
import sys
import time

screen = wnck.screen_get_default()

while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()

for w in windows:
    if len(sys.argv) > 1:
        if w.get_application().get_name() == sys.argv[1]:
            w.activate(gtk.gdk.x11_get_server_time(gtk.gdk.get_default_root_window()))
            break
    else:
        print("Application name of window with title " + repr(w.get_name()) + " is " + repr(w.get_application().get_name()))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .