Taking Screenshots on Wayland
— 579 words — 3 min
I wanted to have a convenient way to take screenshots on sway with different modes:
- capture only a region selected with the mouse
- capture the focused window
- capture the focused output (monitor)
- capture all outputs
I put together a simple script that does exactly that. Depending on the first argument passed to the script, it enters a different mode. That makes the bindings look nice and maintainable in the sway configuration.
On Arch Linux you can install the required packages with
You can save the following script somewhere, for example in
~/.local/bin/screenshot.sh and make it executable with chmod +x ./screenshot.sh.
#!/usr/bin/env sh
# Take a screenshot on wayland with swaymsg, jq, grim, slurp, and swappy
#
# Make sure the script is executable (chmod +x ./screenshot.sh)
#
# If you don't use sway, replace `swaymsg` with whatever your window manager
# gives you to query window information.
#
# Example sway configuration
#
# bindsym Print exec ~/.local/bin/screenshot.sh region
# bindsym Shift+Print exec ~/.local/bin/screenshot.sh window
# bindsym Ctrl+Print exec ~/.local/bin/screenshot.sh output
# bindsym Ctrl+Shift+Print exec ~/.local/bin/screenshot.sh all
# region|window|output|all
mode=""
As you can see, I also included example bindings for sway. How you can
change the bindings should be rather obvious even if you are not too
familiar with sway: just change the first argument of the script to a
different allowed value or change the symbol (key) that this exec
command is bound to.
Copy and paste the following bindings into your sway configuration;
probably at ~/.config/sway/config.
Print exec ~/.local/bin/screenshot.sh region
Shift+Print exec ~/.local/bin/screenshot.sh window
Ctrl+Print exec ~/.local/bin/screenshot.sh output
Ctrl+Shift+Print exec ~/.local/bin/screenshot.sh all
Reload sway and you’re done!
How does it work?
Let’s take a look at the first command that I bound to the Print key.
|
If you press the Print key, then a white-ish overlay will appear and
you can select the region of the screenshot with your mouse. This part
is done by the program slurp. It will print the start
coordinate (x,y) and selected dimensions (<width>x<height>) to
stdout which is fed to grim -g. Grim will go ahead and
capture the content at the provided coordinate with the given dimensions
and write the result to stdout which in turn is read on stdin by
swappy. Swappy displays the image and lets you annotate it
by drawing something or adding text or forms. Then you can copy it to
your clipboard or save it to disk.
The other commands work in a similar way. The trick is to pass the right
coordinate and dimensions to grim. Hence, on sway you can use
swaymsg and jq to extract that information when you only want to
capture the focused window or output.
Other suggestions? Get in touch!
Articles from blogs I follow around the net
Resetting Mutt's terminal after OAuth2 / GPG
A quick fix for how to get mutt's terminal reset after shelling out to GPG during the OAuth2 flow.
via Signs of Triviality March 31, 2026Status update, February 2026
Hi all! Lars has contributed an implementation independent test suite for the scfg configuration file format. This is quite nice for implementors, they get a base test suite for free. I’ve added support for it for libscfg, the C implementation. I’ve spent some…
via emersion February 22, 2026Generated by openring