14

Whenever I copy something from another application and then go to paste it into vim, it messes up the indentation.

For example, just now I tried to copy the manifest.json file from the hello-world tutorial for creating chrome extensions.

It looks like this:

{
  "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"
  }
}

but, when I paste it into vim, it looks like this: enter image description here

My vimrc is as follows:

"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    

It has something to do with this line:

execute pathogen#infect() "filetype plugin indent on

If I comment it out, The problem is resolved. However, this is what I use to achieve auto-indent for when I am coding in python. Is there another way to get auto-indent?

3
  • How do you paste, with middle mouse button? Read :help 'paste' for an explanation and toggle that option before / after pasting, or paste from within Vim (via the register +, e.g. "+p. Jan 10, 2015 at 17:40
  • woah, so I have always pasted by right-clicking and choosing paste, but I just tried it with the middle mouse button and it worked as expected.
    – Luke
    Jan 10, 2015 at 17:56
  • Ah, great! I've expanded my comment into a proper answer; please accept by clicking on the outlined checkmark next to it. Jan 10, 2015 at 19:07

6 Answers 6

14

In the terminal, Vim cannot distinguish between typed text (where you want automatic indenting), and pasted text. So there's the 'paste' option (and 'pastetoggle' to simplify handling), which when set disables auto-formatting and -indenting. An alternative is using graphical GVIM, which can detect that.

Or, you use Vim's clipboard access (if configured and working, which you need to try out), and use the "* / "+ registers for the selection / system clipboard, e.g. via "+p or :put +. Maybe pasting with the middle mouse button also just works; try it out!

17

:set paste

or see http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste

(herearesomecharsbecausesuperuser.comthinksshortanswersarenotanygoodanswers)

2
  • This did not fix the problem
    – Luke
    Jan 10, 2015 at 17:52
  • This worked for me. Outside of insert mode I typed the :set paste command, hit enter, then command+v (I'm on a mac) to paste, and it pasted my content without messing up the indentation. :set paste doesn't do it itself, it just sets it up so you can do it. Probably need to :set nopaste afterwards to get the regular vim features back.
    – RustyToms
    Sep 14, 2018 at 15:33
2

One possibility, which may not be an option for you, is to use gvim instead of vim. The latter is able to distinguish pasting from rapid text typing. From vim's help for :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.
2

One of the best solutions to this issue that I've encountered is to use the bracketed paste plugin: https://github.com/ConradIrwin/vim-bracketed-paste - this allows you to paste directly into vim without worrying about the indentation going awry .. and without needing to hit different keys prior to pasting etc etc

1
  • While inside the editor, press esc to enter the command mode.
  • Type :set paste
  • Press i to switch to the INSERT mode of the editor
  • Paste your content
0

You could use:

inoremap <c-r>+ <c-r><c-p>+

You can read more in :h i_ctrl-r. For normal mode you can do.

nnoremap <leader>+ "+p
nnoremap <leader>0 "0p

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .