All posts tagged “tmux

OSX Copy/Paste in tmux

One of the big things that took me a while to get right in tmux was copy/pasting. The gripes of trying to get it to work were enough to nearly make me forget using it all together.

Here’s what you need to know.

Install reattach-to-user-namespace

brew install reattach-to-user-namespace

and then in your .tmux.conf

set-option -g default-command "reattach-to-user-namespace -l zsh"

Assign a binding to copy the current buffer

In your .tmux.conf set a binding to copy the buffer

bind-key C-c run-shell "tmux save-buffer - | reattach-to-user-namespace pbcopy"

Now it actually took me ages to work out how to use this properly. Once you have created the buffer, either through selecting it with the mouse or by using PREFIX – [ and SPACE/ENTER, press PREFIX – C-c and it will be copied to your system clipboard.

Use the option key

If you hold down the option key while in the terminal, you can create a selection just as if you’re weren’t in tmux.

Switching to tmux

Not being one to jump on bandwagons, I just had to try tmux after hearing so many people talk about it. tmux is a terminal multiplexer allowing you to run a number of terminals within a single screen as well as allowing you to detach and reattach to the same session as you please.

So what? I can just run multiple tabs and get the same effect!

Yes and no. I had been running multiple tabs, with vim in one window, a console session in another and tailing output in another, but the real revelation with tmux came when I tried out the tmuxinator gem. tmuxinator allows you to easily manage tmux sessions. With a simple yaml file you can create and start a tmux session with your editor, console and logging all setup, laid out and ready to go. This is incredibly useful with you work across multiple projects as quite frequently I would find myself in tab hell when having to switch from one project to another. Once you get past having more than four or five tabs open it become increasingly difficult to know which is which. tmux sessions mean I can keep everything related to a single “context” within one terminal session.

Getting up and running wasn’t 100% smooth sailing. Brian P. Hogan’s tmux: Productive Mouse-Free Development was invaluable. Even with less than a full days use, I’m pretty comfortable and have gotten over the initial slowdown that comes with switching to a new tool.

The only thing I miss is that I can no-longer use CMD-S for saving like in MacVim, but to be honest that’s a bad habit I need to break, as well as colour schemes aren’t quite as pretty in command line Vim compared to MacVim.

A few gotchas I encountered:

  • When using Vim and the Command-T plugin, up/down arrows wont work for selecting a file to open, you will have to use CTRL-J/K to move up and down and CTRL-C to close the pane.
  • When adding reattach-to-user-namespace to enable copy/paste to your tmux.conf, you must kill your tmux session for the change to take place, it’s not enough to just quit and restart tmux.
  • If you’re using rvm, opening a new pane/window into a directory with an .rvmrc wont properly load in the selected ruby. If you’re on bash then adding “cmd .” to your .bashrc should work (I haven’t tried it), but for me I had to add “source .rvmrc” to my .zshrc for it load in properly.