Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to focus an opened app instance/window using a command?

I'd like to assign a keyboard shortcut as to quickly switch to a specific app.

I want shortcuts for:

  • chromium (now it opens a new window with blank tab)
  • terminator (now it opens a new window)

It works fine for these (running the default command, eg. thunderbird):

  • thunderbird
  • sublime text 2

I am using gnome-shell @ 12.04 (shouldn't matter)

share|improve this question
up vote 5 down vote accepted

The wmctrl package offers access and manipulation of application windows and would do what you want. Install this little package first:

sudo apt-get install wmctrl

Here is the man page for wmctrl:

When you look at the command line switches, you will see that -a will raise and focus a window. In order to know which window to focus, make sure you have a Chromium window open, and then run

wmctrl -l

This will give you a list of all open windows and their titles. Chromium will most likely always have "Chromium" in the end of it's window title, so you can use that.

With this information at hand, you can construct your command for the shortcut. Open the Keyboard preferences, go to Shortcuts, click on Custom Shortcuts, click the + sign and enter:

Name: Focus Chromium Shortcut
Command: wmctrl -a 'Chromium'

I am using Unity and just tried this with Google Chrome, it did the trick. It should work in Gnome-Shell as well.

Pretty cool idea :)


share|improve this answer
Thanks a ton. Works as expected. As for the terminal, one can use username@pc_name. – mreq Dec 30 '12 at 9:07
I am glad it worked out. You could, if you wanted to, take it a step further and put the wmctrl command into a script. This script could then do further things, e.g. check if there is a window with that name at all, and if not, start the application. This way you could technically setup all your favorite applications to shortcuts, which will have two functions--either start the app if not started yet, or bring it into focus if it is already started... just as an idea if you feel like playing around a bit more. – DrTebi Dec 30 '12 at 10:11
Yes, that's exactly what I am going to do once I get some time ;) Too bad you can't modify apps' titles. You can't for instance target Clementine with wmctrl as it always has the title of what's playing. – mreq Dec 30 '12 at 10:34
This works pretty well, with one limitation being that it doesn't seem to be aware of Unity's workspaces. It does have a concept of Desktops, but these don't seem be the same thing. This means that wmctrl -a 'Google Chrome.' takes me to the Chrome window on another workspace simply because it occurs first in the window list. I have yet to work out a way to discriminate between windows on different workspaces. – nedned Feb 24 at 2:07
Actuall @mreq, wmctrl does support changing the title of a window with the -N flag. – nedned Feb 24 at 2:11

A few years ago, I created a small application called mcgr exactly for this purpose. It originally worked only with the Metacity windows manager, but I later adapted it for Gnome.

share|improve this answer
Seems good. I went even further with my own wmctile. – mreq Oct 27 '15 at 22:14
@mreq wow, looks great, tiling and focus-or-run in a single package! I've also since migrated to tiling window managers, starting with ion3 and moving on to awesome and on to Xmonad. None have focus-or-run put of the box, but it could be added with scripting using available plug-ins. Now I'm experimenting with Gnome-Shell and shellshape, so I need to find a good focus-or-run solution again. BTW, the doc link is broken on – akaihola Oct 28 '15 at 4:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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