5

I have a custom function in vimscript which creates a string and then echo's it to the bottom console. At the moment I copy and paste that output using cmd-c to copy it to the mac os x clipboard. I would like to shorten this workflow by just piping the echo to pbcopy, i tried:

:echo "hello" <BAR> !pbcopy

But of course that doesn't work. I also tried capturing the output into a register and then outputting it:

:@a!pbcopy

But it just trys to execute the string stored in @a, which is no a vim command. I feel like this should be straight forward, thanks.

  • 1
    Possible duplicate of [Piping buffer to external command in Vim](http://stackoverflow.com/questions/7867356/piping-buffer-to-external-command-in-vim) – Whymarrh Apr 13 '17 at 12:36

2 Answers2

7

Assuming pbcopy takes its input from stdin you could try using system()

:call system('pbcopy', 'hello')
:call system('pbcopy', @a)

For more information see

:h system()
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
2

What version of Vim do you use? In any reasonably up-to-date version (installed via Mac Ports/Homebrew or, better, MacVim) a simple

let @*="text"

will put text on the clipboard.

See :h clipboard.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • I am using the default install of vim, which is 7.3 When I try to set @* I get an error, "Invalid register name: '*'" –  Sep 13 '12 at 21:43
  • 1
    @DubsGordon: The (compile-time) `cilpboard` feature controls whether that register is available (the MacVim author contributed the support for the OS X pasteboard to Vim 7.3). Your default installation of Vim probably does not include the feature (i.e. `-clipboard` in the output of `:version`), but as romainl indicates, many other builds do include it (`+clipboard`). – Chris Johnsen Sep 14 '12 at 01:39
  • @DubsGordon, you'll make your life considerably easier by installing a proper build. I'd recommend MacVim. – romainl Sep 14 '12 at 05:12