Ubuntu – Automatically modify clipboard content (regex pattern replacement) when copy-pasting

clipboardcommand linescriptssed

I'm often copying small separate text snippets and pasting them somewhere else. After that I always need to edit the pasted result in a well-defined way, which can be described through a regular expression replacement similar to e.g. sed's s/PATTERN/REPLACEMENT/ command.

I would like to automate the editing part of this task by having a script automatically processing the content of my clipboard buffer at some time between copying and pasting and modifying it by applying a configured regular expression replacement.

The actual regex pattern and replacement shall be entered by the user when the script gets launched and it also should somehow indicate that it is running though, e.g. by leaving a little dialogue window open and by sending a desktop notification each time a modification had been made.

Best Answer

Introduction

The way question is posted, requires more than just having a simple script, but also a GUI interface for enabling/disabling the script. I've found that it would be easier to achieve via an Application Indicator that would be accessible from Ubuntu's top panel. This answer presents exactly that as a solution.

The clipboard-autoedit-indicator , which I've written specifically for this question, allows for automatic editing of clipboard contents based on a regex expression ( choice of sed or python's re style ). It uses Gtk's clipboard module and minimum dependencies, thus no need for installing additional tools such as xclip or xsel.

Installation

Run the following commands in terminal:

sudo add-apt-repository ppa:1047481448-2/sergkolo
sudo apt-get update
sudo apt-get install clipboard-autoedit-indicator

Usage

The indicator can be launched by typing in Unity Dash Clipboard Autoedit or via calling /usr/bin/clipboard-autoedit-indicator from command-line. There are two indicator icons (colored and grayed-out clipboard icon) to display when dynamic editing of the clipboard is enabled/disabled. When editing occurs, notification is sent to the user.

enter image description here

The "Set regex pattern" menu item calls zenity forms dialog. When user edits regex rule, it is remembered and stored in ~/.clipboard-autoedit-config.json file. The dynamic editing will be enabled automatically when user clicks OK button, but users can disable it via "Replacement Enabled" check menu item. The regex type defaults to python's re module.

enter image description here

Source code

The source code is available under the MIT license on GitHub. Further development and newer versions will be there, and if you have a feature request and/or bug report, please submit them on GitHub.

Related Question