Tmux, vim, and the OSX clipboard

Question:

Thanks to Art of Vim, Vimcasts, and Thoughtbot’s Tmux evangelism I have become a pretty devoted convert to using Vim and tmux. However, I’m running into some problems with copying and pasting that are bothering me.

When I’m copying command output from tmux, the process is easy enough with the reattach-to-user-namespace command. Pasting from the system clipboard into vim is also straightforward (I like to use vim-unimpaired’s paste-mode shortcuts). The scenario that seems like it doesn’t have a good Googleable solution is copying text from within Vim while in a Tmux session.

I have tried using or disabling set clipboard=unnamed, yanking to the quoteplus or quotestar register, but everything I do works as expected without tmux but fails with tmux running. My tmux configuration (GitHub - geoffharcourt/dotfiles-local: Personal configuration) is mostly based on Thoughtbot’s dotfiles, so I feel like I must be missing something. Using copy-mode in tmux to copy text from vim results in line numbers and messiness. Is there a straightforward way to get text out of vim into the system clipboard while running tmux?

I am able to copy from vim inside tmux to the system clipboard without issue. In my .vimrc I have:

set clipboard=unnamed " use OS clipboard

In tmux.conf I have:

# Integrate with OS X pasteboard.
set -g default-command "reattach-to-user-namespace -l zsh"

That’s all it took for me. Anything I ‘yank’ in vim is put on my OS X pasteboard. You can see my dotfiles if you’re curious.

Hi @derekprior, thanks for the answer, but unfortunately, it didn’t fix the problem, I have those two lines in my .vimrc.local and .tmux.conf.local, and it still doesn’t seem to work.

For now I’m toggling the line numbers/gutter off and just using tmux’s copy-paste when I need to take something out of Vim.

I tried swapping my dotfiles out for yours, and still had the same problem. For what it’s worth, my Vim (7.4) has the +clipboard flag.

But for the last 3.5 yrs I was programming on Xcode and Eclipse. And now using Sublime text for the rails development. I still dont understand the use of vim. Vim has become more of a status symbol I feel. Just cuz someone does not use should not mean he is a newbie. I have heard thoughtboters say stuff like that on their dotfiles github. :smile:

Thanks, @geoffharcourt… I’m having the identical problem. Hopefully, we’ll get a solution soon.

@charlieanna… without getting into a holy war, I’ve been using vim for years (even before I heard Thoughtbot’ers rave about it) and it’s certainly not a ‘status symbol’ for me. Some of what keeps me in vim includes:

  1. Vim keeps your hands on the keyboard… I don’t need the mouse for coding and I hate having my thought train interrupted because of having to use the mouse.
  2. Vim is (some would say obsessively) focused on making what you do have to type as short and memorable as possible… fewer keystrokes = fewer opportunities for error and greater productivity. It may take a bit to learn, but it works and works well once you’ve taken it on.
  3. Vim’s internal scripting language, while a bit unconventional, is extremely powerful so that I can create commands and shortcuts to do pretty much what I want.
  4. There’s an incredibly active community… so lots of documentation, help, support.
  5. There is a huge amount of add-on software out there to make vim even easier to use. tpope and othes have done some awesome stuff with those internal scripts.
  6. Vim is free and open source
  7. Vim is unde active, continual development so it just gets better and better.

I’m sure others can offer more specifics, but these are some of the reasons why it works for me.

Thanks @JESii. I wanted to hear this from a Non thoughtboter. Even I like Vim but sometimes it makes me frustrated and that post of mine was written under that :slight_smile:

:=) Believe me, @charlieanna… I know ‘frustrated’ when it comes to vim! I know it can be done, but I can’t figure it out or just can’t remember. I’m starting to learn the vim scripting language in a small way now, as I know that’s the way to handle these situations. But the more I use it, the less I feel the frustration.

