Freelance engineer, maker of things.

i3 switch-to-or-launch application shortcut if it's already open, don't open it again

Published under // 

On my computer, I hit a 2-key combo to either launch emacs, or switch focus to the existing window if emacs is already running—even if it’s on another workspace. I do the same for Firefox.

I found this to be a very convenient hotkey and was surprised it’s not in the current i3wm documentation. Searching online now, I see it in the old FAQ with suggestions for a bash script and even a python script, but the truth is, it’s a small one-liner. In fact this is better without a script as it lends itself to a different execution command, like placing on a workspace or setting environment variables. So, I figured this warrants a quick post.

## quick shortcuts from oribarbut.com
# start or switch to emacs
bindsym $mod+x exec pgrep emacs && i3-msg '[class="Emacs"] focus' || emacs

# start or switch to firefox
bindsym $mod+b exec pgrep firefox && i3-msg '[class="Firefox"] focus' || firefox

The same sort of command would work for You’ll just need to change how the key is bound, and look up how your window manager is programmatically told to change focus , tiling or otherwise. I recommend it for any program you find yourself using constantly.