2

My .tmux.conf file was initially empty, but then I edited by basically copying the Linux part of this answer from UnixSE. The problem below occurred both before and after I edited that conf file.
I tried to copy text from a website. When I'm using Vim in bash without tmux, I can paste the text to Vim without problems and it looks 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. 

However, when I'm inside tmux, the indentation 'breaks' and I get something 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. 

That has never happened when I paste the text on Vim inside bash (without tmux). And, in case it matters, the way I usually copy and paste is by selecting the text on my web browser, then pressing Ctrl+C , then going to Vim, enter Insert Mode, and finally press Ctrl+Shift+V .

How can I make Vim inside tmux paste my text the way it is in the block in the top?
What do I need to fix?

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

3 Answers 3

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
8

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
-2

Edit: As pointed out by others, the original answer is wrong, and I plan to delete it later, if the admins don't oppose to it. For a good answer, please check DiningPhilosopher's one.
If you want to see the original answer, you can check the edit history.

5
  • 2
    For anyone reading this answer, setting 'default-terminal' to anything other than "screen", "tmux" or a derivative is wrong; it will lead to other issues. Before setting any option, read the only source of reliable information about tmux, which is not this site, not stackoverflow, nor some blog post, but the man page.
    – user938271
    Mar 6, 2020 at 7:06
  • @user938271 , thank you for bringing this to my attention. I will review your sources.
    – evaristegd
    Mar 6, 2020 at 14:48
  • 1
    1. The main difference is that it assigns a correct value; i.e. screen-256color is ok, xterm or xterm-256color is not. 2. By writing xterm or xterm-256color, you're making Tmux initialize $TERM with a value which tells all the applications (not just Vim) that they're talking to the xterm terminal, which is a lie; they are not talking to xterm directly (in fact the outer terminal may not even be xterm); they are talking to the Tmux terminal. They will now query a wrong entry in the terminfo database when they'll need to use a terminal capability.
    – user938271
    May 23, 2020 at 8:52
  • 1
    3. Your issue is probably due to Vim which doesn't enable the bracketed paste mode. If that's the case, then the best solution is not to meddle with Tmux but with Vim, and to configure terminal options as explained at :h xterm-bracketed-paste. 4. The reason why your line "fixes" the issue is probably because when Vim thinks it's talking to xterm, it automatically configures the terminal options for you. But a good fix should not introduce regressions, which set -g default-terminal "xterm" does.
    – user938271
    May 23, 2020 at 8:52

Your Answer

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

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