85

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.

3

7 Answers 7

62

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

2
  • Darn. I don't use a Mac. Will keep for future reference though. ;) Feb 25, 2010 at 21:53
  • it's working in Mac too ;)
    – HMagdy
    Jul 26, 2018 at 10:00
52

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

1
  • 24
    Example usage: xclip -sel clip < ~/.ssh/id_rsa.pub
    – wim
    Mar 12, 2013 at 4:48
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

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

3

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 

(note this is the pipe operator, not file redirect) 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)

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
    +1. I appreciate this effort though I think xclip is a better solution.
    – 0xc0de
    Sep 29, 2013 at 18:03
0

For Windows context menu (right-click), this adds a new entry to copy the selected file to the clipboard.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\CopyContents]
"Icon"="DxpTaskSync.dll,-52"
"MUIVerb"="Copy Contents to Clipboard"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\CopyContents\command]
@="cmd /c clip <\"%1\""

Modified from this source

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .