Linux: Add Keyboard Shortcut to Switch App
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:
Firefox
google-chrome-stable
→ Google Chrome browsergnome-terminal
emacs
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:

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" PauseThat's in 〔~/.xbindkeysrc〕 … I started using xbindkeys so I can switch WMs without redoing all my hotkeys.
P.S. - found a switch-or-launch script for Linux: https://github.com/mkropat/jumpapp Doesn't work well with systray apps but my goal is to banish those.. map them to W-F1 etc.
JumpApp
https://github.com/mkropat/jumpapp → A faster way to launch applications and switch between them
The idea is simple — bind a key for any given application that will:
- launch the application, if it's not already running, or
- focus the application'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
If you have a question, put $5 at patreon and message me.