79

I have a files where I need to copy their contents to another file or into an application and rather than open it up, select all the text then copy and paste I'd like to know if I can effectively cat the file contents into the clipboard. Is this possible?

A Windows program would be fine but something that works on Linux would be useful too. I don't use a Mac but it might be useful to others. Bonus points if this can be done on the command line.

62

Since you didn't ask about Macs: cat file | pbcopy (and likewise pbpaste) for those.

  • Darn. I don't use a Mac. Will keep for future reference though. ;) – Jonathon Watney Feb 25 '10 at 21:53
  • it's working in Mac too ;) – HMagdy Jul 26 '18 at 10:00
48

xclip (probably available in your Linux system's repos) will work for any X11 system, including most Linux versions, or even X being run under windows or Mac OSX.

Example usage: xclip -sel clip < ~/.ssh/id_rsa.pub

  • 23
    Example usage: xclip -sel clip < ~/.ssh/id_rsa.pub – wim Mar 12 '13 at 4:48
33

In Linux, you can use xsel to mimic pbcopy and pbpaste:

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

Source: Where's Walden? » pbcopy and pbpaste for Linux

14

On Linux and possible other systems which support xclip:

xclip -i -selection c file_to_copy_to_clipboard.txt

I saw @JustinSmith also mentioned xclip but was missing an example, so had to look it up myself.

Another useful one: paste your clipboard to a file.

xclip -o -selection c > file_to_paste_to.txt

Source

1

Use this program f2clip. Run it from the command line. It copies file contents into clipboard. I use it for copying text files into web browser for further processing. Download it from http://smrz.xf.cz/f2clip.exe or write your own from this source (it's ugly):

 program f2clip;

 {$APPTYPE CONSOLE}

 uses
SysUtils,
clipbrd;

var i,r:integer;
    s:string;
 f:file;
 buf:array[0..1024*1024-1] of byte;

 data:string;
 d:pointer;
 begin
 try

 { TODO -oUser -cConsole Main : Insert code here }
    if (paramcount=0) then begin
    writeln('parameters: f2clip filename.txt');
end else begin
    write('parameter count: ');
  writeln(paramcount);  
    for i:=1 to paramcount do begin
    s:=paramstr(i);
    writeln('file: ',s);

    assignfile(f,s);
    reset(f,1);
    BlockRead(f,buf,1024*1024,r);
    writeln('size: ',r);
    buf[r]:=0;

    d:=@(buf[0]);
    data:=PAnsiChar(d);
Clipboard.AsText := data;
    close(f);
  end;

  end;

 except
   on E:Exception do
     Writeln(E.Classname, ': ', E.Message);
 end;
end.

Sorry.

  • 1
    +1. I appreciate this effort though I think xclip is a better solution. – 0xc0de Sep 29 '13 at 18:03
0

use the command "type" as an equivalent to "cat" in windows to pipe the files content in text format into stdout (standard output) for that is the Terminal/prompt emulator you're using (CMD in windows). So you can combine the commands into something like this :

type myFile.txt > clip 

now the content of myFile.txt is transferred to the clipBoard buffer (I think it's just a buffer since it's not linux ). It's also a global value , so one value is held at a time OS wide. So that's for the "copy" feature , now for the "paste" :

  • Either you want to append to an existing file the values of the CLIP like usual stuff:

    type clip >> target.txt (or your target file - will add data without deleting the existing one inside that target file)

  • Or , you want to add/create a new file with the values of the CLIP like :

    type clip > target.txt (or your target file - will add data or OVERWRITE means deleting the existing one inside that target file)

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.