Charles
Charles

Reputation: 1239

Copy text to clip board

I'm currently writing a Makefile whose task is to copy the text content from a given file into the computer's actual clipboard.

One way I thought was to run vim with a special startup command (option -c 'command'). So I thought of using

vim -c '%w !pbcopy | q'

That doesn't work because !pbcopy prompts for a carriage return (I think). Anyway, at runtime Vim tells me

zsh:1: command not found: q

shell returned 127

Press ENTER or type command to continue

Sucks.

Any other way I could do that ? Either get around that carriage return problem in this double vim command, or simply find another way to copy my text from my terminal (I use Zsh).

Thanks in advance !

Upvotes: 0

Views: 1366

Answers (3)

Yann Moisan
Yann Moisan

Reputation: 8281

You can use xclip :

cat file | xclip -i -selection clipboard

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172718

You don't need Vim at all for this; what you're doing is equivalent to taking a trip around the world just to fetch the newspaper from the front porch.

To explain the Vim error: There are Vim commands that can be chained together, e.g. :version | help, but some commands can take arbitrary arguments (including the | command separator), so chaining is not directly possible. As you can see at :help :|, the :write ! command is one of those. You can work around this by wrapping the command in :execute (which allows chaining):

:execute '%w !pbcopy' | q

Upvotes: 2

verdammelt
verdammelt

Reputation: 942

To quote pbcopy(1):

pbcopy takes the standard input and places it in the specified pasteboard. If no pasteboard is specified, the general pasteboard will be used by default. The input is placed in the paste- board as plain text data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

So you just have to do the command pbcopy < file

Become familiar with the Unix command man which give you access to the manual pages for commands. When you are familiar with it then it is easy to get the answer to such questions as these for yourself.

Upvotes: 2

Related Questions