Jorisvandijk.com

Living on Linux

My i3 config: Part three - SwitchLaunch

A feature I have missed in i3 has been run-raise, or switch-launch. What I mean by this is a way for me to press a key combination, and have the right application launch or if already running, switch to it.

So say I press Mod+g then Gimp will launch, unless it's already running, in which case, i3 will switch to the workspace that's running Gimp. The following script I wrote does that for me.

#!/usr/bin/env bash

# SwitchLaunch 1.0
# Launch an application if it is not running, or else switch to it.
# Dependencies: i3, wmctrl, kitty
#
# By Joris van Dijk | Jorisvandijk.com
# Licensed under the GNU General Public License v3.0

i3-msg workspace "${1^}"

if [ $1 == newsboat ]; then
wmctrl -x -a "mpv" || wmctrl -x -a "$1" || kitty --hold --detach --class newsboat -e newsboat
else
wmctrl -x -a "$1" || $1
fi

Important to note is that the if-else logic there is only needed because I want to do some fancy stuff with MPV/Newsboat. The switch-launch magic is in these lines:

i3-msg workspace "${1^}"
wmctrl -x -a "$1" || $1

I call this script from my i3 config like this:

bindsym Mod4+g exec --no-startup-id bin/SwitchLaunch gimp

So pressing Mod4+g will run the SwitchLaunch script and it will pass along the name of the application. The script will then go to a workspace called "Gimp" and if Gimp is not running there, it will run it.

I can't stress how much this little script has improved my i3 workflow, please feel free to try it for yourself.