Clipboard tricks

Clearing the clipboard every 15 minutes

A few days ago, a member of our matrix room shared with us a small systemd timer and systemd unit to clear the clipboard every 15 minutes.

You can find the source code here. (credits to @cipherseven)

Quick instructions:

  • Create .config/systemd/user/
mkdir -p ~/.config/systemd/user
  • Create clipclear.timer with the contents:
[Unit]
Description=Run clipclear service every 15 mins

[Timer]
Unit=clipclear.service
OnCalendar=*:0/15

[Install]
WantedBy=timers.target
  • Create clipclear.service with the contents:
[Unit]
Description=run clear clipboard command

[Service]
Type=oneshot
ExecStart=/usr/lib64/qt5/bin/qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory 

[Install]
WantedBy=graphical.target
  • Enable the timer: systemctl --user enable --now clipclear.timer

Now our clipboard will be cleared on the clock at 0, 15, 30 and 45 minutes 🙂

pbcopy

Mac users are probably used to having this little helper pbcopy so I decided to create my own.

  • Create /usr/local/bin/pbcopy with the contents:
#!/usr/bin/env python
"""Copy input to clipboard."""

import os
import subprocess

if os.getenv("WAYLAND_DISPLAY"):
    with subprocess.Popen(['wl-copy']) as proc:
        proc.wait()
else:
    with subprocess.Popen(['xsel', '--clipboard', '--input']) as proc:
        proc.wait()
  • Execute permissions: chmod a+rx /usr/local/bin/pbcopy

NOTE: it uses two little programs: wl-copy on wayland and xsel on X11

NOTE: I created it in python only because I am forcing myself to write pyton code to become more proficient on it

tmux vi style copy/paste

If you are a tmux and vi/nvim user, perhaps you will find this trick useful. Add this to your .tmux.conf file:

# Enable vi on copy-mode
set -g mode-keys vi

# vi style copy/paste
bind p paste-buffer
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle \
                             \; send-keys -X begin-selection
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel 'pbcopy'
bind-key -T copy-mode-vi \
         MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'pbcopy'

And now you can use vi copy/paste style on your tmux and get it both into the tmux buffer and into the clipboard!

selection by columns vi style 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *