Automatically modify clipboard content (regex pattern replacement) when copy-pasting
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.
Solution 1:
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.
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.
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.
Solution 2:
This is straightforward with xclip
. For the notifycation bit notify-send
from the libnotify-bin
package should suit your needs. You may need to install the packages first:
sudo apt-get install xclip libnotify-bin
Put the following into a script and make it runnable with a keybinding:
#!/bin/bash
xclip -o | sed 's/pat/repl/' | xclip -i
notify-send "Clipboard" "Replacement done"