Clipboard utility to paste back multiple lines one by one

clipboardx11

I need a tool or utility that can take a text file and copy each line onto the clipboard in a way so I can paste back the text line by line by continuously pressing Ctlr-V.

For example, if the text holds these lines

line 1
line 2
line 3

Then I will get:

Ctlr-v: line 1
Ctlr-v: line 2
Ctlr-v: line 3

I need it for pasting lines into the terminal when debugging telnet sessions. It is very tedious to copy and paste the lines one by one.

Does such a tool exists, or can it be created using xclip or similar?

It is for Debian based distros if that makes any difference.

Note:
See don_chrissti's comment for a variation to the accepted solution that worked for me.

Addition:

This is the script I ended up with. Note the use of double backslash to retain the newlines from the text file.

while IFS= read -r line; do
  printf %s\\n "$line" |
  xclip -l 1 -quiet -selection primary
done < telnet

It works very well for testing smtp connections over telnet which is my use of it.

Best Answer

With xclip:

while IFS= read -r line; do
  printf %s "$line" |
    xclip -l 1 -quiet -selection clipboard -in
done < file.txt

Replace %s with %s\n if you need the newline included.

With -l 1 xclip holds the CLIPBOARD selection for one request (by other applications doing Ctrl-V for instance), and then exits. You need -quiet for xclip to do that in foreground.

That won't work if you have an application like xclipboard running. Those applications try to always be the owner of the CLIPBOARD selection, so will steal it continuously from xclip.

If you have such an application running, you can either suspend or kill it, or you could use the PRIMARY selection instead (-selection primary, or omit -selection as primary is the default) and paste using the middle mouse button. Many terminal emulators can paste the PRIMARY selection upon Shift-Insert, some other upon Ctrl-Shift-Insert.

If you want to know who is stealing the CLIPBOARD selection from xclip, this may work:

xwininfo -id "$(xclip -selection clipboard -o -t CLIENT_WINDOW | od -vAn -tu8)" -wm

provided that application offers the CLIENT_WINDOW target (run xclip -selection clipboard -o -t TARGETS to see if it does).

See also expect (and dejagnu for a testing framework based on expect) and GNU screen to automate inserting text into terminal applications.

Related Question