Vim 复制/粘贴弄乱了缩进

Vim 复制/粘贴弄乱了缩进

每当我从另一个应用程序复制某些内容然后将其粘贴到 vim 中时,缩进都会变得混乱。

例如,刚才我尝试从用于创建 chrome 扩展的 hello-world 教程中复制 manifest.json 文件。

它看起来像这样:

{
  "manifest_version": 2,

  "name": "One-click Kittens",
  "description": "This extension demonstrates a browser action with kittens.",
  "version": "1.0",

  "permissions": [
    "https://secure.flickr.com/"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

但是,当我将其粘贴到 vim 中时,它看起来像这样: 在此处输入图片描述

我的vimrc如下:

"se t_Co=256
syntax enable

set nowrap
set mouse=a
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab 
set number
set showcmd
set cursorline
set showmatch

execute pathogen#infect()
"filetype plugin indent on

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=1            

set clipboard=unnamed  "share one clipboard for everyhting    

它与以下行有关:

execute pathogen#infect() "filetype plugin indent on

如果我将其注释掉,问题就解决了。但是,这是我在用 Python 编码时用来实现自动缩进的方法。还有其他方法可以实现自动缩进吗?

答案1

在终端中,Vim 无法区分输入的文本(您希望自动缩进的位置)和粘贴的文本。因此,有一个'paste'选项(并'pastetoggle'简化处理),设置后将禁用自动格式化和缩进。另一种方法是使用图形 GVIM,它可以检测到这一点。

或者,您可以使用 Vim 的剪贴板访问(如果已配置并正常工作,您需要试用一下),并使用"*/"+寄存器来选择 / 系统剪贴板,例如通过"+p:put +。也许使用鼠标中键粘贴也可以;试试看!

答案2

:设置粘贴

或查看http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste

(这里有些字符,因为superuser.com 认为简短的答案并不是好的答案)

答案3

一种可能性(可能不适合您)是使用gvim而不是vim。后者能够区分粘贴和快速​​文本输入。来自vim的帮助:paste

'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.

答案4

  • 在编辑器中,按下esc进入命令模式。
  • 类型:set paste
  • 按 i 切换到编辑器的 INSERT 模式
  • 粘贴您的内容

相关内容