Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intermittent copy/paste issues #151

Open
grese opened this issue Jan 10, 2021 · 11 comments
Open

Intermittent copy/paste issues #151

grese opened this issue Jan 10, 2021 · 11 comments

Comments

@grese
Copy link

grese commented Jan 10, 2021

I've been encountering an intermittent issue. I'm not sure if its a tmux issue, a tmux-yank problem, an issue with one of the other plugins, or an xclip/xsel issue. My tmux config is shown below.

# enable mouse controls
set -g mouse on

# enable tmux plugin manager
set -g @plugin 'tmux-plugins/tpm'
# some basic tmux settings that everyone loves
set -g @plugin 'tmux-plugins/tmux-sensible'
# plugin for improved copy/paste behavior
set -g @plugin 'tmux-plugins/tmux-yank'

# require pressing "y" to copy mouse selection
set -g @yank_with_mouse off

# Initialize TMUX plugin manager (keep this line at the very bottom)
run '~/.tmux/plugins/tpm/tpm'

My copy/paste workflow is as follows... I select text in tmux using the mouse, and then press "y" to copy to my system clipboard. Then, I can paste into any terminal/tmux window using "ctrl+shift+c". It works for a while, but it will break after some time.

When it breaks, here's what I've observed... After copying a large amount of data (a few hundred lines or more), the copy/paste behavior in the terminal/xterm seems to break. After that, when I copy something new, the new content is written to the system clipboard (because I can paste into a text document outside of the terminal). However, when I try to paste into the terminal/tmux, the clipboard still contains the OLD clipboard contents (most of the large amount of content). No matter how many times I re-copy the new content, its still there. Just to reiterate... if I paste outside of the terminal/tmux, the content is correct.

The problem happens pretty consistently when I copy a relatively large amount of content (more than a few hundred lines). That's normally when this happens. Pasting into other programs outside of the terminal has the correct content. Just pasting into the terminal produces the incorrect contents (strange, isn't it?).

Closing/opening terminal windows and killing/restarting tmux sessions doesn't help. The only thing I've been able to do to get it working again is to restart the computer.

Here's a quick illustration of the issue

Step 1: Copy a few hundred lines of content:

Data copied:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Data pasted into terminal (correct):

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Step 2: Copy something new.

Data Copied:

BBBBBBBBBB

Data pasted into terminal (incorrect):

BBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Data pasted outside terminal (correct):

BBBBBBBBBB
@grese
Copy link
Author

grese commented Jan 11, 2021

Quick update:
The bug still happens, but I found a temporary workaround so I don't have to restart to fix it... Clearing out the clipboard fixes the issue for a while (until I copy a large amount of content again). I added the following function to my .zshrc file, and I just call run empty-clipboard whenever this bug comes up. After emptying the clipboard, I'm able to resume copying and pasting into the terminal normally.

empty-clipboard() {
    touch /tmp/blank
    xclip -selection clipboard /tmp/blank
    rm /tmp/blank
}

As I mentioned in the original issue above, it seems to break when I copy a few hundred lines or more. So, maybe there's a bug in one of the components (tmux-yank, xclip, xsel, etc) when copying large amounts of data. I'll file a more coherent issue on the correct repo if I can nail down the root cause of the problem.

@grese
Copy link
Author

grese commented Jan 12, 2021

A question for the contributors... Does anybody know if tmux-yank, xclip, and/or xsel do any logging when they encounter an error? If so, anyone know where they are? I'm thinking that an exception might be occurring when I copy a large amount of content. If logs exist, it might help me nail down which component is breaking.

@life5ign
Copy link

life5ign commented Jul 9, 2022

I'm having this issue on Ubuntu 22.04 (PopOS) but pasting content yanked with tmux-yank behaves incorrectly in both the terminal and in external applications. Here's what happens:

Copy data

aaaaaaaaaaaaaaaaaaa

paste data with Ctrl + V in an external application or Ctrl + Shift +V in the terminal; at some point in time, this will work

aaaaaaaaaaaaaaaaaaa

copy more data

bbbbbbbbbbbbbbbbbbb

paste data

aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb

From here on out, data will keep getting appended to this buffer.

If I run either xsel -bo or xclip -selection clipboard -o, they show the same appended content. -b specifies the system's "clipboard" selection, which is the default for tmux.

@grese are you also using tmux-resurrect? I think I noticed that this cropped up around the time I started using that plugin. Also, would you consider renaming this issue to something a bit more specific? Like "tmux-yank buffer accumulates and appends content unexpectedly in different scenarios, intermittently?" I think that might help it get more attention. I had a gut feeling when reading your title that it was the same issue, but it wouldn't have shown up as well in a google search, and the contribs may not see it as meriting attention with a generic message.

I'm thinking of opening a separate issue since we have slightly different scenarios. Also you could add your shell function to override_copy_command like so (not sure if this will work) if you want to empty the clipboard every time you yank in tmux-yank

set -g @override_copy_command 'empty-clipboard && <insert correct xsel command>'
# I don't know what the correct xsel command is

Documented here https://tmux-plugins.github.io/tmux-yank/

@alextrastero
Copy link

did you manage to solve this @life5ign , I don't use tmux-yank but I'm having this issue with neovim+tmux+popos, this doesn't happen with same dotfiles in ubuntu

@ch3n9w
Copy link

ch3n9w commented Dec 6, 2023

