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.


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.


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.

  • 2
    Unfortunately this doesn't work. When I run the script, all I get is this output: Waiting for one selection request. Waiting for a selection request.Waiting for one selection request. And the clipboard is empty afterwards. – marlar Jun 26 '13 at 7:57
  • @mariar, The idea is that xclip holds the selection for 1 request with the current line and once it has been queried, do the same with the next line. Normally, when you press CTRL-V in another application, that application should query the selection. Maybe you have some application like xclipboard that continuously queries the selection if it's not its owner. You could try using another selection like the primary selection (middle-click to paste) or suspend xclipboard or whichever application steal the selection. – Stéphane Chazelas Jun 26 '13 at 9:48
  • @mariar, I can confirm that xclip with the PRIMARY selection works when xclipboard is running. – Stéphane Chazelas Jun 26 '13 at 10:28
  • 1
    This didn't work for me either. It just blows through the loop saying "Waiting for one selection request.". – slm Jun 26 '13 at 11:40
  • @slm, with or without the "-selection clipboard"? – Stéphane Chazelas Jun 26 '13 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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