在vim中粘贴文本后,文本失真

7
我想把一个网页或vim窗口中的一系列行粘贴到另一个vim窗口中 我使用鼠标进行复制和粘贴 原始内容如下:
    // Calculate the sum                                            //
     sum = 0;
     while (len > 1)
     {
             sum += *buf++;
             if (sum & 0x80000000)
                     sum = (sum & 0xFFFF) + (sum >> 16);
             len -= 2;
     }

     if ( len & 1 )
             // Add the padding if the packet lenght is odd          //
             sum += *((uint8_t *)buf);

在将它们粘贴到另一个 vim 中后,这些行就变成了:
     // Calculate the sum                                            //
     //          sum = 0;
     //                   while (len > 1)
     //                            {
     //                                             sum += *buf++;
     //                                                              if (sum & 0x80000000)
     //                                                                                       sum = (sum & 0xFFFF) + (sum >> 16);
     //                                                                                                        len -= 2;
     //                                                                                                                 }
     //
     //                                                                                                                          if ( len & 1 )
     //                                                                                                                                           // Add the padding if the packet lenght is odd          //
     //                                                                                                                                                            sum += *((uint8_t *)buf);
     //
     //                                                                                                                                                                     // Add the pseudo-header     

为什么会出现这种情况?如何使粘贴的内容符合预期?谢谢!


5
输入 set paste 然后再进行粘贴。 - Kent
@kent:就这样了。你应该将它作为自己的答案添加进去。 - Nathan Fellman
@NathanFellman 好的,我添加了一个答案,还加了一些文字。 :) - Kent
@NathanFellman 哎呀!我以为你是原帖发布者!!! :D :D :D - Kent
只是好奇:是否可能将“set paste”、“p”(粘贴)和最后的“set nopaste”全部映射到一个键映射中,例如control-v? - zanegray
3个回答

9

好的,我添加了一个回答。

在粘贴之前考虑使用set paste(特别是缩进代码),请注意paste选项可能会产生“副作用”,请阅读帮助文档。

在粘贴后最好将paste设置为false。如果你经常进行粘贴操作,可以方便地将一个键映射到启用/禁用粘贴选项。 :)


1
+1...我绑定了一个快捷键来切换它,因为大多数情况下这不是我想要的,但当需要时只需两个按键即可获得该行为。 - 0xC0000022L
3
使用 :set nopaste 命令。 - Nathan Fellman
1
@user138126,你可以使用set paste!来切换它。ON->OFF and OFF->ON,Vim很不错,不是吗? - Kent

2

这是由于自动缩进或智能缩进导致的。在粘贴之前,请执行以下操作:

: set noai
: set nosi

然后进行粘贴操作,应该能够正常粘贴。完成粘贴后,请执行以下步骤:

:set ai
:set si

aiautoindent的缩写。

sismartindent的缩写。


根据肯特的评论,您还可以使用:set paste来执行此操作。以下是官方帮助文件中的解释:

                        *'paste'* *'nopaste'*
'paste'         boolean (default off)
            global
            {not in Vi}
    Put Vim in Paste mode.  This is useful if you want to cut or copy
    some text from one window and paste it in Vim.  This will avoid
    unexpected effects.
    Setting this option is useful when using Vim in a terminal, where Vim
    cannot distinguish between typed text and pasted text.  In the GUI, Vim
    knows about pasting and will mostly do the right thing without 'paste'
    being set.  The same is true for a terminal where Vim handles the
    mouse clicks itself.
    This option is reset when starting the GUI.  Thus if you set it in
    your .vimrc it will work in a terminal, but not in the GUI.  Setting
    'paste' in the GUI has side effects: e.g., the Paste toolbar button
    will no longer work in Insert mode, because it uses a mapping.
    When the 'paste' option is switched on (also when it was already on):
        - mapping in Insert mode and Command-line mode is disabled
        - abbreviations are disabled
        - 'textwidth' is set to 0
        - 'wrapmargin' is set to 0
        - 'autoindent' is reset
        - 'smartindent' is reset
        - 'softtabstop' is set to 0
        - 'revins' is reset
        - 'ruler' is reset
        - 'showmatch' is reset
        - 'formatoptions' is used like it is empty
    These options keep their value, but their effect is disabled:
        - 'lisp'
        - 'indentexpr'
        - 'cindent'
    NOTE: When you start editing another file while the 'paste' option is
    on, settings from the modelines or autocommands may change the
    settings again, causing trouble when pasting text.  You might want to
    set the 'paste' option again.
    When the 'paste' option is reset the mentioned options are restored to
    the value before the moment 'paste' was switched from off to on.
    Resetting 'paste' before ever setting it does not have any effect.
    Since mapping doesn't work while 'paste' is active, you need to use
    the 'pastetoggle' option to toggle the 'paste' option with some key.

1
你可能开启了autoindent或者smartindent。如果想关闭它们,请使用:
:set noai
:set nosi

去掉 "no" 即可重新启用它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接