Wayland

Sway: Application startup via run-or-raise

In my Sway configuration I have two lines that start an application in a specific way:

bindsym Mod4+e exec ~/.config/sway/run-or-raise Emacs emacs
bindsym Mod4+w exec ~/.config/sway/run-or-raise Firefox firefox

What does this do?

First, the “exec” clause calls a python script, run-or-raise.

This python scripts wants two arguments. The first one is a container name. It uses swaymsg -t get_tree to get all outputs, work spaces and containers. And then it looks for any container (that is: application, Wayland client) that matches the name. This match is actually done case insensitive.

Sway: default initial layout

When I start Sway, I’d like to have Emacs on the left side of workspace 1, and a terminal (here Alacritty) on the right side, Unfortunately, this ~/.config/sway/config except won’t work:

exec emacs
exac alacritty

Because alacritty starts much faster than Emacs. So I’d get the terminal on the right side of the screen. And Emacs on the left. Not good.

Instead, I do this:

exec emacs
for_window [class="^Emacs$"] exec sh -c 'pgrep -x alacritty >/dev/null || alacritty'
for_window [app_id="^emacs$"] exec sh -c 'pgrep -x alacritty >/dev/null || alacritty'

That is, I start Emacs. And I add two for_window rules that fire when an Sway container with the named criteria is created. The first line is for Emacs compiled for X11. And the second line would fire if your Emacs is compiled for PGTK, which you should only do when you run Emacs under Wayland.