Linux: Add Keyboard Shortcut to Switch App

By Xah Lee. Date: . Last updated: .

Suppose you want to have F8 switch to browser. Here's how.

First, install wmctrl.

wmctrl is a command line util to {switch, close, move, resize, set title, list, …} windows.

# install wmctrl
sudo apt-get install wmctrl

Example use:

wmctrl -xa firefox
Switch to the window class name that contains “firefox”. (use this to switch apps.
wmctrl -a firefox
Switch to the window whose name contains “firefox”
wmctrl -c firefox
Close a window whose name contains “firefox”
wmctrl -l
List all windows

Type xprop in terminal to find the name of a app's window class name. 〔see Linux: Commands Related to GUI Apps〕)

here's some popular app's window class name:

Now, create a keyboard shortcut to the command.

In your Linux desktop {Gnome, KDE, Ubuntu, Xfce, …}, there's usually a keyboard setting tool in the control panel to set a key to execute command. Use that.

Here's a example from xfce:

xfce keyboard setting panel 2015-03-15
Xfce keyboard setting panel

LXDE / OpenBox

For LXDE or OpenBox, you need to modify a config file. See: Linux: LXDE/Openbox, Change Keyboard Shortcuts

Misc

xdotool

Here's another method.

Here's what you can do with xdotool to switch/launch app.

This is what I came up with to switch between my editor/browser/terminal using surplus numpad keys. (Tried wmctrl -xa first but it switches to the first terminal on the first desktop it finds.)

"xdotool search --onlyvisible --class  Geany   windowraise windowfocus"
    Print

"xdotool search --onlyvisible --class  Firefox   windowraise windowfocus"
    Scroll_Lock

"xdotool search --onlyvisible --class  Sakura   windowraise windowfocus"
    Pause

That's in ~/.xbindkeysrc I started using xbindkeys so I can switch WMs without redoing all my hotkeys.

JumpApp

run-or-raise application switcher for any X11 desktop. [ https://github.com/mkropat/jumpapp]

The idea is simple. Launch the application, if it's not already running, or Raise the app's window, if it is running. Pressing the key again will cycle to the application's next window, if there's more than one.

2012-09-15 Thanks to {Yuri Khan, Vadim Atlygin, Evan Cofsky} for help.

2014-11-30 Thanks to Tom Novelli for help.

Mac and Microsoft Windows

How to Set Key to Switch to Browser