Cannot copy text to clipboard on macOS with tmux from terminal session - tmux

The version I am running is tmux 3.3a
I cannot copy text to my clipboard when i scroll up in a terminal session. This seems to be because the text i'm highlightning is yellow in color rather than clear. I cannot scroll up in my tmux history and copy text to my clipboard with fn + cmd+c.
If I do not scroll the tmux session, copying to my clipboard works correctly when i press fn + cmd+c.
Here is an image that shows text that I cannot copy to my clipboard with fn + cmd+c.
Here is my tmux.conf
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# To copy, left click and drag to highlight text in yellow,
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
set -g visual-bell on
# https://stackoverflow.com/questions/32374907/tmux-mouse-copy-mode-jumps-to-bottom
unbind -T copy-mode-vi MouseDragEnd1Pane

Related

Refresh buffer while in Tmux copy mode?

While in copy mode (Ctrl+[) is there a way to refresh the buffer without having to leave copy mode?
This would be helpful while scrolling through long running output.
There's a refresh-from-pane (requires tmux 3.2+) command in copy mode. The default key binding for it is r.
To check if you tmux supports refresh-from-pane:
$ tmux list-keys -T copy-mode | grep refresh-from-pane
bind-key -T copy-mode r send-keys -X refresh-from-pane
$ tmux list-keys -T copy-mode-vi | grep refresh-from-pane
bind-key -T copy-mode-vi r send-keys -X refresh-from-pane

tmux can't find session: Editor

I'm trying to write a script that sets up a series of tmux panes within one window. Each pane wil have a separate program loaded into it, mimicking an IDE.
This is the script I'm running:
#!/bin/sh
tmux new-session -s Editor -n Desktop -d
# Set up main editor window
tmux select-window -t Editor:Desktop
tmux -u attach -t Editor
# Create splits
tmux send-keys -t Editor:Desktop 'C-b %' # 0
tmux send-keys -t Editor:Desktop 'C-b "' # 1
tmux send-keys -t Editor:Desktop 'C-b "' # 2
tmux send-keys -t Editor:Desktop 'C-b "' # 3
# Load programs into panes
tmux select-pane -t 0
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'vim' Enter
tmux select-pane -t 1
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'working_set --watch .' Enter
tmux select-pane -t 2
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'clear' Enter
tmux select-pane -t 3
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'npm start' Enter
This doesn't do as expected. Instead, it loads up a window without panes. When I exit, I see the errors:
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find session: Editor
can't find pane: 3
can't find session: Editor
can't find session: Editor
You should use tmux split-window to achieve what you want. Keys sent by send-keys are interpreted as input in command line, not shortcut to operate tmux.
#!/bin/sh
# Note: the default window, pane index start from 0
# you may need to modify the following index if you changed it in .tmux.conf
# E.g.
# set -g base-index 1 # start window index at 1
# setw -g pane-base-index 1 # pane index starts at 1
tmux has-session -t development
if [ $? != 0 ]; then
tmux new-session -s Editor -n Desktop -d
# Set up main editor window
tmux select-window -t Editor:Desktop
# Create splits (must executed outside of the session)
tmux split-window -h -t Editor
tmux split-window -v -t Editor
tmux split-window -v -t Editor
tmux split-window -v -t Editor
# Load programs into panes
tmux select-pane -t 0
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'vim' Enter
tmux select-pane -t 1
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'working_set --watch .' Enter
tmux select-pane -t 2
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'clear' Enter
tmux select-pane -t 3
tmux send-keys -t Editor:Desktop 'ccb' Enter
tmux send-keys -t Editor:Desktop 'npm start' Enter
fi
tmux attach -t Editor
References
tmux 2: Productive Mouse-Free Development, a comprehensive tutorial about tmux. (maybe a little outdated)

How to set key binding to switch panes in tmux?

Here is my .tmux.conf
set-option -g prefix C-\
bind-key C-p select-pane -U
bind-key C-n select-pane -D
bind-key C-b select-pane -L
bind-key C-f select-pane -R
What I want is to bind C-\ C-b to switch to the left pane, C-\ C-f to switch to the right pane and etc.
But I got the message .tmux.conf:2: usage: set-option [-agosquw] [-t target-session|target-window] option [value] when I started tmux.
Any idea how to do it?
It has to do with your choice of prefix being C-\. The '\' character is used to indicate that the next line is a continuation of the set-option command. Add a gap after C-\ or quote C-\ as explained here: https://superuser.com/questions/417236/tmux-with-non-alphanumeric-prefix
You can use the following:
set-option -g prefix 'C-\'
bind-key C-p select-pane -U
bind-key C-n select-pane -D
bind-key C-b select-pane -L
bind-key C-f select-pane -R

How to create a layout and run commands in at tmux launch?

I am trying to write a script I can run/source so tmux set a specific layout and run commands. I have some results.
Here is what I've written so far:
selectp -t 1
splitw -v -p 15
splitw -h -p 50
selectp -t 1
send-keys 'cd ~/code/octoly' Enter
send-keys 'vim .' Enter
selectp -t 2
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails c' Enter
new-window -d -n server -c ~/code/octoly
selectw -t 2
send-keys 'fd' Enter
splitw -h -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails s' Enter
selectp -t 1
splitw -v -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'be guard' Enter
What works more or less is the creation of the second window and the panes. Though the panes does not give me what I want. They are all created in the first window, none are created in the second.
Here is what I run to start tmux:
tmux new 'tmux move-window -t 99 \; source-file ~/.tmux/session_octoly'
Furthermore, where I'm really lost is that if I run each command by hand one by one, it gives me what I want.
What am I missing here?
First of all, you're using the -d flag in new-window that does not make the new window the current window:
If -d is given, the session does not make the new window the current window.
However I would probably spin up tmux in a different way, using tmux -f flag to use an ad-hoc config file that does what you want.
Please check the commands before running it in you're environment.
# File: ~/.tmux/octoly.conf
# Load default .tmux.conf
source-file ~/.tmux.conf
# setup octoly session
new-session -s octoly -n editor -d -c ~/code/octoly
send-keys 'vim .' Enter
split-window -v -p 15 -c ~/code/octoly
send-keys 'drails c' Enter
split-window -h -p 50 -c ~/code/octoly
# Select vim pane
select-pane -t 1
# create second window
new-window -n server -c ~/code/octoly
send-keys 'fd' Enter
split-window -h -p 50 -c ~/code/octoly
send-keys 'drails s' Enter
select-pane -t 1
split-window -v -p 50 -c ~/code/octoly
send-keys 'be guard' Enter
# Optional step, reselect window 1 (the one with vim)
select-window -t editor
Then you need to launch tmux using:
tmux -f ~/.tmux/octoly.conf attach
Another alternative would be to build a bash script sending the same commands.
Note: I've used the -c flag to specify the start directory instead of running every time a cd command. Given that every command is run in the same folder you can take them out in the split-window commands.

Can I use double click to select and copy in tmux?

I am learning to use tmux, I found when I in a tmux window, double-click to select and copy function did not work any more.
Can I use double-click to select and copy just as in iterm2?
I have googled for some time, but did not find an short and clear answer to this. I have added setw -g mode-mouse on in the tmux configure file already.
I found a way to achieve that: hold the option key when double clicking.
Don't know about iterm2, but this can be made to work in tmux 3.0 or newer(tested on Linux w/ tmux 3.0, last command uses X11 xclip).
Added triple click to select and copy a line too.
# Double LMB Select & Copy (Word)
bind-key -T copy-mode-vi DoubleClick1Pane \
select-pane \; \
send-keys -X select-word-no-clear \; \
send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
bind-key -n DoubleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-word \; \
send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
# Triple LMB Select & Copy (Line)
bind-key -T copy-mode-vi TripleClick1Pane \
select-pane \; \
send-keys -X select-line \; \
send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
bind-key -n TripleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-line \; \
send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
If you don't use copy-mode-vi, replace this with copy-mode.
For older tmux versions check the edit-history.
In Alacrity holding Shift allows copying as if there's no tmux.
source
Building off of #ideasman42 's answer. This is using tmux 2.8 and pbcopy for macos mojave.
# Double LMB Select & Copy (Word)
bind-key -n DoubleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-word \; \
run-shell "sleep .5s" \; \
send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -n DoubleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-word \; \
run-shell "sleep .5s" \;
send-keys -X copy-pipe-and-cancel "pbcopy
My version selects the word, briefly highlights it, copies it to the system buffer and then cancels copy-mode.
I have figured out a copy paste mechanism that is similar of what you will expect form a terminal
I used the following settings to be able to:
select a word with a mouse double click action
select a line with a mouse tripple click action
select a partial line a mouse drag and drop action
This solution will keep the selection highlighted and copy the selection output to both clipboard buffers (primary and clipboard)
When you hit "Enter" you exit and go back to the shell
The advantage here is that you can use both middle mouse button as shift-insert combination outside of tmux to paste the content, while it is still selected.
Also when you exited back to the shell, you can use middle mouse button or hit shift-insert to paste the content
All what you would expect from a normal terminal environment
# Enable mouse control
setw -g mouse on
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter \
send -X cancel
# Drag and Drop Aelect & Copy (Selection)
bind-key -T copy-mode-vi MouseDragEnd1Pane \
send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
send-keys -X no-clear
# Double LMB Select & Copy (Word)
bind-key -T copy-mode-vi DoubleClick1Pane \
select-pane \; \
send-keys -X select-word \; \
send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
send-keys -X no-clear
bind-key -n DoubleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-word \; \
send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
send-keys -X no-clear
# Triple LMB Select & Copy (Line)
bind-key -T copy-mode-vi TripleClick1Pane \
select-pane \; \
send-keys -X select-line \; \
send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
send-keys -X no-clear
bind-key -n TripleClick1Pane \
select-pane \; \
copy-mode -M \; \
send-keys -X select-line \; \
send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
send-keys -X no-clear
# Middle click to paste from the primary buffer
unbind-key MouseDown2Pane
bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"
# Shift insert to paste from the clipboard
unbind-key S-IC
bind-key S-IC run "tmux set-buffer \"$(xclip -o -sel c)\"; tmux paste-buffer"
NOTE1 : in order for this to work across a ssh session : -X has to be provided as option to ssh
NOTE2: I'm using tmux version 2.8
On Kitty/Alacritty, we double-click on the text-block while keeping Shift pressed. And copying works fine natively as well as within tmux.
Just uncheck the "Enable mouse reporting" option in iTerm2.
But this has side effect: set -g mouse on in ~/.tmux.conf will not work.

Resources