Vim 复制粘贴时每行代码缩进

Vim indents every line of code when copying-pasting

当我复制并粘贴 Vim 中的一段代码时,每一行都缩进了一个。例如,我有这个来源:

    print "Hello"
    print "World"
    print "I'm copying"
    print "and pasting"

粘贴到 Vim 时会出现混乱:

print "Hello"
        print "World"
            print "I'm copying"
                print "and pasting"

对于复制长行代码,这非常令人沮丧,因为所有内容都没有对齐(对 python 不利)。

这是我的 vimrc。它目前在换行符上自动缩进,并用标准的 4 个空格替换制表符。

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

虽然此配置有效,但某些原因导致了复制粘贴问题。如何解决这个问题,但仍然保留我定义的行为?

使用:set paste切换到粘贴模式。

This article explains paste mode

它专门用于将文本粘贴到 vim,因此不会触发任何输入映射。完成后记得 :set nopaste 取回映射。