I have the same problem when using tmux with kitty under hyprland, here is my configuration

set -ga terminal-overrides ",xterm*:Tc"
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'
set-environment -g COLORTERM "truecolor"
set -s set-clipboard on
set -g mouse on

# image preview in tmux
set -g allow-passthrough on
set -ga update-environment TERM
set -ga update-environment TERM_PROGRAM

# unbind C-b
# set -g prefix C-x
# bind C-x send-prefix
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

# Pane
bind -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'
bind -T copy-mode-vi 'C-h' select-pane -L
bind -T copy-mode-vi 'C-j' select-pane -D
bind -T copy-mode-vi 'C-k' select-pane -U
bind -T copy-mode-vi 'C-l' select-pane -R
bind -n C-Home swap-pane -U
bind -n C-End swap-pane -D
bind -n C-S-M-h resize-pane -L 5
bind -n C-S-M-l resize-pane -R 5
bind -n C-S-M-k resize-pane -U 5
bind -n C-S-M-j resize-pane -D 5
bind -n 'C-q' if-shell "$is_vim" 'send-keys C-q' 'kill-pane'
bind -n C-x split-window -h -c "#{pane_current_path}"
bind -n C-S-x split-window -v -c "#{pane_current_path}"
bind -n C-f resize-pane -Z
# press q to exit copy mode
# to search string, press pageup to enter copy mode, and press / to search
bind -n PageUp if-shell "$is_vim" 'send-keys PageUp' 'copy-mode'
bind -n PageDown if-shell "$is_vim" 'send-keys PageDown' 'send -R'
bind-key s command-prompt -p "send pane to:"  "join-pane -t ':%%'"

# Window
bind -n C-t     new-window -c "#{pane_current_path}"
bind -n C-S-t     command-prompt -I "#W" "rename-window '%%'"
bind -n C-PageUp  previous-window
bind -n C-PageDown next-window
bind -n C-S-PageUp swap-window -t -1\; select-window -t -1
bind -n C-S-PageDown swap-window -t +1\; select-window -t +1
bind -n C-1 selectw -t 1
bind -n C-2 selectw -t 2
bind -n C-3 selectw -t 3
bind -n C-4 selectw -t 4
bind -n C-5 selectw -t 5
bind -n C-6 selectw -t 6
bind -n C-7 selectw -t 7
bind -n C-8 selectw -t 8
bind -n C-9 selectw -t 9

# Session
bind -n C-s choose-session -Z
bind -n C-S-s command-prompt -I "#W" "rename-session '%%'"
bind -n C-n new-session -c "#{pane_current_path}"

# Start windows and panes at 1, not 0
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# if selection mouse out of pane, use <Enter> to copy
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @resurrect-strategy-vi 'session'
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'

source-file ~/.config/tmux/tokyonight-storm.conf

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'

@life5ign
Copy link

life5ign commented Dec 6, 2023

did you manage to solve this @life5ign , I don't use tmux-yank but I'm having this issue with neovim+tmux+popos, this doesn't happen with same dotfiles in ubuntu

I'm still having the issue, unfortunately. ooc, are you using kitty as your terminal emulator?

@ch3n9w does the problem go away when you stop using kitty? I also am using kitty.

@ch3n9w
Copy link

ch3n9w commented Dec 6, 2023

@life5ign yes! I found the problem disappears when I switch to another terminal emulators like alacritty and foot.

BTW, I noticed that the clipboard problem happened when I copy text from neovim using nvim-osc52 plugin, however, if I focus to another window and then focus back to kitty, the problem disappeared! Not sure if it has something to do with my desktop environment (hyprland).

@life5ign
Copy link

life5ign commented Dec 6, 2023

@ch3n9w ok, it goes away for me too when I use e.g. gnome terminal. So that's something... above my ability/time to debug the interaction between tmux yank and kitty right now, but at least there's a workaround. My DE is simply Gnome, so the base issue I think is with the plugin and the emulator, not the DE, most likely.

@life5ign
Copy link

life5ign commented Dec 6, 2023

I think we should open a separate issue e.g. "tmux-yank accumulates and appends copied content to the system clipboard when using the kitty terminal emulator in various desktop environments." The title of this issue is too generic imho.

@ch3n9w
Copy link

ch3n9w commented Dec 7, 2023

@life5ign I think the issue should be under kitty topic. kovidgoyal/kitty#6890

@naddeoa
Copy link

naddeoa commented Sep 26, 2024

I've managed, at least for the time being, to fix this with the following suite of config options

# I always had these
set -g set-clipboard off # fix race condition with terminal and tmux
bind -T copy-mode-vi y send-keys -X copy-pipe "xclip -i -selection clipboard > /dev/null 2>&1"

# I just added these from the tmux-yank docs
set -g @custom_copy_command "xclip -i -selection clipboard > /dev/null 2>&1"
set -g @override_copy_command "xclip -i -selection clipboard > /dev/null 2>&1"

It also started off working for me but eventually ended up not working. I guess this might have something to do with tmux-yank somehow conflicting with whatever happens under the hood when I use vi mode to yank? There's no obvious logs anywhere so its hard to tell. I didn't have to restart tmux after adding those new lines, just sourced the config again. I probably don't even need the vi keybinds given the current behavior 🤷

Yeah, I guess a million years ago I needed those vi keybinds but I clearly don't anymore, so just adding the overrides for tmux-yank to use xclip seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants