Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 356
Michael Härtl Avatar asked Feb 26 '15 18:02

Michael Härtl


2 Answers

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.

like image 117
Michael Härtl Avatar answered Oct 03 '22 20:10

Michael Härtl


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.

like image 34
alfred Avatar answered Oct 03 '22 22:10

alfred