Can I use double click to select and copy in tmux? - 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.

Related

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

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

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

Can't bind \ key (unknown key: split-window error)

I'm usint tmux 3.3a on a mac and I'm trying to bind the \ in my tmux.conf file
bind-key \ split-window -h -c '#{pane_current_path}'
The problem is that \ seems to escape the next character and I get the following error:
/Users/myname/.tmux.conf:18: unknown key: split-window
I tried escaping the \ character
bind-key \\ split-window -h -c '#{pane_current_path}'
I also tried wrapping it into single quotes
bind-key '\' split-window -h -c '#{pane_current_path}'
But I still get the same error message
What should I do to solve this?

Inconsistent window behavior on tmux startup

I'm using iTerm2's hotkey window to open a default set of tmux panes whenever I first launch the window. I have a file named .hotkeyconfig I'm using to source the commands necessary to size the window.
tmux new -s hotkey \; \
select-pane -t 0 \; \
split-window -h \; \
resize-pane -t 0 -L 34 \; \
split-window -h \; \
resize-pane -L 15 \; \
split-window -h \; \
resize-pane -R 19 \; \
select-pane -t 1 \; \
split-window -v \; \
resize-pane -U 2 \; \
resize-pane -R 10 \; \
select-pane -t 0 \; \
I call this when the window starts with:
source ~/.hotkeyconfig
When there's another attached tmux session using this command produces the top result, however if the hotkey window is the only attached tmux session, it produces the output on the bottom.
output
What is causing this inconsistent behavior and how can I fix it?

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.

Resources