在粘贴一大堆代码时,vim中的智能缩进(通常很棒)是一种灾难

时间:2022-11-22 21:05:18

I guess other editors are smart enough to turn that stuff off for pasting but when using vim in a terminal it can't distinguish between pasting and actual typing.

我想其他编辑都足够聪明,可以将这些东西用于粘贴,但是当在终端中使用vim时,它无法区分粘贴和实际打字。

What kinds of solutions or workarounds do you have for this?

您有什么样的解决方案或解决方法?

Added: there's also a setting that makes comments automatically continue on the next line. The indenting at least doesn't change the semantics of the code but the auto comment continuation really screws things up. Come to think of it, I should just turn that off altogether -- anyone know what that option is called?

补充:还有一个设置可以让评论自动在下一行继续。缩进至少不会改变代码的语义,但自动注释延续确实搞砸了。想想看,我应该完全关闭它 - 任何人都知道这个选项叫什么?

4 个解决方案

#1


:set paste is the way to go, but if you forget, as I often do, then if you are using a language with {} as the open/close of blocks, then doing a =% on the first { or last } will reapply the indenting.

:设置粘贴是要走的路,但如果你忘了,就像我经常那样,那么如果你使用{}作为块的打开/关闭的语言,那么在第一个{或最后}上做一个=%将重新申请缩进。

#2


:set paste

#3


add this to your .vimrc and use it with the F2 key to toggle paste status before and after you add in chunks of code:

将此添加到.vimrc并使用F2键在添加代码块之前和之后切换粘贴状态:

set pastetoggle=<F2>

#4


Another way to do this, assuming you have your system clipboard set up properly is to do

另一种方法是假设您正确设置了系统剪贴板

"*p

This will paste from the system clipboard.

这将从系统剪贴板粘贴。

Check your vim --version. On OS X you'll need +clipboard and on Linux +xterm_clipboard, I believe.

检查你的vim --version。在OS X上,你需要+剪贴板,在Linux + xterm_clipboard上,我相信。

If you're on OS X, you can always brew install macvim and use mvim -v instead of the bundled Vim (it was not compiled with +clipboard).

如果您使用的是OS X,那么您可以随时使用mvim -v而不是捆绑的Vim(它没有使用+剪贴板编译)来安装macvim。

#1


:set paste is the way to go, but if you forget, as I often do, then if you are using a language with {} as the open/close of blocks, then doing a =% on the first { or last } will reapply the indenting.

:设置粘贴是要走的路,但如果你忘了,就像我经常那样,那么如果你使用{}作为块的打开/关闭的语言,那么在第一个{或最后}上做一个=%将重新申请缩进。

#2


:set paste

#3


add this to your .vimrc and use it with the F2 key to toggle paste status before and after you add in chunks of code:

将此添加到.vimrc并使用F2键在添加代码块之前和之后切换粘贴状态:

set pastetoggle=<F2>

#4


Another way to do this, assuming you have your system clipboard set up properly is to do

另一种方法是假设您正确设置了系统剪贴板

"*p

This will paste from the system clipboard.

这将从系统剪贴板粘贴。

Check your vim --version. On OS X you'll need +clipboard and on Linux +xterm_clipboard, I believe.

检查你的vim --version。在OS X上,你需要+剪贴板,在Linux + xterm_clipboard上,我相信。

If you're on OS X, you can always brew install macvim and use mvim -v instead of the bundled Vim (it was not compiled with +clipboard).

如果您使用的是OS X,那么您可以随时使用mvim -v而不是捆绑的Vim(它没有使用+剪贴板编译)来安装macvim。