This copy-paste issue is a huge hassle right now, but the move to Vim enabled me to break through a speed barrier in my coding. I considered myself to be super-fast on Sublime Text, and had honed deep muscle memory of lots of shortcuts in the editor. Transitioning to Vim made it a long summer, but I’m very happy with the result.

@JESii, are you using iTerm? I’m having the problems in OSX terminal as well, but I’m just trying to find anything I can use to start narrowing down the cause of the problem.

For those of you sitting on the edge of your seat over my saga, I’ve also tried the following:

  1. Disabling Alfred and rebooting (in case Alfred’s clipboard feature was somehow getting in the way)
  2. Going with completely stock Thoughtbot dotfiles with the exception of the set clipboard=unnamed option in vimrc and the reattach-to-namespace option in tmux.conf.
  3. Trying OSX terminal instead of iTerm
  4. Reinstalling Vim 7.4.

Still no luck. For now I’m just toggling the gutter to be hidden and using tmux copy-mode to get things out of Vim when the need arises.

Hey @geoffharcourt, to throw one more option into the ring, I use custom mappings to interact with the system clipboard via Vim within a Tmux section.

You can see the code in my dotfiles, here: https://github.com/christoomey/dotfiles/blob/master/vim/rcfiles/copy-paste. This relies on the reattach code in my ~/.tmux.conf as well: set-option -g default-command "reattach-to-user-namespace -l zsh"

The crux of it is making use of Vim’s system command to explicit pipe things to and from pbcopy and pbpaste. In the end I actually prefer the fact that I have to explicitly pipe things into the system clipboard since I do a ton of yanking and deleting in Vim, and I would not want that polluting my clipboard history in Vim (I also use Alfred to manage clipboard history).

Hope that helps!

@christoomey I think I’m starting to narrow down the cause. Even with the set-option -g default-command "reattach-to-user-namespace -l zsh" line in my tmux.conf, I still have to add this to use pbcopy: echo "test" | reattach-to-user-namespace pbcopy.

Copying text from copy-mode in tmux works fine regardless of whather I use this setting. I think I can adapt your script and add reattach-to-user-namespace before the uses of pbcopy and pbpaste and that will fix the issue.

I’m frustrated that there’s something buried in my laptop setup (brand new laptop!) that’s causing this problem, but at least there appears to be a solution. @christoomey, thanks very much for pointing me to those rc files!

Your script worked without any changes to my config. THANK YOU.

I’ve been having this same problem copying text out of Vim in tmux and into the OS X clipboard. I’ve got things set up to where I can copy text while in tmux copy mode (as per TB’s blog post), but the problem of course is that when copying code from Vim, all the line numbers are copied as well.

I’ve tried the code suggested initially by @derekprior, but it doesn’t work for me. In fact, as long as the line set clipboard=unnamed is in my vimrc, I can’t paste in Vim/tmux with p. I get the error E353: Nothing in register *. However, if I open Vim outside of tmux, then p works normally.

I also tried to implement the fix suggested by @christoomey, which @geoffharcourt said worked for him, but it seems that something must have changed since this thread was started. The system-copy plugin that Christ referenced doesn’t appear anymore on GitHub that I can tell.

I’m sure I must be missing something simple, but so far I’ve not been able to find out what it is. I’ve listed my system specifics below in case it helps. Thank you in advance for any assistance!

  • iTerm2

  • Vim 7.4.253 (installed via Homebrew)

  • Tmux 1.9a (also Homebrew)

  • vimrc

  • tmux.conf

Well this forum never ceases to amaze. No sooner do I post a question than the problem solves itself. :smile: Turns out that even though I had saved and reloaded both the vimrc and tmux.conf files after adding the code from @derekprior, I needed to kill my tmux session and come back in. I was running my custom environment built with tmuxinator. Once I killed that and reentered, I can copy text from Vim into the system clipboard just fine. Sorry for the false alarm, and thanks to everyone who contributed to this thread.

1 Like