6

From Terminal.app on my Mac, I SSH in to a box where I run Vim 8.0.1601. Left at that, everything works fine. But if I launch Vim from within a tmux window, go to insert mode, and then attempt to paste text from my Mac's clipboard into the terminal window, autoindent breaks the formatting.

If I place this on my Mac's clipboard...

Level 1
    Level 2
    Level 2
Level 1

...and then paste it into Vim running in tmux, I get this...

Level 1
    Level 2
        Level 2
        Level 1

Which is all wrong.

Now the standard solution to this problem is to use "paste mode" as described here:

https://stackoverflow.com/questions/2514445/turning-off-auto-indent-when-pasting-text-into-vim

However, I shouldn't have to do this because Terminal.app supports bracketed paste mode (described here: https://cirw.in/blog/bracketed-paste), and so does Vim. And I can confirm bracketed paste mode results in the correct paste behavior without my having to manually invoke paste mode—at least it works if I don't run tmux.

Without running tmux I see $TERM is set to xterm-256color, which matches Terminal.app's declaration.

From within tmux I can see $TERM is set to "screen-256color". From within tmux, if I override $TERM (I run "export TERM=xterm-256color") and then run Vim, bracketed paste mode works, and my pastes from my Mac are once again correctly indented. But this is a hack, and I don't know what unintended side effects I'm signing up for by spoofing $TERM.

Vim seem to look for "xterm-256color" in $TERM so it knows it is safe to offer bracketed paste mode. But in the case of tmux, it seems to be safe anyway. tmux just doesn't present to Vim a $TERM of "xterm-256color".

There are plugins and .vimrc configurations that address this (see https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode). But it doesn't seem like one should have to do any of this given that Vim already implements the functionality natively.

Am I missing the "correct" way to make this work?

1 Answer 1

5

I have this in my .vimrc, which works when running tmux in gnome-terminal

if &term =~ '^tmux'
  let &t_BE="\<Esc>[?2004h"
  let &t_BD="\<Esc>[?2004l"
  let &t_PS="\<Esc>[200~"
  let &t_PE="\<Esc>[201~"
endif

Terminal in tmux is set to tmux-256color; fragment from ~/.tmux.conf:

set -g default-terminal tmux-256color

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.