Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

texts distorted after pasting on vim

Tags:

vim

I want to paste a series of lines from webpage or a vim window to another vim window I used mouse for copying and pasting the original is:

    // 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);

after pasting them on another vim , these lines become:

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

why this happens? and how to make the pasting as expected? thanks!

like image 965
user138126 Avatar asked Mar 19 '13 15:03

user138126


2 Answers

ok, I add one answer.

consider set paste before you do paste (specially for codes with indents). note, that paste option could have "side-effects". read help for that.

Better set paste back to false after your pasting. it would be convenient to map a key to en/disable paste option, if you do paste a lot. :)

like image 175
Kent Avatar answered Sep 29 '22 06:09

Kent


Either auto-indent or smart-indent is doing this to you. Do this before pasting:

: set noai
: set nosi

And then paste. it should paste ok. When you're done pasting, do:

:set ai
:set si

ai is short for autoindent.

si is short for smartindent.


Following Kent's comment to the question, you can also do this with :set paste. Here's the explanation why from the official help:

                        *'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.
like image 25
Nathan Fellman Avatar answered Sep 29 '22 04:09

Nathan Fellman