Vim style copy paste in Tmux

“Colorful lines of code on a MacBook screen” by Caspar Rubin on Unsplash

Tmux is a terminal multiplexer which allows us to create and manage multiple terminal windows. In this post we will configure tmux to have vim style copy and paste.

Tmux configuration file is located at ~/.tmux.conf. Lets add following configuration in it.

To reload updated configuration we will run the following command tmux source-file ~/.tmux.conf in terminal. This will apply the newly updated configuration.

After the configuration is applied, we can press Ctrl+a v to enter visual mode in tmux and make selection. Ctrl+a y to copy selection and Ctrl+a p to paste the selection.

The above configuration will work on most operating system, however we will need to modify it a bit to get it working in osx. There is a bug in osx which prevents tmux from sending the copied output to the clipboard. This bug was fixed in el capitan however was regressed on sierra again. A small utility called reattach-to-user-namespace fixes this issue.

Lets install it by running brew install reattach-to-user-namespace and add the following configuration in ~/tmux.conf.

The above configuration will pipe tmux yanked contents to pbcopy command in osx. pbcopy command copies given contents to the osx clipboard.

I have a central dotfiles which I use across multiple machines and operating system. At work I work on centos and my personal laptop run osx. So lets update the above configuration so it works on both.

The only change here is that if the operating system is osx, we source osx specific configuration called ~/.tmux-osx.conf.

If you are interested, my full tmux configuration can be found here.

Originally published at on November 25, 2017.

Lead Software Engineer at A Cloud Guru

Lead Software Engineer at A Cloud Guru