1

I would like to make a style command/macro to paste my current unnamed yank register (or a specific one if I must) to an external command.

My current use case is to send the lines to the clipboard. Although in the future I would like to use the same technique to send text to other programs.

For the clipboard, as a Mac user, I would normally just add the * to my clipboard, but I'm also a Tmux user so this does not work. I must instead use the reattach-to-user-namespace command. What I would like to happen is have the yank register piped out to reattach-to-user-namespace -l bash -c pbcopy.

Jason
  • 3,736
  • 5
  • 33
  • 40
  • 2
    That command is supposed to be added to your `~/.tmuxconf`: no piping needed. I'm fairly certain that `"*y` works in tmux on my Mac at work. – romainl Oct 22 '12 at 20:34
  • Possible similar post: http://stackoverflow.com/questions/12414745/send-echo-or-register-contents-to-pbcopy-mac-clipboard-on-mac-os-x/12415965 – Peter Rincker Oct 22 '12 at 21:03
  • Not a duplicate post. I'm trying to output a yank register to an external command. It just so happens this time I want `pbcopy`. In the future I will use the same commands to pipe to `ruby` as well. – Jason Oct 22 '12 at 21:26

2 Answers2

0

As stated by romainl, by putting the namespace command in your tmux.conf you shouldn't need to do thi, but if you did want to pipe the contents of a register to an external command you could use getreg() to retrieve its contents and exe to execute the command. For instance...

:exe "!pipereg ".getreg('"')." | pbcopy"
Conner
  • 30,144
  • 8
  • 52
  • 73
0

If you launch your shells via the reattach-to-user-namespace wrapper (as I describe in the documentation for the wrapper), all their children will be attached to the user bootstrap namespace, so you will not have to use it in your individual calls to (e.g.) pbcopy and pbpaste.

Alternatively, you could run just you instances of Vim via the wrapper to give it (and its children) access to the bootstrap namespace. That way, you should just be able to use the * register (if you have Vim 7.3 compiled with the +clipboard feature, i.e. practically any build of Vim besides the one that comes with OS X).

reattach-to-user-namespace vim …
# use "* inside Vim to access the OS X clipboard.

If you really want to avoid running the wrapper except for the ultimate processes that need it (e.g. pbcopy), then you can use Vim’s system() function. This is also how you would generically send some internal-to-Vim data to any (non-interactive) external command:

:call system('reattach-to-user-namespace pbcopy', getreg(''))

You might want to package this as a new command (so that you can more easily use it with different registers):

command -bar -register Pbcopy call system('reattach-to-user-namespace pbcopy', getreg(<q-reg>))

This new command can be used like this (the argument is the register name):

:Pbcopy "
:Pbcopy a

You could go one step further and make a mapping that calls the command:

nnoremap <F3> :<C-u>execute 'Pbcopy ' . v:register<CR>

You would use it by typing F3 or "aF3 (i.e. with a register prefix, like any other register-using, normal-mode command).

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186