How can I load a file's contents into the clipboard?

83

27

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.

Jonathon Watney

Posted 2010-02-25T21:35:38.320

Reputation: 1 221

1

Aha, I knew it was asked before, for Windows: "How to pipe text from command line to the clipboard" at http://superuser.com/questions/97762/how-to-pipe-text-from-command-line-to-the-clipboard Maybe leave this open for Linux then?

– Arjan – 2010-02-25T21:56:45.183

1Suburb. Looks like I can use clip.exe for this. Would still like a non-command line version though. Maybe via Windows Explorer context menu? – Jonathon Watney – 2010-02-25T22:08:27.277

Answers

63

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

Arjan

Posted 2010-02-25T21:35:38.320

Reputation: 29 084

Darn. I don't use a Mac. Will keep for future reference though. ;) – Jonathon Watney – 2010-02-25T21:53:42.320

it's working in Mac too ;) – HMagdy – 2018-07-26T10:00:31.650

51

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

Justin Smith

Posted 2010-02-25T21:35:38.320

Reputation: 3 746

23Example usage: xclip -sel clip < ~/.ssh/id_rsa.pub – wim – 2013-03-12T04:48:11.790

35

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

pmac72

Posted 2010-02-25T21:35:38.320

Reputation: 501

13

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

Sam Stoelinga

Posted 2010-02-25T21:35:38.320

Reputation: 241

2

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)

Hichem Al Abbessi

Posted 2010-02-25T21:35:38.320

Reputation: 21

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.

JanSmrz

Posted 2010-02-25T21:35:38.320

Reputation: 11

1+1. I appreciate this effort though I think xclip is a better solution. – 0xc0de – 2013-09-29T18:03:01.077