Configuration for Screenshot on i3wm

Photo of author

I use i3wm and it doesn’t have a screenshot configured by default. Found a tutorial recently that gave me ideas on how to have better screenshot shortcuts (see here). I have modified the configuration from that tutorial with some additional features.

Overview

Here are the shortcuts I now have for taking screenshot on i3wm:

FeatureShortcut
Full ScreenPrtScr
SelectionShift + PrtScr
Active WindowMod + PrtScr
Shortcuts for better Screenshot

I have also configured it with the following features:

  • Store the screenshots in ~/Pictures directory.
  • Copy the screenshot to the clipboard for ease of sharing.
  • Screenshots are accompanied by a sound effect.
  • Show a notification with an action to delete it.

Let’s start off with basic implementation of the configuration and we will then add more features to it as we go along.

Packages Required

# maim: to take sreenshots
# xdotool: to figure out active window id
# xclip: to manage clipboard
sudo apt install maim xdotool xclip

Once you have these packages, you can run the following commands to take screenshots:

# Full Screenshot
maim $FILE_PATH

# Select the rectangle on screen for Screenshot
maim --select $FILE_PATH

# Screenshot of current active window
maim --window $(xdotool getactivewindow) $FILE_PATH

Check out the man pages for maim and xdotool if you are curious how they work.

Create a Script for taking Screenshot

We can directly bind the commands from above with the PrtScrn key for a minimal setup, but if we want more features like sound and notification, it’s better to not clutter i3 config. A separate script will make things easier to maintain.

Let’s wrap the above commands into a script. I like to store i3-related scripts together with my i3 configs, so I created a file at ~/.config/i3/scripts/screenshot.sh.

#!/bin/sh

# Use the current timestamp as the unique filename of the screenshot.
FILE_PATH="/home/$USER/Pictures/screenshot-$(date -u +'%Y%m%d-%H%M%SZ').png"

main() {
    case $1 in
        full) maim --format=png $FILE_PATH ;;
        select) maim --select $FILE_PATH ;;
        window) maim --window $(xdotool getactivewindow) $FILE_PATH
    esac
}

main "$@"

With this script, we can now pass any of {full, select, window} string as a parameter and our script will handle the rest. Later we can add more commands to run before and after taking screenshots.

Keybinding Configuration for i3

Let’s bind our script with PrtScrn key before we add more features to it. Add the following lines at ~/.config/i3/config:

# Set a custom variable to invoke our script. Makes it easy to refer.
set $i3screenshot sh $HOME/.config/i3/scripts/screenshot.sh

bindsym Print exec --no-startup-id $i3screenshot full
bindsym Shift+Print exec --no-startup-id $i3screenshot select
bindsym $mod+Print exec --no-startup-id $i3screenshot window

Try taking some screenshots now. You will find that screenshots have been captured silently at ~/Pictures directory.

Add Shutter Sound Effect

Without a sound effect, you might feel clueless about whether or not a screenshot has been captured and end up pressing the button multiple times. Let’s add the sound effect to remove the confusion.

Just add the following line at the beginning of the screenshot.sh:

# Play shutter sound to indicate a screenshot has been taken.
paplay /usr/share/sounds/freedesktop/stereo/camera-shutter.oga

Copy the Screenshot to Clipboard

Often times, we want to share the screenshot we have taken with others. It’s handy to have it on clipboard so that we can just paste it on chat.

maim actually captures the image as a stream and supports piping of the output. So we can easily pipe it into our clipboard.

I decided to do both: capture the screenshot in a file AND copy it to my clipboard. Just update the maim commands in the script as follows:

# Example for updating full screenshot. Update others similarly.
maim --format=png | tee "$FILE_PATH" | xclip -selection clipboard -t image/png

[Optional] Actionable Notification that Deletes the Screenshot Using Dunst

Sometimes I end up taking the wrong kind of screenshot, so I configured a notification which I can interact with to delete the screenshot just taken.

I use Dunst for notification management, so I added this block of code to screenshot.sh and called it after the screenshot has been captured:

send_notification() {
    ACTION=$(dunstify --action="delete,Delete Screenshot" "Screenshot" "Stored at $filename")
    case "$ACTION" in
        "delete")
            rm "$FILE_PATH"
            dunstify "Screenshot" "Deleted $filename"
            ;;
        "2")
            # handle_dismiss
            ;;
    esac
}

Conclusion

I now have a decent flow for taking screenshot on i3wm. It was pretty fun considering I got to explore Dunst notification system a bit more here. I didn’t know it was possible to interact with the notifications via actions like this.

Until next time.

Samiul

Oh hi there 👋
It’s nice to meet you.

Sign up to receive updates in your inbox once a week.

I don’t spam! You can always unsubscribe if you don't like it.

🗞️ Don’t miss the latest posts!

Subscribe to the Weekly Newsletter

I won't spam. Promise!

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments