search

Home  >  Q&A  >  body text

When Vim pastes code, hierarchical indentation is displayed. How to solve it?

滿天的星座滿天的星座2838 days ago694

reply all(4)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:42:14

    Before pasting:
    :set paste
    Finished pasting:
    :set nopaste


    Another great method:
    :r! cat
    Then use
    shift+insert
    or other way to paste the code, then
    ctrl+d
    Complete input.

    Source

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 16:42:14

    I always use another better way:

    augroup PASTETOGGLE
      autocmd! InsertEnter * setlocal paste
      autocmd! InsertLeave * setlocal nopaste
    augroup END
    

    This way you don’t need to set anything at all when pasting in insert mode, and there is no need for alternative key combinations.

    Supplement: I forgot to mention the side effects of this method. It will cause the options such as 'autoindent' and 'smartindent' in insert mode to be reset, so it is equivalent to canceling automatic indentation, so please use it selectively.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 16:42:14

    Use vi to paste the code and it’s OK

    reply
    0
  • 阿神

    阿神2017-05-16 16:42:14

    You can also do this “+p to paste;
    For convenience, I made a mapping nnoremap <leader>p "+p;
    Type :reg to see what's held in the register;
    Select some characters first, and then ”+y you can copy the selected part to the system clipboard;
    Reference Accessing the system clipboard How to use vim registers?

    reply
    0
  • Cancelreply