2

When pasting text into Vim without tmux, it shows a normal paste:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 

However, when using Vim inside tmux, the indentation breaks and the paste shows like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 

How can this be solved?

4
  • 2
    try using :set paste before insertion maybe ; I use tmux but I never had a similar case.
    – francois P
    Mar 5, 2020 at 19:34
  • @francoisP , that command fixed the problem, thank you! I also found some other observations which I'll explain in my next edit. Please consider writing an answer to this post; I'll most probably accept it after 24 hours of waiting.
    – evaristegd
    Mar 5, 2020 at 19:54
  • @francoisP , by the way, do you think I can see your .tmux.conf file? If not, what would you recommend?
    – evaristegd
    Mar 5, 2020 at 19:56
  • 1
    @francoisP , your answer says 'past' instead of 'paste'.
    – evaristegd
    Mar 5, 2020 at 20:32

2 Answers 2

5

So using :set paste first can resolv the issue.

as you asked here is my tmuxrc but it is very short :

:~$ cat ~/.tmuxrc
set -g status-utf8 on
:~$

So I don't think it is special

1
  • 3
    Reminder that one should unset it afterwards
    – D. Ben Knoble
    Mar 6, 2020 at 13:54
9

Manually toggling :set paste/:set nopaste as suggested by francois P is cumbersome, and resetting the TERM variable as evaristegd suggests is a very bad idea as explained in the comments (which hint at the better solution below). The best solutions that I could find are explained here, and I'll briefly repeat them below.

Background

The central concept for this problem is "bracketed paste mode", which lets a program distinguish between pasted and typed input. The problem is that vim 8 only enables bracketed paste mode when started with certain values for the TERM-variable (e.g. TERM=xterm-256color). Within tmux however, vim should get either TERM=screen (default) or TERM=tmux, and in neither case does vim enable bracketed paste mode (I have no idea why not, please edit if you do).

Solution

Add the following to your ~/.vimrc, as explained in the vim help: :h xterm-bracketed-paste.

if &term =~ "screen"                                                   
    let &t_BE = "\e[?2004h"                                              
    let &t_BD = "\e[?2004l"                                              
    exec "set t_PS=\e[200~"                                              
    exec "set t_PE=\e[201~"                                              
endif

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.