andreas.schaertl

Autocutsel and an Introduction to Systemd User Services

tech

This post shows how to run autocutsel as a systemd user service to keep X11 clipboards in sync.

X11 has two kinds of copy/paste buffers, namely the primary selection and the clipboard selection. The primary selection is invoked when highlighting text and with the middle mouse button while the clipboard selection uses Ctrl+C and Ctrl+V.

If you wish to keep both buffers in sync, you can use the autocutsel daemon to do just that, effectively merging the two buffers.

User Service

I used to just start autocutsel with my window manager, but there is a more clean solution, systemd user services. User services are like regular system-wide services, but they are only started when the associated user is logged in and kept running until all sessions of that users are closed.

  1. As with regular systemd services, we need a service file.

     [Unit]
     Description=Autocutsel
    
     [Service]
     Type = forking
     Restart = on-failure
     RestartSec = 10
     ExecStartPre = /usr/bin/autocutsel -fork
     ExecStart = /usr/bin/autocutsel -selection PRIMARY -fork
    
     [Install]
     WantedBy=default.target
    

    User service file go into the ~/.config/systemd/user directory. We will save above service file as

     ~/.config/systemd/user/autocutsel.service
    
  2. To control user services, use systemctl like usual, but with the --user flag. After saving autocutsel.service, first reload the database of services with

    systemctl --user daemon-reload
    

    and then enable the service

    systemctl --user enable autocutsel
    
  3. Log out and back on again. autocutsel should be running and keeping your clipboards in sync.

systemd will make sure that only one instance of the service is running at a time and restart the daemon if it crashes.