将文本粘贴到 vim 时关闭自动缩进 [英] Turning off auto indent when pasting text into vim

查看:46
本文介绍了将文本粘贴到 vim 时关闭自动缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习 Vim.

I am making the effort to learn Vim.

当我将代码从剪贴板粘贴到我的文档中时,我会在每行的开头获得额外的空格:

When I paste code into my document from the clipboard, I get extra spaces at the start of each new line:

line
  line
    line

我知道你可以关闭自动缩进,但我无法让它工作,因为我有一些其他设置冲突或其他东西(这在我的 .vimrc 中看起来很明显,但当我把它们取出来时似乎并不重要).

I know you can turn off auto indent but I can't get it to work because I have some other settings conflicting or something (which look pretty obvious in my .vimrc but don't seem to matter when I take them out).

如何在粘贴代码时关闭自动缩进,但在编写代码时仍然有 vim 自动缩进?这是我的 .vimrc 文件:

How do I turn off auto indenting when I paste code but still have vim auto indent when I am writing code? Here is my .vimrc file:

set expandtab  
set tabstop=2  
set shiftwidth=2  
set autoindent  
set smartindent  
set bg=dark  
set nowrap  

推荐答案

更新: 更好的答案:https://stackoverflow.com/a/38258720/62202

要在粘贴代码时关闭自动缩进,有一种特殊的粘贴"模式.

To turn off autoindent when you paste code, there's a special "paste" mode.

输入

:set paste

然后粘贴您的代码.请注意,工具提示中的文本现在显示 -- INSERT(粘贴)--.

Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

粘贴代码后,关闭粘贴模式,以便在键入时自动缩进再次正常工作.

After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

:set nopaste

但是,我总是觉得那很麻烦.这就是为什么我映射 以便它可以在编辑文本时在粘贴和非粘贴模式之间切换 我将其添加到 .vimrc

However, I always found that cumbersome. That's why I map <F3> such that it can switch between paste and nopaste modes while editing the text! I add this to .vimrc

set pastetoggle=<F3>

这篇关于将文本粘贴到 vim 时关闭自动缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