97 lines
3.7 KiB
Markdown
97 lines
3.7 KiB
Markdown
+++
|
|
date = "2017-03-22"
|
|
title = "Tmux and Vim: Copy and Paste on macOS Sierra"
|
|
tags = ["vim", "tmux", "terminal", "macos"]
|
|
description = "As a developer, more than anything, you copy and paste things. Making copy and paste work in properly with Vim and Tmux is, unfortunately, not trivial."
|
|
+++
|
|
|
|
As a developer, more than anything, you copy and paste things. Making copy and paste work in properly with Vim and Tmux is, unfortunately, not trivial.
|
|
|
|
There's a lot of information to be found on the internet about setting up copy and paste with Tmux. There's also
|
|
lots of information on how to setup Vim. But then you run Vim 8. On macOS Sierra. And things break down
|
|
quickly.
|
|
|
|
This short guide helps you setup Tmux and Vim on macOS Sierra for proper copy pasting glory!
|
|
|
|
## Dependencies
|
|
|
|
First, you'll have to install _one_ dependency. I also recommend you install the latest and greatest
|
|
Vim instead of using the (old) version bundled with macOS, and of course, tmux.
|
|
|
|
``` shell
|
|
brew install reattach-to-user-namespace
|
|
brew install vim tmux
|
|
```
|
|
|
|
## Configure Tmux
|
|
|
|
Tmux is a weird beast. Copy and pasting can be done in different ways, but I prefer the Vim style navigation.
|
|
|
|
``` text
|
|
# Configure your default shell, Zsh in my case.
|
|
set -g default-shell $SHELL
|
|
|
|
# Override the default command to use `reattach-to-user-namespace` for everything.
|
|
set -g default-command "reattach-to-user-namespace -l ${SHELL}"
|
|
|
|
# Remap prefix to ctrl-a (or caps-a for my mac)
|
|
set -g prefix C-a
|
|
|
|
# Vim style navigation in copy mode
|
|
setw -g mode-keys vi
|
|
|
|
# Setup 'v' to begin selection, just like Vim
|
|
bind-key -t vi-copy v begin-selection
|
|
|
|
# Setup 'y' to yank (copy), just like Vim
|
|
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
|
|
|
|
# Update default binding of `Enter` to also use copy-pipe
|
|
unbind -t vi-copy Enter
|
|
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
|
|
|
|
# Bind ']' to use pbbaste
|
|
bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"
|
|
```
|
|
|
|
This is how copy pasting will working after you've made the proper changes to your `.tmux.conf`. Note that
|
|
I have remapped <kbd>ctrl</kbd> + <kbd>b</kbd> to <kbd>ctrl</kbd> + <kbd>a</kbd>, because that works pretty
|
|
fast when you've already remapped Caps-Lock to Control.
|
|
|
|
* <kbd>ctrl</kbd> + <kbd>a</kbd>, <kbd>[</kbd> to enter copy mode. This allows you to use normal Vim
|
|
motion keys to move around.
|
|
* <kbd>v</kbd> to start selection, again use vim motion keys to move around.
|
|
+ <kbd>y</kbd> to yank the selection. <kbd>enter</kbd> also works.
|
|
|
|
Pasting works just as before:
|
|
|
|
* <kbd>ctrl</kbd> + <kbd>a</kbd>, <kbd>]</kbd>
|
|
|
|
Congratulation, you can now copy and paste with Tmux.
|
|
|
|
## Configure Vim
|
|
|
|
The changes needed for Vim are minimal. All you need to do is _unset_ the name of the default clipboard,
|
|
so it will pass through to Tmux. In `~/.vimrc`:
|
|
|
|
``` vim
|
|
" Clipboard
|
|
set clipboard=unnamed
|
|
```
|
|
|
|
## Bonus features
|
|
|
|
So, copy and paste is now working from Vim, Tmux and Vim-in-Tmux. Pasting is also working as expected.
|
|
|
|
As a bonus, when you have two tmux panes or windows with different instances of Vim running, you can now
|
|
easily copy and paste between them by using normal Vim commands!
|
|
|
|
## Conclusion
|
|
|
|
Copy and paste with Vim and Tmux does not work out of the box, but once setup works like a charm.
|
|
|
|
Here are links to my [`~/.vimrc`](https://git.devroom.io/ariejan/dotfiles/blob/e54317082b2a72363dad9aa6bb38310bfe8a6cc8/vim/.vimrc) and [`~/.tmux.conf`](https://git.devroom.io/ariejan/dotfiles/blob/e54317082b2a72363dad9aa6bb38310bfe8a6cc8/tmux/.tmux.conf) for your perusal.
|
|
|
|
I highly recommend [tmux 2: Productive Mouse-Free Development](http://amzn.to/2n8I3Ax), but feel free to check all
|
|
my [book recommendations](/recommendations).
|