0

OS: Kubuntu 18.04 or Ubuntu 18.04

I want to remove the first seven characters of certain strings I copy. For example, I want to first copy and then convert

1234567890

to

890

I can do so with the following code:

xsel -b -o | cut -c 8-

I can put the code into a simple script and can run that script successfully in a terminal.

#!/bin/bash
xsel -b -o | cut -c 8-

But that does not help to paste the modified contents into a GUI-based text file . So I assigned keyboard shortcuts to the code directly or to the corresponding script. But either way, nothing happens in Kubuntu 18.04 or in Ubuntu 18.04 when I press the assigned keyboard shortcut.

Even

#!/bin/bash
bash -c 'xsel -b -o | cut -c 8-'

does not work.

Why is that? Is is something peculiar to xsel (and to xclip which poses the same issue)?

  • Keyboard shortcuts don't run in a shell, so you can not use stuff like pipes directly. You need to wrap the command in a bash -c '...' then or put it in a script file which you can call. Also as it is, your script/command outputs to the console, which is nonexistent when it runs as a keyboard shortcut. What do you want to happen with the result? Put it back into the clipboard? Append it to a fixed text file? Pop up a notification? You can also automatically watch for clipboard events and modify the contents instantly without extra shortcut, if that is what you need. Please clarify. – Byte Commander Nov 12 '18 at 14:19
  • I tried the bash -c route as well but had the same result. I want the modified string to be pasted into a GUI-based text editor or the terminal. No, I don't want to automatically watch for clipboard events. – DK Bose Nov 12 '18 at 14:27
  • 1
    Use xdotool in your bash script to simulate keystrokes for cutting and pasting text in your application, i.e. xdotool key ctrl+x to cut text to the clipboard – vanadium Nov 12 '18 at 14:42
0

This script works when bound to a keyboard shortcut:

#!/bin/bash

xsel -b -o | cut -c 8- | tr -d '\n' | xsel -b -i

After running the script, the trimmed string can be pasted into the destination file using standard paste methods.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.