banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Middle-click paste a series of numbers or letters

I'm not sure I really need this trick, but it might come in handy someday. It's a scripted command that allows me to paste a series of numbers or letters, one after the other, using middle-click pasting from the X primary clipboard. You can think of it as loading the X clipboard with character "bullets" and firing them in sequence when you press the middle-click "trigger".

Here's an example script using the series 1-5:

#!/bin/bash
for i in {1..5}; do xclip -l 1 -r -quiet <<<"$i"; done
exit 0

I launch the script with a keyboard shortcut. The GIF below shows what happens as I middle-click-paste the numbers 1 to 5 in a text editor. I left a space after each word in the list and pasted after the space. (The GIF will loop 10 times, then stop.)

series-gif

Notice that after the script runs out of numerical "bullets", nothing is pasted after "pineapples", and the X clipboard will accept a fresh load of highlighted text.

The trick is based on xclip options. The first one, -l, tells xclip (according to the man page) the "number of X selection requests (pastes into X applications) to wait for before exiting". I've set the option value to "1", meaning that xclip pastes an item just once before waiting for its next input.

The -r option strips off any newline character at the end of the item to be pasted.

The -quiet option (according to the man page) tells xclip to "show informational messages on the terminal and run in the foreground". It also shows that xclip is waiting for the next input. If you run the command above in a terminal and paste 1-5 somewhere else, you can see a series of 5 paired messages in the terminal, all like this one:

message

Without the -quiet option, xclip just pastes the last item in the series.

The series that work in a for loop with "{..}" are numbers, lowercase letters (like "a..j") and uppercase letters (like "C..G"). The for loop also works with the seq command, meaning you can specify the interval between pastes. Here's the result (not a GIF) of pasting with the command beginning

for i in $(seq 3 2 11);...

list2

Last update: 2024-07-26
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License