Text copied from vim inside a tmux session is padded with spaces to the right
Asked Answered
M

3

15

When I run Vim from inside a tmux session and copy some text to the clipboard, each line gets padded with spaces to the right.

For example, say I have a text file like this:

^some$
^text$

Note: I've used ^ and $ to mark the beginning and end of a line respectively. They are not part of the file content.

I start tmux and open this file in Vim. I press Shift (to prevent Vim from processing the mouse click) and mark the complete text by holding the left mouse button clicked. Then I copy it to the clipboard with Shift+Ctrl+c.

The result in the clipboard is something like:

^some                                                  $
^text                                                  $

Note the extra spaces. The number of spaces depends on the terminal width.

If I start Vim without tmux or if I just cat the file content and then copy it, there are no extra spaces. So it must have to do with the combination of Vim + tmux. I've seen this on different Linux flavours, i.e. on Ubuntu and Mint. I use the default terminal (Gnome Terminal 3.6.2) there.

So how can I prevent this?

EDIT: My tmux.conf

set-option -g prefix C-a
set-option -g mouse-utf8 off

set-option -g status-keys vi
set-window-option -g mode-keys vi
set-window-option -g mode-mouse on
set-option -g terminal-overrides 'xterm*:smcup@:rmcup@'

bind-key C-a last-window
bind-key C-h select-pane -L
bind-key C-l select-pane -R

# colors:
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left '#[fg=green]#H'
set-window-option -g window-status-current-bg red

EDIT 2: I've also tried without the above .tmux.conf, using tmux' default settings - no difference.

Montanez answered 26/2, 2015 at 18:22 Comment(14)
Can't you just select the text in visual mode and do "+y? this yanks the selected text to system clipboard.Infinitude
Thanks, I didn't know this is possible. But it will not work if you have a remote session, which is my main use case.Myramyrah
Ah, I see. The spaces are probably added because when you select multiple lines, it selects across all the columns on the terminal screen, so it adds the spaces.Infinitude
Well ... yes, you just describe the problem in other words. But why does this only happen in combination with tmux and Vim? It works with cat or without tmux.Myramyrah
That is very odd. What terminal emulator are you using?Infinitude
Not sure what you mean. It's all already described in the question: I'm on Ubuntu (or Mint), using the default terminal there (Gnome Terminal), firing up tmux, then vi and that's all. No other terminal emulator involved.Myramyrah
This happens to me in konsole with and without tmux. Maybe it is something that only the gnome terminal does?Infinitude
can you show your tmuxconf ? also why shift+ctrl+C does copy to clipboard? you have vim mapping? or tmuxconfig?Klingel
@Klingel Added the .tmux.conf. The shift+ctrl+c shortcut is default on Ubuntu / Mint, as far as I know. I've always used it on the terminal, because ctrl+c is already occupied there (abort the current task).Myramyrah
If you use <shift> to prevent Vim from processing the mouse clicks, how do you think the terminal emulator should know, that you are selecting empty chars? The terminal emulator lets you select the "visible" space and so appends those spaces to your selection.Maharani
@ChristianBrabandt Then why does it work without tmux?Myramyrah
I honestly do not know. I don't think Vim has anything to do with itMaharani
@ChristianBrabandt You're probably right, as I think I've seen this in other situations, too. But this is the only combination where I now can definitely reproduce it every time.Myramyrah
I just made a quick test and can see it sometimes and sometimes not (depending on the terminal and whether I use the primary selection or the system clipboard). Strangely, adding some trailing whitespace will make it disappear (so it looks like the terminal emulator might be stripping trailing whitespace sometimes)Maharani
M
8

After more investigation I found the root of the problem. It's because tmux does not support the bce feature. From the xterm FAQ:

The term "bce" stands for "back color erase". Terminals such as modern xterm and rxvt implement back color erase, others such as dtterm do not. (Roughly half of the emulators that I know about implement bce). When an application clears the screen, a terminal that implements back color erase will retain the last-set background color. A terminal that does not implement back color erase will reset the background color to the default or initial colors. Applications that paint most of the screen in a single color are more efficient on terminals that support back color erase. Inevitably, there are tradeoffs and issues with standardization of the feature as noted in the ncurses FAQ. Unsurprisingly, ncurses supports xterm's behavior.

The original screen multiplexer had this feature. I've opened a feature request issue, but unfortunately they refused to implement it. So the hard truth is: There's no solution with tmux.

UPDATE: For those checking the feature request - Good luck! The author is very opinionated and reacts quite thin-skinned. He completely banned me from the repository, because I dared to counter one of his snotty comments. Take care.

Montanez answered 15/9, 2015 at 12:30 Comment(0)
P
3

Emm, I found a workaround, to change TERM to xterm from screen:

TERM=xterm vim

or use below in ~/.tmux.conf

set -g default-terminal "xterm"

Above I've tested in GNOME terminal's local shell and SecureCRT's remote shell. If this still not works, maybe you can check you don't have "set t_ut=" in your ~/.vimrc or other global settings, because I find this break my workaround.

but in "man tmux", it says

"The TERM environment variable must be set to “screen” for all programs running inside tmux. New windows will automatically have “TERM=screen” added to their environment, but care must be taken not to reset this in shell start-up files."

and

"Set the default terminal for new windows created in this session - the default value of the TERM environment variable. For tmux to work correctly, this must be set to ‘screen’ or a derivative of it."

I don't know the side effect of this.

Pitiful answered 15/9, 2015 at 4:17 Comment(1)
Thanks, this actually really works. But now I have issues with Vim's color themes. For example the background color now also stops at the line ending. So I think, there's a reason why the man page requires TERM to be set to screen. But thanks anyway! At least something to play around with.Myramyrah
M
1

In tmux and later v3.3 this is fixed.

In your tmux.conf set:

set -ag terminal-overrides ",screen-256color:colors=256:Tc:Ms=\\E]52;c;%p2%s\\7"
set -g set-clipboard on

Then osc52 escape codes will be used to copy text without the trailing spaces.

Also, it seems we're very close to getting this working even while using tmux via mosh.

For details, check Tmux Wiki - Clipboard

Moller answered 28/6, 2024 at 4:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.