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     

为什么会这样?以及如何按预期进行粘贴?谢谢!

4

3 回答 3

9

好的,我添加一个答案。

set paste在粘贴之前考虑一下(特别是对于带有缩进的代码)。请注意,该paste选项可能会产生“副作用”。阅读帮助。

粘贴后最好将 paste 设置为 false。如果您确实粘贴了很多,则将键映射到启用/禁用粘贴选项会很方便。:)

于 2013-03-19T15:32:20.610 回答
2

auto-indent 或 smart-indent 都会对你这样做。在粘贴之前执行此操作:

: set noai
: set nosi

然后粘贴。它应该可以粘贴。完成粘贴后,请执行以下操作:

:set ai
:set si

ai是 的缩写autoindent

si是 的缩写smartindent


根据 Kent 对该问题的评论,您也可以使用: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.
于 2013-03-19T15:25:49.813 回答
1

您已打开autoindentsmartindent打开。要关闭它们,请使用

:set noai
:set nosi

删除否以重新打开它们。

于 2013-03-19T15:26:22.810 回答