Tmux – System Clipboard Copy & Mouse Mode
If you have ever wanted to copy an error to debug and search on stackoverflow or copy a piece of text on terminal from a tmux session and failed, this post will guide you through the process of setting up a the Tmux Plugin Manager and installing Tmux-Yank to copy directly on the Linux System clipboard. I also demonstrate how to use mouse mode to scroll and copy using the mouse in Tmux.
Note: This post was written in context of Tmux 3.2a version.
Installing Tmux Plugin Manager
Assuming you already have Tmux installed, you’ll first need to clone the Tmux Plugin Manager.
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Add these lines at the bottom of the .tmux.conf file.
# List of plugins set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-sensible' # Other examples: # set -g @plugin 'github_username/plugin_name' # set -g @plugin 'github_username/plugin_name#branch' # set -g @plugin 'git@github.com:user/plugin' # set -g @plugin 'git@bitbucket.com:user/plugin' # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) run '~/.tmux/plugins/tpm/tpm'
Installing xsel
We need XSelection to copy highlighted text to clipboard. Install it with the following command:
sudo apt-get install xsel
Adding Tmux Yank Plugin
Let’s add the Tmux-yank plugin that enabled us to copy to the system clipboard. Add the line below in the .tmux.conf file.
set -g @plugin 'tmux-plugins/tmux-yank' #Set Yank selection as primary set -g @yank_selection 'primary' # or 'secondary' or 'clipboard'
If you already have a tmux session running, you need to source the configuration file for the effect to take place.
# type this in terminal if tmux is already running tmux source ~/.tmux.conf
Finally, start a new tmux session if you haven’t already and use the Prefix key + I (capital i) to install the plugin.
prefix + I
Press Enter to continue. That’s it!
Scroll and Copy with Mouse in Tmux
If you would like to select text and panes with the mouse, scroll down using the mouse in Tmux, here’s how you can do it.
Add the below configuration in the .tmux.conf file.
#Enable mouse scrolling using mouse mode set -g mouse on
We will also need to add the yank selection for mouse like we did while adding the yank plugin.
set -g @yank_selection_mouse 'clipboard' # or 'primary' or 'secondary'
Finally, this is how my .tmux.conf file looks like with my preferences.
- Prefix key set to “Ctrl + a”
- Window numbering at 1.
- Set Tmux logging location.
- Enabled Tmux mouse scrolling using mouse mode.
- Added Tmux Plugin Manager Snippet.
- Added the Tmux Yank plugin.
# remap prefix from 'C-b' to 'C-a' unbind C-b set-option -g prefix C-a bind-key C-a send-prefix # Start window numbering at 1 set -g base-index 1 #history-limit set -g history-limit 20000 # pane movement bind-key j command-prompt -p "join pane from:" "join-pane -s '%%'" bind-key s command-prompt -p "send pane to:" "join-pane -t '%%'" #Vi search mode set-window-option -g mode-keys vi #Tmux logging in /opt/tmux-log(Prefix alt+shift+p) run-shell /opt/tmux-logging/logging.tmux #Enable mouse scrolling using mouse mode set -g mouse on ########################## ## Tmux Plugin Manager # List of plugins set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-sensible' # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) run '~/.tmux/plugins/tpm/tpm' set -g @plugin 'tmux-plugins/tmux-yank' set -g @yank_selection 'primary' # or 'secondary' or 'clipboard' set -g @yank_selection_mouse 'clipboard' # or 'primary' or 'secondary' #################################