I'm trying to create a script which processes selected text from the gui (any number of apps, like browser, text editor, etc) and then replaces the selected text with the processed text automatically using one keyboard command.
I can get xsel to replace the clipboard or primary buffer, but I don't want to have to paste with another keyboard shortcut, if possible.
Workflow:
- Select text
- Hit key command
- Bash script processes text
- Bash script pastes text into gui editor, replacing selection (<-- This is what I can't figure out)
- Fin
I've read that xsel allows pasting, but as far as I can tell, it only allows outputting text to cli (not really pasting) or redirecting to something else. This may be what I need, but I don't understand how to do it (i.e., to what do I redirect the output to get it to automatically replace selected text in the gui?)
Thanks!
Further info:
- I'm using Xubuntu 19.10
- I just want to replace with simple text (e.g. change lowercase to upper case)
- Let's keep this simple - how do I (using a keycommand) take text from a bash script and paste it into a gui text editor/area where the cursor currently is, replacing selected text if there is any?
While
bash
andxdotool
will definitely handle a simple case like this, this is exactly the sort of thing AutoKey was designed to do.In AutoKey, you can define a simple script written in Python using the AutoKey API to do this. This can be assigned to a hotkey and will run whenever that hotkey is pressed. It would look something like this (untested.)
which gets the selected text into a variable, converts it to lower case, puts it back in the clipboard, and then pastes it back into the current selection where it originally came from. (If your application window is a terminal, then you'd have to use Ctrl+Shift+v instead.)
Doing it this way has several advantages: It's arguably easier to do than modifying your keyboard. You can easily change the hotkey in the AutoKey GUI. You can define a window filter so the hotkey only works in the windows of your choice. You can turn this functionality on and off at will. And, since you have the entire power of Python at your disposal, you do almost anything else you can think of.
If you had just wanted to substitute one phrase for another, an AutoKey phrase would have done that without writing a single line of code, but since you needed to manipulate the text, a script was required.
Note: Debian and derivative distributions (Ubuntu ...) currently provide a very old version of AutoKey. This is fixed in Debian testing and will be fixed in Ubuntu 20.04. For now, you can easily install the package with these instructions.
For assistance, visit our support forum.
Since you are using Xorg and ctrl+v ist almost universally the shortcut for paste, the easiest solution is
xdotool
(available from the official repositories):The
--clearmodifiers
flag attempts to clear all modifiers (ctrl,alt...) before issuing the key presses and restores them afterwards. This is useful when you invoke your script via a keyboard shortcut, because otherwise the application will also see the modifiers you pressed for your shortcut, and e.g. ctrl+alt+v may result in a different command.Sometimes, this can result in a race condition when you release the modifiers before
xdotool
restores them, making them stuck. In that case, just press and release the stuck modifiers to unstick them.xdotool
can do a lot more. Readman xdotool
to get an overview. It is also often used in combination withxbindkeys
, which lets you configure keyboard shortcuts more versatilely than e.g. gnome and also enables you to bind actions to mouse buttons.I agree with Joe that AutoKey would be perfect for what you want to do. I thought I'd include another example script. This one uses string formatting to also change the selected text to lower case (you could have it do whatever you like instead of changing case):