DEV Community

Cover image for How to copy text from one pane – MacOS/Tmux/Alacritty
christine
christine

Posted on • Originally published at christine-seeman.com on

How to copy text from one pane – MacOS/Tmux/Alacritty


My problem? I only want to copy text from one tmux pane, not across two.

But I was finding that my mouse was failing me, and I knew there had to be a better way than just closing out my panes and only having one open. My journey on understanding terminals, shells, editors, and how to use them correctly has been rocky, but I have been feeling in a good place lately. The current setup that I am using is bash shell (starship shell prompt for that added sparkle), Alacritty terminal, Neovim editor, and Tmux for running multiple terminals. I had gotten this all configured, which probably took too much time. Debugging your developer setup is a considerable learning experience, but developers (meaning me) sometimes (all the time) make it more complicated than it needs to be. I probably didn’t need to throw in attempting to theme everything too, but I like my consistent look.

In my search to figure out how to copy the text on one pane, some Linux solutions were close to what I needed but not 100% the way there. This setup got me all the way there with a solution that should fit nicely in my vim workflow.

The suggested Linux setup is a good start but does not work great for MacOS



 bind-key -T copy-mode-vi v send-keys -X begin-selection
 bind-key -T copy-mode-vi y send-keys -X copy-selection
 bind-key -T copy-mode-vi r send-keys -X rectangle-toggle



Enter fullscreen mode Exit fullscreen mode

For MacOS, add the following onto your tmux configuration (for ex. at ~/.tmux.conf). This solution came from a superuser post from user jeremysprofile.



setw -g mode-keys vi
set -g set-clipboard off
bind-key -T copy-mode-vi v send-keys -X begin-selection
# bind y key in copy mode to select and copy to system clipboard
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"



Enter fullscreen mode Exit fullscreen mode

The configuration will add some vi key bindings, and then to supercharge tmux copy mode. For copying and pasting, reach for typical vim keybindings. Start with v to select the text, then y to copy/yank the text. Then the text is in your copy buffer and ready for pasting.

Let’s see our new bindings in action.

Hit your terminal, start up tmux ( tmux source ~/.tmux.conf ) and make sure to refresh your config if you already have tmux open.

Split your pane (tmux default keys C-b ")

Untitled

Load up some text on either side. I am looking at two readme files, one that I loaded up in vim and one that is just cat on the terminal.

Untitled

Now attempt to use your mouse to highlight some text. You will notice that both sides will highlight.

Untitled

Frustrating if you want the output from just the right side! Let’s use the new keybindings we added to the tmux configuration instead.

Switch over to tmux scroll mode (C-b [), use your arrow keys to move around, and when you are on the text that you want to copy, use v to make your selection.

Untitled

What’s this? Only text selected on one side? Perfect! Copy the text using y , and it is ready to put where you need it. In my case, moving to my left pane (C-b then left arrow ), and then pasted (vim p) into my other readme.

Untitled

Now we have a reusable, memorable solution for grabbing text in a single tmux pane!

Top comments (1)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

You will notice that both sides will highlight. Frustrating if you want the output from just the right side!

Yes, absolutely frustrating.

Fortunately, a simple solution for this is to enable mouse support in tmux.

Simply add this to your ~/.tmux.conf file:

set-option -g mouse on
Enter fullscreen mode Exit fullscreen mode

Enabling mouse support in tmux will also enable mouse scroll in tmux to scroll back through tmux buffer/pane history